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 = B00010100;  /* Turn Right*/  
delay(500);
PORTD = B00000000;  /* Briefly Stop*/ 
delay(5);
}

void TurnLeft(){
PORTD = B00101000;  /* Turn Left*/  
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 > 500){
          PORTB = B00000001;
          TurnLeft();
          Serial.println("Turn Right ");
          } 

       if (LeftVal > 500 ){
          PORTB = B00000010;        
          TurnRight();
          Serial.println("Turn Left ");
          } 

       if(RightVal