Button Push Counter Reset with modulo

im making a electronic trigger. im trying to make different firing modes. i used the Button state change as my base code, this is the code

const int solenoidpin = 5; // initalize pin 5 for solenoid
const int triggerswitchpin = 7;  // intialize pin 7 for trigger switch
boolean running = false;
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// 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);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  pinMode (solenoidpin, OUTPUT);  //output voltage to solenoid
  pinMode(triggerswitchpin, INPUT); //input voltage from trigger switch
  digitalWrite(triggerswitchpin, HIGH); //input reads high when trigger is pulled
  Serial.begin(9600);
}


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
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(10);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);

  }
  if (buttonPushCounter % 3 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(30);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  if (buttonPushCounter % 2 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(200);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  else
    buttonPushCounter % 1;

}

the problem in having is after i hit the button 3 times i want it to end up back and mode 4. can someone tell me what im doing wrong. thankyou

In one place, you use % 4. In another, you use % 3. In another, you use % 2. In another, you use %1. Which is correct?

I don't think you understand what the % function does. It returns the remainder of the integer division.

6 % 4 is 2. 7 % 4 is 3. 8 % 4 is 0.

Don't keep starting new threads. Keep all your problems in one place.

i dont know if i have this right or not. it works fine except when i get to the if (buttonPushCounter 4 i want it to do nothing. then if i press it again i want it to start back at number 1. instead it activates the relay slowly instead of not activating it at all. then if i press it again it goes back to number one like i would like it to.

const int solenoidpin = 5; // initalize pin 5 for solenoid
const int triggerswitchpin = 7;  // intialize pin 7 for trigger switch
boolean running = false;
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// 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);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  pinMode (solenoidpin, OUTPUT);  //output voltage to solenoid
  pinMode(triggerswitchpin, INPUT); //input voltage from trigger switch
  digitalWrite(triggerswitchpin, HIGH); //input reads high when trigger is pulled
  Serial.begin(9600);
}


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
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 1 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(10);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);

  }
  if (buttonPushCounter % 2 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(30);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  if (buttonPushCounter % 3 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(200);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(solenoidpin, HIGH);
    (buttonPushCounter =0);
  }
}

what do i have to do to make if (buttonPushCounter % 4 == 0) { sort of like a saftey and not fire. then one more press brings me back to number 1. sorry and thankyou

i dont know if i have this right or not.

not

Did you note what PaulS said about your use of the modulo operator ? As your code is at the moment you might just as well test the actual value of buttonPushCounter

Try this instead

switch ( buttonPushCounter % 4)
{
  case 0:  //no remainder
    //whatever
    break;

  case 1:  //remainder is 1
    //whatever
    break;

  case 2:  //remainder is 2
    //whatever
    break;

etc
etc
}

the part im having probl;ems with as you already know is

switch (  if (buttonPushCounter % 1 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(10);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);

  }
  if (buttonPushCounter % 2 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(30);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  if (buttonPushCounter % 3 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(200);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(solenoidpin, HIGH);
    (buttonPushCounter =0);
  }
}

if i bring up the console with button presses it goes through 1,2,3,4,1,2,3,4

am i suposed to use case 1: through 4. or should i have a if (buttonPushCounter % ? ==? for each mode. i read some about modulo bu ti cant seem to picture how its used for more than one mode. if % 2 == 2 then that means mode 1?

switch ( if (buttonPushCounter % 1 == 0) {

The if statement does not return a value, so it can NOT be the value that switching happens on.

What is the remainder when you divide by 1? Can it EVER be anything other than 0? Get a clue about what the modulo operator does or quit using it.

As Paul previously explained, modulo returns the remainder of the division so when you use

buttonPushCounter % 4

you will get a number representing the current state derived from the current value of buttonPushCounter and you never need to reset the value of buttonPushCounter as long as you use an unsigned data type so that it can never go negative.

Change 4 to another number and you can increase or decrease the number of states if you need to, but you can use the remainder returned by modulo directly using if or switch/case without using modulo again.

sorry it is supposed to be

 if (buttonPushCounter % 1 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(10);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);

  }
  if (buttonPushCounter % 2 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(30);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  if (buttonPushCounter % 3 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(200);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(solenoidpin, HIGH);
    (buttonPushCounter =0);
  }
}

i didnt pick modulo it just happened to be part of the example i chose.. and so far its the only thing that has worked properly except for not knowing how to use it. can you correct the script to function properly please

maybe i have been awake too long. but it would really help to see how it is supposed to be. then maybe i could visualize it better

this works until i get to number 4. when i get to 4 i dont want it to do anything until i hit the button i want it to go back to 1

if (buttonPushCounter % 1 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(10);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);

  }
  if (buttonPushCounter % 2 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(30);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  if (buttonPushCounter % 3 == 0) {
    if (digitalRead(triggerswitchpin) == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      delay(200);        //delay to debounce switch
      digitalWrite(ledPin, LOW);
      running = !running;    //toggle running variable
      digitalWrite(solenoidpin, running);  //sends signal to solenoid
      delay (20); //dwell time for solenoid
    }
    else if (digitalRead(triggerswitchpin) == LOW)
      digitalWrite(solenoidpin, HIGH);
  }
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(solenoidpin, HIGH);
    (buttonPushCounter =0);
  }
}

:astonished: :astonished:

 if (buttonPushCounter % 1 == 0)

Please stop doing that, or at least get the syntax right

What is the remainder when you divide any positive integer by 1 ?

Did you read post #3 and post #6 ?

Forget the modulo operator, which you clearly do not understand.

if(buttonPushCounter == 0)
   // do the zero thing
else if(buttonPushCounter == 1)
   // do the one thing
else if(buttonPushCounter == 2)
   // do the two thing
else if(buttonPushCounter == 3)
   // do the three thing
else
   buttonPushCounter = 0; // reset to 0.

PaulS: Forget the modulo operator, which you clearly do not understand.

if(buttonPushCounter == 0)
   // do the zero thing
else if(buttonPushCounter == 1)
   // do the one thing
else if(buttonPushCounter == 2)
   // do the two thing
else if(buttonPushCounter == 3)
   // do the three thing
else
   buttonPushCounter = 0; // reset to 0.

thankyou. i have only been learning arduino for about a week now. the only reason i used modulo was because i was just adding to the example sketch. i CLEARLY have alot to learn. thankyou the both of you!! why did they use the modulo operator in the button state example sketch?? btw as you probably already knew that worked perfectly !

why did they use the modulo operator in the button state example sketch??

They used it correctly.

   switch(buttonPushCounter % 4)
   {
      case 0:
         // do the zero thing
         break;
      case 1:
         // do the one thing
         break;
      case 2:
         // do the two thing
         break;
      case 3:
         // do the three thing
         break;
   }

would do the same thing as the code I posted last time, without the need to reset buttonPushCounter.

this is how they showed it on my end

/*
  State change detection (edge detection)

 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.

 This example shows how to detect when a button or button changes from off to on
 and on to off.

 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)

 created  27 Sep 2005
 modified 30 Aug 2011
 by Tom Igoe

This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/ButtonStateChange

 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// 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);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


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
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

this is how they showed it on my end

So, "the zero thing" is to turn the LED pin on, and "the one thing", "the two thing", and "the three thing" are to turn the LED pin off.

That sort of explains things. so thats why its off during 123 but on during 4. idk why they didnt just use what you just showed me. i dont think i can stay up any longer. i will be back at it again later my next step is to use a rgb led to identify modes. i got it to come on but not turn off. im going to try to figure that one out myself lol. thanks again for your help