My first program works but I have questions

Hi,
I have taken my 3pi bot and added 3 sharp 810Z0F sensors (Connected to PD0, PD1, PC5). What I want to make is a desk wanderer that will use the built on IR sensors and my added sensors to stay on a desk top. I am now just testing the Sharp sensors. What I have does seem to work but I do not understand some of the code fully. Can someone give me a detailed explanation of the following lines? And please tell me what you think of my program and how I may improve it.

Thanks

Mike

#include <pololu/orangutan.h>

int main()               
{
DDRD&=~(1<<PD0);
DDRC&=~(5<<PC0);
  while(1)
  {
  	if(!(PIND&(1<<PD0))){
    print("Left");
	set_motors(75,50);          
    delay_ms(200);
	}
	if(!(PIND&(1<<PD1))){
    print("Front");
	set_motors(75,-75);          
    delay_ms(200);
	}
	if(!(PINC&(1<<PC5))){
    print("Right");
	set_motors(50,75);          
    delay_ms(200);
	}
	else { 
	set_motors(75,75);
    clear(); 
	}                
  }
}

Hello Mike,

Your bitwise assignments are not doing what you intend them to do.

This code clears the first bit of the D data direction register (this makes PIND0 an input):

DDRD&=~(1<<PD0);

This code clears the fifth bit of the C data direction register (this makes PINC5 an input):

DDRC&=~(1<<PC5);

Notice that I do not change the 1 to a 5. The reason your program worked when you did this is that all of the bits of the data direction registers start out cleared (all IO pins as inputs) when the AVR starts. It is good practice to clear these bits when starting your program, because it makes it more clear that you intend to use those pins as inputs, and it might protect the sensors if the AVR somehow gets into a weird state. Your program also does not explicitly clear PD1. You could do that with this code:

DDRD&=~(1<<PD1);

I noticed that you are using the Orangutan include file for a 3pi program. This works because the Orangutan and the 3pi have similar pinouts, but if you wanted to use any specific 3pi functionality you should use this include statement instead:

#include <pololu/3pi.h>

When you use the 3pi library, you should be careful about using the line sensor functions and PC5 as an input. By default, when you call a line sensor function it drives PC5 high to light the IR LEDs. A sensor connected to PC5 could be damaged by this. You can avoid this by calling pololu_3pi_init_disable_emitter_pin instead of pololu_3pi_init.

Also, you should be aware that there is an LED on PD1. If your source on PD1 is weak, it may not be able to source enough current to light the LED and drive AVR’s IO pin high. When I tried to use a 810Z0F sensor with PD1 it was unreliable for me. Variations in the manufacturing of the sensor and the AVR might make it more (or less) reliable for you.

In general, you should ask specific questions about how programs work (especially programs you wrote) instead of asking people to explain every line to you. If you have any specific questions about your program feel free to ask them.

- Ryan

Thanks for the help Ryan. Sorry, for my posting mistake. I wanted only 2 lines explained, but somehow my comments on the code were missing. I see what you mean about using PC5. I will move my sensors out to ADC6 and just use a threshold setting to trigger my if statement. This way I can still use my line sensors. Is there a good explanation somewhere of how to assign the pins as inputs, outputs, or analogs. Can you assign a variable to one of the pins? Thanks again for all the input. It is greatly appreciated.

Mike