[code]
int sensorPinA0 = A0; // pin that the Bottom/Left sensor is attached to
int sensorPinA1 = A1; // pin that the Right sensor is attached to
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9; // H-bridge enable pin
const int switchPin = 2;
// variables:
int sensorValueA0 = 0; // Bottom/Left photoresistor
int sensorValueA1 = 0; // Right photoresistor
void setup() {
// Send debugging information via the Serial monitor
Serial.begin(9600);
analogReference(INTERNAL);
//Assign and set motor driver pins to low
pinMode(motor1Pin, OUTPUT);
//digitalWrite(motor1Pin, LOW);
pinMode(motor2Pin, OUTPUT);
//digitalWrite(motor2Pin, LOW);
pinMode(enablePin, OUTPUT);
//digitalWrite(enablePin, HIGH);
//pinMode(9, OUTPUT);
pinMode(sensorValueA0, INPUT);
pinMode(sensorValueA1, INPUT);
pinMode(switchPin, INPUT);
//digitalWrite(9, LOW);
//pinMode(8, OUTPUT);
//digitalWrite(8, LOW);
}
void loop()
{
// read the sensor:
sensorValueA0 = analogRead(sensorPinA0);
sensorValueA1 = analogRead(sensorPinA1);
// if the switch is high, motor will turn on one direction:
if (sensorValueA0 > sensorValueA1)
{
digitalWrite(switchPin, HIGH); // set the switch pin high
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
//digitalWrite(enablePin, HIGH);
delay(100);
}
else
{
digitalWrite(switchPin, LOW); // set the switch pin high
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge high
}
if (sensorValueA0 < sensorValueA1)
{
digitalWrite(switchPin, HIGH);
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
//digitalWrite(enablePin, HIGH);
}
else
{
digitalWrite(switchPin, LOW); // set the switch pin high
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge high
}
//Print debugging information
Serial.print("left reading = ");
Serial.println(sensorValueA0); // the left/bottom sensor analog reading
Serial.print("right reading = ");
Serial.println(sensorValueA1); // the right sensor analog reading
//Serial.print("Top reading = ");
//Serial.println(sensorValueA2); // the top sensor analog reading
delay(1000);
}
[/code]
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w