Code Example

int RightVal = 0;           // variable to store the value read
int LeftVal = 0;            // variable to store the value read 

void Forward(){ 
PORTD = B00100100;/* Go Forward*/
delay(10);
}

void TurnRight(){
PORTD = B00011000;/* Go Reverse*/  
delay(750);
PORTD = B00000000;  /* Briefly Stop*/ 
delay(5);
PORTD = B00010100;  /* Turn Away*/  
delay(500);
PORTD = B00000000;  /* Briefly Stop*/ 
delay(5);
}

void TurnLeft(){
PORTD = B00011000;/* Go Reverse*/  
delay(750);
PORTD = B00000000;  /* Briefly Stop*/ 
delay(5);
PORTD = B00101000;  /* Turn Away*/  
delay(500);
PORTD = B00000000;  /* Briefly Stop*/ 
delay(5);
}

void setup()
{
DDRD = DDRD | B11111100; //Data Direction Register for Port D
DDRB = DDRB | B00000011; //Data Direction Register for Port B
/* This is the same as written above
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT); 
  pinMode(4, OUTPUT); 
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  pinMode(7, OUTPUT); 
  pinMode(8, OUTPUT); 
  pinMode(9, OUTPUT);
  pinMode(10, INPUT); 
  pinMode(11, INPUT); 
  */
Serial.begin(9600);      // open the serial port at 9600 bps: 
}
 
void loop()
{
      RightVal = analogRead(0);    // read the input pin 0
      LeftVal = analogRead(1);     // read the input pin 1
      Serial.print("Right Signal = "); 
      Serial.println(RightVal);
      Serial.print("Left Signal = ");
      Serial.println(LeftVal);
      Serial.println("");      // prints a carriage return
       if (RightVal > 400){
          PORTB = B00000001;
          TurnRight();
          Serial.print("Turn Right ");
          } 

       if (LeftVal > 400 ){
          PORTB = B00000010;        
          TurnLeft();
          Serial.print("Turn Left ");
          } 

       if(RightVal