Photovore Arduino car

From Wiki

Jump to: navigation, search



Photovore circuit diagram.png


Photovore arduino car1.jpg

Photovore arduino car2.jpg

Photovore arduino car3.jpg


#include <AFMotor.h>  //Use Adafruit Motor Shield library

/**********************************************************************
 *  Program for a simple light Seeking Robot.  Based on:
 *  http://www.instructables.com/id/First-Arduino-Robot-Light-Seeker
 *
 *  Physical setup:
 *  Two photoresistors connected to ground and 
 *    to Analog pin 1(Right) and Analog pin 2(left)
 *  2 DC motors connected to DC inputs 3 and 4 of Adafruit Motor Shield
 ********************************************************************/
// Using Arduino Uno with Adafruit Motor Shield


// Pin definitions - attaches a variable to a pin.
AF_DCMotor motor3(3, MOTOR12_64KHZ); // create motor #3, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #4, 64KHz pwm   
const int RightSensor = 1;    // pin used to read the value Right Sensor.
const int LeftSensor = 2;     // pin used to read the value Left Sensor.


// Variable definitions
int SensorLeft;
int SensorRight;        
int SensorDifference;   


void setup()   {                
  pinMode(LeftSensor, INPUT);  // Defines this pin as an input.  
                               // Arduino reads values from this pin.
  pinMode(RightSensor, INPUT); // Defines this pin as an input.  
                               // Arduino reads values from this pin.
  
  // One pin of the LDR is connected to ground. 
  // The other is connected to the analog input
      // pin and a pullup resistor is connected to the same analog 
      // input pin and +5v. 
  // Luckily, Arduino has a pullup resistor built in to every pin. 
  // To use the pullup
  // resistor, you must first define the pin as an input, 
  // then use digitalWrite() to set the pin HIGH
  digitalWrite(A1, HIGH);     // Enables an internal pullup resistor 
  digitalWrite(A2, HIGH);     // Enables an internal pullup resistor 
  Serial.begin(9600);   // Enables a serial connection to Arduino; 9600
  motor3.setSpeed(204); // set the speed to 204/255 LEFT
  motor4.setSpeed(240); // set the speed to 240/255 RIGHT
}


//methods
void forward() {
  motor3.run(FORWARD); // turn it on going forward
  motor4.run(FORWARD); // turn it on going forward    
}

void turnLeft() {
  motor3.run(BACKWARD); // turn it on going forward
  motor4.run(FORWARD); // turn it on going forward   
}

void turnRight() {
  motor3.run(FORWARD); // turn it on going forward
  motor4.run(BACKWARD); // turn it on going forward  
}

void loop()    {
  SensorLeft =  1023 - analogRead(LeftSensor);
  delay(1);
  SensorRight = 1023 - analogRead(RightSensor);
  delay(1);
  SensorDifference = abs(SensorLeft - SensorRight);

  // interpret the data and run the motors accordingly
  if (SensorLeft > SensorRight && SensorDifference >= 75) {    
    turnLeft();
  }

  else if (SensorLeft < SensorRight && SensorDifference >= 75) {    
    turnRight();
  }

  else if (SensorDifference < 75) {       
    forward();
  }
}

Personal tools