code
int motorA = 3; //Direction Pin for Left Motor
int motorB = 4; //Direction Pin for Left Motor
int motorSpeedA = 6; //Speed Pin for Left Motor
int motorSpeedB = 5; //Speed Pin for Right Motor
int motorC = 7; //Direction Pin for Right Motor
int motorD = 8; //Direction Pin for Right Motor
// Variable definitions
int SensorRight; // This pin is used to read the value of the Right Sensor.
int SensorLeft; // This stores the value of the Right Sensor pin to use later on in the sketch
int SensorDifference; // This value is used to determine the difference between the Left and Right
// the setup() method runs once when the program is run. When the
// Arduino is reset, the setup() will be executed once again.
void setup()
{
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
pinMode(motorC, OUTPUT);
pinMode(motorD, OUTPUT);
pinMode(motorSpeedA, OUTPUT);
pinMode(motorSpeedB, OUTPUT);
pinMode(LeftSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
pinMode(RightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
digitalWrite(motorSpeedA,HIGH);
digitalWrite(motorSpeedB,HIGH);
digitalWrite(A1, HIGH); // Enables an internal pullup resistor
digitalWrite(A2, HIGH); // Enables an internal pullup resistor
Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600
Serial.println(" \nBeginning Light Seeking Behavior"); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
SensorLeft = 960 - analogRead(LeftSensor);
delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.
SensorRight = 1020 - analogRead(RightSensor);
delay(1);
SensorDifference = abs(SensorLeft - SensorRight); // This calculates the difference between the two sensors and then saves it to an integer.
// This section of the sketch is used to print the values of the
// sensors through Serial to the computer. Useful for determining
// if the sensors are working and if the code is also functioning properly.
Serial.print(“Left Sensor = “); // Prints the text inside the quotes.
Serial.print(SensorLeft); // Prints the value of the Left Sensor.
Serial.print(”\t”); // Prints a tab (space).
Serial.print(“Right Sensor = “); // Prints the text inside the quotes.
Serial.print(SensorRight); // Prints the value of the Right Sensor.
Serial.print(”\t”); // Prints a tab (space).
// This section of the sketch is what actually interperets the data and then runs the motors accordingly.
if (SensorLeft > SensorRight && SensorDifference > 85)
{
LeftSpin();
}
if (SensorLeft < SensorRight && SensorDifference > 85)
{
RightSpin();
}
else if (SensorDifference < 150) {rints Forward when the robot would actually go forward.
}
Forward();
}
void Forward()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void Reverse()
{
digitalWrite(motorC,HIGH);
digitalWrite(motorD,LOW);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void LeftSpin()
{
digitalWrite(motorC,HIGH);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,HIGH);
}
void Rightspin()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,HIGH);
digitalWrite(motorA,HIGH);
digitalWrite(motorB,LOW);
}
void Stop()
{
digitalWrite(motorC,LOW);
digitalWrite(motorD,LOW);
digitalWrite(motorA,LOW);
digitalWrite(motorB,LOW);
delay(10);
}