Light Sensor Robot

This project my partner and I build and programmed this robot and our objective was make the robot turn and move towards the light.

int pwm_a = 10;  //PWM control for motor outputs 1 and 2 is on digital pin 10
int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 11
 int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 13
int incomingByte = 0; // for incoming serial data
 int encoderCount=0;
int encoderOld=0;
int encoderState=0;
int sensorState=0;
int lightsensor_left=0;
int lightsensor_right=0;
int temp_var=0;
int gain=2; // control system gain var
const int encoderPin = 2;
 const int sensorPin = 3;

void setup()
{
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);

  analogWrite(pwm_a, 0);  //set both motors to run at (100/255 = 39)% duty cycle (slow)
   analogWrite(pwm_b, 0);

  Serial.begin(115200);  

  Serial.print("Zagros Robotics, Inc.");
  Serial.print("Magician Light Follower Demo 4/6/2013\n ");

}

void loop()
{

        // Serial.print("Zagros Robotics, Inc.");

   if (Serial.available() > 0) {
  // read the incoming byte:
  incomingByte = Serial.read();
  }

  //read light sensors

lightsensor_left=analogRead(0);
lightsensor_right=analogRead(1);
Serial.println(lightsensor_left);
Serial.println(lightsensor_right);

  switch(incomingByte)
  {
     case 's':
        digitalWrite(dir_a, LOW); 
       analogWrite(pwm_a, 0);
       digitalWrite(dir_b, LOW); 
       analogWrite(pwm_b, 0);
       Serial.println("Stop\n");
       incomingByte='*';

     break;

     case 'f':
       digitalWrite(dir_a, LOW); 
       analogWrite(pwm_a, 255);
       digitalWrite(dir_b, LOW); 
       analogWrite(pwm_b, 255);
       Serial.println("Forward\n");
        incomingByte='*';
     break;

      case 'b':
       digitalWrite(dir_a, HIGH); 
       analogWrite(pwm_a, 255);
       digitalWrite(dir_b, HIGH); 
       analogWrite(pwm_b, 255);
        Serial.println("Backward\n");
       incomingByte='*';
     break;

     case 'l':
       digitalWrite(dir_a, LOW);
       analogWrite(pwm_a, 255);
       digitalWrite(dir_b, HIGH); 
        analogWrite(pwm_b, 255);
       Serial.println("Rotate Left\n");
       incomingByte='*';
     break;

       case 'r':
       digitalWrite(dir_a, HIGH);
       analogWrite(pwm_a, 255);
        digitalWrite(dir_b, LOW); 
       analogWrite(pwm_b, 255);
       Serial.println("Rotate Left\n");
       incomingByte='*';
     break;

     case 'g': //follow the light

     gain = 5;

     temp_var = (lightsensor_left - lightsensor_right)*gain;

     if (temp_var > 0)
     {
       digitalWrite(dir_a, HIGH);
       digitalWrite(dir_b, LOW); 
       Serial.println("Rotate Left\n");
      }
     else
     {
      digitalWrite(dir_a, LOW);
       digitalWrite(dir_b, HIGH); 
       Serial.println("Rotate Right\n");
     }

     Serial.println(temp_var);

     temp_var=abs(temp_var);

      if(temp_var > 255)
     {
       temp_var = 255;
     }

         analogWrite(pwm_a, temp_var);
         analogWrite(pwm_b, temp_var);

     break;

     case 'v':
     Serial.println("Version 04042013a\n");
     incomingByte='*';
     break;

    delay(500);

  }

}  // end loop

Pictures

FD8OI05HF23MV4P.LARGE (2) F6UDMEXHF23MV5E.LARGE F5GGT1LHF23MV3K.LARGE FAMV5LMHF23MV48.LARGE

Leave a Reply

Your email address will not be published. Required fields are marked *