Can someone tell me why this seemingly simple code won't run a DC motor back and forth using the "if" statement.
int westLimit = 10; // momentary limit switch
int eastLimit = 11; // momentary limit switch
int westLED = 3; // LED with 220 resistor
int eastLED = 6; // LEd with 220 resistor
int westLimitVal = 0; // defined in LOOP
int eastLimitVal = 0; // defined in LOOP
int in1 = 8; // jumper wire to L298n
int in2 = 9; // jumper wire to L298n
void setup() {
Serial.begin(9600);
pinMode(westLimit, INPUT);
pinMode(eastLimit, INPUT);
pinMode(westLED, OUTPUT);
pinMode(eastLED, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
}
void loop() {
int westLimitVal = digitalRead(westLimit);
Serial.print("westLimtVal = "); // prints zeros & ones in Serial monitor
Serial.println(westLimitVal); // prints zeros & ones in serial monitor
int eastLimitVal = digitalRead(eastLimit);
Serial.print("eastLimtVal = "); // prints zeros & ones in serial monitor
Serial.println(eastLimitVal); // prints zeros & ones in serial monitor
if (eastLimitVal == 1) {
digitalWrite(eastLED, HIGH); // uses the same jumpers(in1 & in2)
digitalWrite(in1, HIGH); // does not turn on
digitalWrite(in2, LOW); // has worked wit different configeration
} else {
digitalWrite(eastLED, LOW); // has worked with different configeration
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
if (westLimitVal == 1) {
digitalWrite(westLED, HIGH); // works fine
digitalWrite(in1, HIGH); // works fine
digitalWrite(in2, LOW); // works fine
} else {
digitalWrite(westLED, LOW); // works fine
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}