Hello everyone,
I am trying to control a DC motor with IR sensors. I know the motor works as I have ran the motor sucsessfuly without the IR sensor; however, when I try to run it with the sensors nothing happens. This is part of a bigger project but since I am having a difficult time I've scaled it back to just this basic concept so I can then build from there when I get this working. I've looked at other posts from people with similar issues and I feel like i've tried everything but nothing has worked. Appreciate any feedback I can get. Thanks!
//motor 2: controls bottom conveyor
int motor2pin1 = 7;
int motor2pin2 = 8;
int en2 = 9;
//sensors
int sensor1 = A1;
int sensor2 = A2;
void setup() {
pinMode(en2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
analogWrite(motor2pin1, LOW);
analogWrite(motor2pin2, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
int irvalue1 = analogRead(sensor1);
int irvalue2 = analogRead(sensor2);
if (irvalue1 == HIGH)
{
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin1, LOW);
}
if (irvalue2 == HIGH)
{
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin1, LOW);
}
}