If State is not working (as I expect) with a button

When I run the following code, I’m expecting the motor to turn one way when the button is pressed and the other way when it is not pressed. What is happening is that it works when it is not pressed, but when I press and hold the button it tries to go forward and backwards.

The Serial monitor shows 0's when it is not pressed, and when it is pressed it switches between 0's and 1's.

const int button1Pin = 10;  // pushbutton 1 pin
int button1State;

 void setup() {
   Serial.begin(9600);

//Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

//seting up for the buttons / switches
 pinMode(button1Pin, INPUT);

}
void loop() {
      button1State = digitalRead(button1Pin);

//when it is LOW (off) this code will not run, when it is HIGH (on) this code will run

  if (button1State == HIGH)  //LOW is not pressed and HIGH is pressed
 {
     {
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Engage the Brake for Channel A
  analogWrite(3, 150);
  Serial.println(button1State);
 }

}
if (button1State == LOW){
   digitalWrite(12, LOW); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Engage the Brake for Channel A
  analogWrite(3, 150);
   Serial.println(button1State);
}
}

I think you need to debounce your buttons. also, you have an extra { under the first if statement..

First question when anyone has switch issues (if you have button issues, see a tailor) is How is the switch wired?

  if (button1State == HIGH)  //LOW is not pressed and HIGH is pressed
if (button1State == LOW){

Are you planning to add code to deal with the other states?

PaulS: if (button1State == HIGH) if (button1State == LOW)

Are you planning to add code to deal with the other states?

button1State can only be HIGH or LOW - no other values; at lease this is the documentation on digitalRead() function (http://arduino.cc/en/pmwiki.php?n=Reference/digitalRead).

ardiri:

PaulS: if (button1State == HIGH) if (button1State == LOW)

Are you planning to add code to deal with the other states?

button1State can only be HIGH or LOW - no other values; at lease this is the documentation on digitalRead() function (http://arduino.cc/en/pmwiki.php?n=Reference/digitalRead).

I think PaulS was making a point.... if there are only two states, there's no point checking for both. If the first test fails, the other state must be the one. So checking for two, sort of looks like there might be more.

(Am I starting to think like PaulS, or what?)

JimboZA: If the first test fails, the other state must be the one. So checking for two, sort of looks like there might be more. (Am I starting to think like PaulS, or what?)

a little sarcasm can sometimes go a long way :) heh but, yes, there are no other states so the second could have been an else, but providing a nice clear answer is better :)

Originally I had something similar to the code below but tried a variety of different things to get it to work

if{
// code.
}
else{
// code
}

The debouncing looks promising, but I have never used it before. For this part of the code, I just want the motor to spin in one direction when the state is HIGh and the other direction when it's LOW.

I thought the original issue was because the switch value was getting reset, hence the 0,1,0,1,0,1 at the else section.....

PaulS:
First question when anyone has switch issues is How is the switch wired?

You need to answer that, which was a real question.

edit… see attached pic for the possibilities.

Sorry about missing that post about wiring.

It's a simple 2 prong push button from an Arduino kit. I don't have it in front of me, but it is the standard beginner example. One end good to #10 io and resister and positive, the other goes to ground.

Hmmmm ok- so it has a pullup resistor and so the pin will be high usually, and go low when button pressed.

I thought maybe your string of 1s and 0s might have been because there was no resistor and the pin was floating.

edit... I think the logic is backwards from what your code says, which is that it goes high when pressed. That shouldn't matter, it just turns the logic upside down. But just for clarity can you provide a sketch of that connection? Just scribble a pic on paper and take a photo with your phone. Is it like the sw2 or sw3 example in my attachment to reply7?

Ok well I ran your code. I have some switches already wired up on my breadboard, that happen to have pulldown resistors, I just used them.

I get 0s when not pressed and a solid stream of 1s when pressed. It could be your switch is bouncy, but that usually lasts a very short time just as it's pressed and if you hold it firmly pressed it should make decent contact and not bounce.

So I think a schematic and a photo of your set-up might help.

(ps i'm not hooked up to a motor driver, just going off what I see in the monitor.)

I would like to thank everyone that helped with this topic. I was originally using a similar, but different push button switch from another electronics package. I swapped it for the original and the code worked as expected! :)

It would be interesting for all of us to see the culprit switch in a photo. Was it just a bad one, maybe a particularly "bouncy" one?

The switch was an AC switch, which I assume that is why I was getting 0,1,0,1,0…

muldoon: The switch was an AC switch, which I assume that is why I was getting 0,1,0,1,0....

Hmmmm....

An "AC switch", ie a switch rated to switch AC, is just a switch. The AC coming out is only there because there's AC going in in the first place. Unless it was some kind of flasher thing like a car indicator relay? I'd love to see a pic or a part number....

I believe it was this one: http://www.radioshack.com/product/index.jsp?productId=2062543 It was a 1.5amp @ 12VAC.