4

Here's my circuit:

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. schematics

Thank you.

**PS: Forgive for lack of comments! But I think a good Arduino programmer should not need comments. **

  • Comments are not for extended discussion; this conversation has been moved to chat. – Nick Gammon Apr 16 '17 at 12:14
  • What if you use an empty breadboard and put in a button and resistor only? And make your code print the button state over and over? – Paul Aug 16 '17 at 4:57
1

when the switch on pin 4 is closed the stateDirection variable still reads 0, or open circuit.

you could have pull-up enabled on that pin: with a nominal value of 50k, 10k pull-down is just on the cusp of making it a logic low.

solution?

1) disable pull-up; 2) use a stronger pull-down; 3) get rid of the pull-down and rely on the pull-up alone. ...

0

If it shows low when bypassing switch and jumping to ground.. Then either bad switch or maybe simply wrong orientation of switch.

Edit: if GND is making pin 5 HIGH in logic, then your button needs to jump to GND and have the resistor on 5v. Currently it is opposite according to your schematic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.