And the code:
//pins
const int control1 = 2;
const int control2 = 3;
const int enable = 9;
const int pinDirection = 4;
const int pinOnOff = 5;
const int pot = A0;
//states
int stateOnOff = 0;
int stateOnOffPrev = 0;
int stateDirection = 0;
int stateDirectionPrev = 0;
int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;
void setup(){
pinMode(pinDirection, INPUT);
pinMode(pinOnOff, INPUT);
pinMode(control1, OUTPUT);
pinMode(control2, OUTPUT);
pinMode(enable, OUTPUT);
digitalWrite(enable, LOW);
Serial.begin(9600);
}
void loop(){
stateOnOff = digitalRead(pinOnOff);
delay(10);
stateDirection = digitalRead(pinDirection);
motorSpeed = analogRead(pot)/4;
if(stateOnOff != stateOnOffPrev){
if(stateOnOff == HIGH){
motorEnabled = !motorEnabled;
}
}
if(stateDirection != stateDirectionPrev){
if(stateDirection == HIGH){
motorDirection = !motorDirection;
}
}
Serial.println(stateDirection);
if(motorDirection == 1){
digitalWrite(control1, HIGH);
digitalWrite(control2, LOW);
}else{
digitalWrite(control1, LOW);
digitalWrite(control2, HIGH);
}
if(motorEnabled == 1){
analogWrite(enable, motorSpeed);
}else{
analogWrite(enable, 0);
}
stateDirectionPrev = stateDirection;
stateOnOffPrev = stateOnOff;
}
As you can see, the switch on pin 5 controls whether the motor is on and the potentiometer controls motor speed. Both work.
However, when the switch on pin 4 is closed the stateDirection
variable still reads 0, or open circuit. It should when pressed change the direction of the motor via the H-bridge. I've tried everything I could, new switches, higher resistance. The only time it read HIGH is when I literally plugged switch out to GND.
Serial shows stateDirection
as an unending chain of zeroes.
And finally the schematics from the original project.
Thank you.
**PS: Forgive for lack of comments! But I think a good Arduino programmer should not need comments. **