Using a pushbutton to advance a count.

I am currently trying to use a pushbutton switch is set a time delay for a sequence of flashing LED's.

Currently my code is written this way

Push1_State = digitalRead(Push1);
 Push2_State = digitalRead(Push2);
 Push3_State = digitalRead(Push3);
/*Serial.print(PB1);
Serial.println(Push1_State);
Serial.print(PB2);
Serial.println(Push2_State);*/



if(Push3_State == LOW)
  {
    count = count + 1;
    delay(50);
    if (count == 4)
      {
        count = 1;
      }
    }
  

 Time_D = 500 * count;
Serial.println(Push3_State);

I want to be able to push the pushbutton switch and advnce the count by 1 and then pass the time delay to the functions that control the flashing for the LED's.

Right now, the code kind of works.

When I activate the push button swith, I can see on the serial monitor that the count increases by one and my Time_D goes to 1000. However as soon I relase the pushbutton, the cound drops back down to 1 and the Time_D goes back to 500.

I tired to do if(Push3_State = LOW) but that didn't work.

Any tips?

Any tips?

1 - post your complete program or a short but complete program that illustrates the problem
2 - increment the variable when the button becomes pressed rather than when it is pressed. Look at the StateChangeDetection example in the IDE

Wrote this for a single button speed selector, might work for you, connect a pushbutton from GND to pin 4,each press increments counter, if no press for 1500 mS, counter value is accepted.

uint32_t tStart, timeStart, tEnd;// = 2000UL;
int speeds [] = {500, 1000, 1500, 2000, 2500};
int speed;
const byte dbTime = 15, btn = 4, ledPin = 13;
bool btnState = false, bState = true, oldBstate = true,
     timing = false, speedSet = false;
byte cntr;

void setup()
{
  //Serial.begin(115200);
  pinMode(btn, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}
void loop()
{
// debounce-----------------------------
  if (digitalRead(btn) != btnState) // they are different!
  {
    btnState ^= 1;                  // make them equal
    timeStart = millis();           // restart timer
  }
  if (millis() - timeStart > dbTime)  // if not changed for dbTime,
     bState = btnState; // btnState is valid, use it
// end debounce-------------------------
  
  if(!timing && !bState && oldBstate)
  {
    tStart = millis();
    tEnd = 1500UL;
    cntr = 0;
    timing = true;
    speedSet = true;
  }
  if(timing && !bState && oldBstate)
  {
    if(++cntr > 5)
      cntr = 1;
    tStart = millis();
    Serial.println(speeds[cntr - 1]);
    oldBstate = bState;
  }
  if(millis() - tStart > tEnd)
  {
    timing = false;    
    if(speedSet)
    {
      speed = speeds[cntr - 1];
      if(!bState)
        speed = 0;
      Serial.print("Delay set to ");
      Serial.println(speed);
      Serial.println("Press button to change");
      speedSet = false;
    }
  }
  digitalWrite(ledPin,timing);
  oldBstate = bState;
}

Bonus :slight_smile:

uint32_t tStart, timeStart, tEnd = 1500;
const byte dbTime = 25, btn = 4, ledPin = 13;
bool btnState = false, bState = true, oldBstate = true,
     timing = false;
byte cntr;

void setup()
{
  Serial.begin(115200);
  pinMode(btn, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}
void loop()
{
// debounce-----------------------------
  if (digitalRead(btn) != btnState) // they are different!
  {
    btnState ^= 1;                  // make them equal
    timeStart = millis();           // restart timer
  }
  if (millis() - timeStart > dbTime)  // if not changed for dbTime,
     bState = btnState; // btnState is valid, use it
// end debounce-------------------------
  
  if(!timing && !bState && oldBstate)
  {
    tStart = millis();
    timing = true;
  }
  if(timing && !bState && oldBstate)
  {
    cntr++;
    tStart = millis();
    oldBstate = bState;
  }
  if(millis() - tStart > tEnd)
  {
    timing = false;    
    while(cntr > 0)
    {
      digitalWrite(ledPin,HIGH);
      delay(500);
      digitalWrite(ledPin,LOW);
      delay(500);
      cntr --; 
    }
  }
  oldBstate = bState;
}

UKHeliBob

Thanks for the help. I was able to get my code working with the stateChangeDetection.