Button Push Counter Reset with modulo


im making electronic trigger. im trying make different firing modes. used button state change base code, code


code: [select]
const int solenoidpin = 5; // initalize pin 5 solenoid
const int triggerswitchpin = 7;  // intialize pin 7 trigger switch
boolean running = false;
const int  buttonpin = 2;    // pin pushbutton attached to
const int ledpin = 13;       // pin led attached to

// variables change:
int buttonpushcounter = 0;   // counter number of button presses
int buttonstate = 0;         // current state of button
int lastbuttonstate = 0;     // previous state of button

void setup() {
  // initialize button pin input:
  pinmode(buttonpin, input);
  // initialize led output:
  pinmode(ledpin, output);
  // initialize serial communication:
  pinmode (solenoidpin, output);  //output voltage solenoid
  pinmode(triggerswitchpin, input); //input voltage trigger switch
  digitalwrite(triggerswitchpin, high); //input reads high when trigger pulled
  serial.begin(9600);
}


void loop() {
  // read pushbutton input pin:
  buttonstate = digitalread(buttonpin);

  // compare buttonstate previous state
  if (buttonstate != lastbuttonstate) {
    // if state has changed, increment counter
    if (buttonstate == high) {
      // if current state high button
      // wend off on:
      buttonpushcounter++;
      serial.println("on");
      serial.print("number of button pushes:  ");
      serial.println(buttonpushcounter);
    } else {
      // if current state low button
      // wend on off:
      serial.println("off");
    }
    // delay little bit avoid bouncing
    delay(50);
  }
  // save current state last state,
  //for next time through loop
  lastbuttonstate = buttonstate;


  // turns on led every 4 button pushes by
  // checking modulo of button push counter.
  // modulo function gives remainder of
  // division of 2 numbers:
  if (buttonpushcounter % 4 == 0) {
    if (digitalread(triggerswitchpin) == high)
    {
      digitalwrite(ledpin, high);
      delay(10);        //delay debounce switch
      digitalwrite(ledpin, low);
      running = !running;    //toggle running variable
      digitalwrite(solenoidpin, running);  //sends signal solenoid
      delay (20); //dwell time solenoid
    }
    else if (digitalread(triggerswitchpin) == low)
      digitalwrite(solenoidpin, high);

  }
  if (buttonpushcounter % 3 == 0) {
    if (digitalread(triggerswitchpin) == high)
    {
      digitalwrite(ledpin, high);
      delay(30);        //delay debounce switch
      digitalwrite(ledpin, low);
      running = !running;    //toggle running variable
      digitalwrite(solenoidpin, running);  //sends signal solenoid
      delay (20); //dwell time solenoid
    }
    else if (digitalread(triggerswitchpin) == low)
      digitalwrite(solenoidpin, high);
  }
  if (buttonpushcounter % 2 == 0) {
    if (digitalread(triggerswitchpin) == high)
    {
      digitalwrite(ledpin, high);
      delay(200);        //delay debounce switch
      digitalwrite(ledpin, low);
      running = !running;    //toggle running variable
      digitalwrite(solenoidpin, running);  //sends signal solenoid
      delay (20); //dwell time solenoid
    }
    else if (digitalread(triggerswitchpin) == low)
      digitalwrite(solenoidpin, high);
  }
  else
    buttonpushcounter % 1;

}




the problem in having after hit button 3 times want end , mode 4. can tell me im doing wrong. thankyou

in 1 place, use % 4. in another, use % 3. in another, use % 2. in another, use %1. correct?

i don't think understand % function does. returns remainder of integer division.

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

don't keep starting new threads. keep problems in 1 place.



Arduino Forum > Using Arduino > Programming Questions > Button Push Counter Reset with modulo


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts