Reset Push Button Count

So after getting my Push Button State knowledge down. I decided to implement it into a project to better understand it. So I altered the push button state example code:

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int motorPin1 = 7;       // the pin that the servo is attached to
const int motorPin2 = 8;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() 
{
   // initialize the button pin as a input:
   pinMode(buttonPin, INPUT_Pullup);
   // initialize the motor1 as an output:
   pinMode(motorPin1, OUTPUT);
   // initialize the motor2 as an output:
   pinMode(motorPin2, OUTPUT);
}


void loop() 
{
   // read the pushbutton input pin:
   buttonState = digitalRead(buttonPin);

   // compare the buttonState to its previous state
   if (buttonState != lastButtonState) 
 {
     // if the state has changed, increment the counter
     if (buttonState == HIGH) 
  {
       // if the current state is HIGH then the button
       // went from off to on:
       buttonPushCounter++;
  } 
     else 
   {
       // if the current state is LOW then the button
       // went from on to off:
   }
}
   // save the current state as the last state, 
   //for next time through the loop
   lastButtonState = buttonState;

   
   // turns on the motor1 when pushed once 
   if (buttonPushCounter == 1) 
{
     digitalWrite(motorPin1, HIGH);
  } 

else 

 {
    digitalWrite(motorPin1, LOW);
   }
   
   // turns on the motor2 when pushed twice
   if (buttonPushCounter == 2) 
{
     digitalWrite(motorPin2, High);
  } 

else 

{
    digitalWrite(motorPin2, Low);

  }
  
}

Sry about the formatting, I am at school so I just wrote this in notepad.

So after I have pressed the button once, Motor1 should turn on. Then I press the button again, and Motor 2 should turn on. When I press the button a third time, I would like the counter to go 0, thus turning off both motors until I press the button again and the first motor should start again.

Is it really as simple as:

// turns on the motor2 when pushed twice
   if (buttonPushCounter == 2) 
{
     digitalWrite(motorPin2, High);
  } 

else 

{
    digitalWrite(motorPin2, Low);

  }
  
}

//Turns counter to 0 when pushed a third time
 if (buttonPushCounter == 3)
{
  (buttonPushCounter == 0)
}
  1. That seems a little too easy.

  2. It seems like that wouldn't be the correct syntax.

Any advice would help. Thank you.

You're missing a semicolon there, and you used one too many equals signs, but the concept is right.

Delta_G: You're missing a semicolon there, and you used one too many equals signs, but the concept is right.

Delta_G: You're missing a semicolon there, and you used one too many equals signs, but the concept is right.

Ahh, good point. See? If I had my arduino compiler it would have caught the semicolon. Freggin notepad. Ok so:

// turns on the motor2 when pushed twice
   if (buttonPushCounter == 2) 
{
     digitalWrite(motorPin2, High);
  } 

else 

{
    digitalWrite(motorPin2, Low);

  }
  
}

//Turns counter to 0 when pushed a third time
 if (buttonPushCounter == 3)
{
  (buttonPushCounter = 0);
}

That way I have my semicolon to say end the statement. And I set buttonPushCounter to 0, as apposed to how I was saying it "buttonPushCounter is equal to 0". Thanks delta.

you probably want to debounce your button, aka wait after you press it for like 100msec to avoid reading all the transition states on/off as it may send noise before being firmly down.

you probably want to reset your button count once you turn on the motor, else you'll loop again and notice the same button state and redo the same work.

int
readbutton()
{
 if (digitalRead(buttonPin) == HIGH){
   return 0;
 }
 sleep(100); // Confirm after 100msec button was pressed
 if (digitalRead(buttonPin) == HIGH){
   return 0;
 }
 return 1;
}

loop(){
count += readbutton();
if (count == 1){
 motoron(1);
}
if (count == 2){
 motoron(2);
}
if (count >= 3){
 motoroff(1);
 motoroff(2);
 count = 0;
}
}

@sonyhome:

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” button above the posting area (It looks like a scroll with < > inside it).

How to use this forum

fixed, but tabulation is still as ugly. I was lazy.

sonyhome: fixed, but tabulation is still as ugly. I was lazy.

Too lazy to use auto format?

what auto format? There's a vi/emacs button? I've not been on this board for long.

I'm already struggling to find the posts I've made :grin: Also using a tablet I'm having issues with the virtual kbd popping up and screwing my browser layout.

The IDE has an auto-format feature.

ah!

the ide autoformat is not great sadly. anyways, i coded this in the browser directly.