counting using a nested loop

Hello, I am new here and could use a little help. I have built a circuit using 2 push buttons as a input to produce a output to a LED to turn it on and off. The program is a loop that counts how many times the button is pressed, but I couldnt figure out a way for it to work without adding the second push button. i was wondering if anyone knew or had any tips for making a counter that would only need 1 push button to turn the LED on and off for how many times i pressed the button. Here is the code i currently have and that works but i have to use two push buttons. the first i hold down to start the loop and the second one is the counter then it exits the loop when the first button is not pressed.

int numberPush = 0;
int lastnumberPush = 0;
int i;
int j;
void setup()
{
Serial.begin(9600);  // initialize serial communication at 9600 bits per second:
}
void loop() {
  int led = 9; // led output on pin 9
  int pushState = analogRead(A0);  // read the input on analog pin 0
  int sensorValue = analogRead(A0);
  int pushState2 = analogRead(A4); // read the input on analog pin 1
if (pushState2 > 800)
{
  delay(50);
  if(pushState > 750)
  {
    numberPush += 1;
    Serial.print("number of Pushes:");
    Serial.println(numberPush);
    lastnumberPush = numberPush - 1;
    Serial.print("lastnumber:");
    Serial.println(lastnumberPush);
    pushState = analogRead(A0);  //read if button is pressed
    delay(200);
  }
  pushState2 = analogRead(A4);  // read if starting button is pressed
}
 else
  {
    for(i = 0 ; numberPush > 0 ; i++)
    {
      numberPush--;   // count down number of times button pressed
      Serial.print("count down:");
      Serial.println(numberPush);
      if(numberPush >= 0)
      {
      digitalWrite(led, HIGH); // turn led on for .3 seconds
      delay(300);
      digitalWrite(led,LOW);  // turn led off 
      delay(300);
      }
      else
      {
        digitalWrite(led,LOW); // turn led default off when no button is pressed
      }
    }
  }
 }

In the IDE select File => Examples => Digital => Button. This example program turns an LED on and off with the push of a button.

Yes but that doesn't count how many times it has been pressed. I am trying to press the button a number of times then after i stop entering a input then turn the LED on and off that many times.

I think I would incorporate state change with a timer based on "Blink without delay". Set how long of a pause is necessary to tell the program you're done pushing the button. Then as it counts toward that time check for a state change. If there is one, reset the timer and add 1 to a variable such as howManyBlinks. If it gets to the set time, blink the light however many times you pushed the button. Makes sense in my head. So, hope I'm tapping it out so you make sense of it too. And someone may have a better method.

Welcome to the forum.

Why are you using analog inputs rather than digital inputs?

.

DangerToMyself:
I think I would incorporate state change with a timer based on "Blink without delay". Set how long of a pause is necessary to tell the program you're done pushing the button. Then as it counts toward that time check for a state change. If there is one, reset the timer and add 1 to a variable such as howManyBlinks. If it gets to the set time, blink the light however many times you pushed the button. Makes sense in my head. So, hope I'm tapping it out so you make sense of it too. And someone may have a better method.

That makes sense to me i am just not sure how i would code that is the problem

Another method would be to use the pulseIn() function. With one button connected to a digital pin press and hold the button for about a second to indicate starting. Then press the button as you normally would (a fraction of second per press) for however many times you want. Press the button and hold down for one second again to signal input has ended.
pulseIn() returns the time in microseconds that the button was pressed. Use the difference in times to distinguish between start or stop and counting.

larryd:
Welcome to the forum.

Why are you using analog inputs rather than digital inputs?

.

There is not really a reason i just modified a differnt program and didnt change them to digital, should i change them to digital?

Due_unto:
Another method would be to use the pulseIn() function. With one button connected to a digital pin press and hold the button for about a second to indicate starting. Then press the button as you normally would (a fraction of second per press) for however many times you want. Press the button and hold down for one second again to signal input has ended.
pulseIn() returns the time in microseconds that the button was pressed. Use the difference in times to distinguish between start or stop and counting.

Thank you i will have to try that, and see if i can get it to work

D3STROYER:
That makes sense to me i am just not sure how i would code that is the problem

You could start here. Then have a go at this. They are both well commented. Once you have a grasp on them, make an attempt at combining those methods into your own sketch.

You might want to start with this simple counting sketch.
Set up the hardware as mentioned in lines 1,2,3
Upload it to the Arduino.
Start the serial monitor, cycle the switch a number of times.

What happens?

const byte heartBeatLED = 13;  //OutputPin---LEDanode---LEDcathode---220ohms---GND
const byte myLED        = 12;  //OutputPin---LEDanode---LEDcathode---220ohms---GND
const byte myButton     = 8;   //InputPin---SwitchPin1---SwitchPin2---GND

byte lastButton         = HIGH;
byte Counter            = 0;
byte currentButton;

unsigned int counter;

unsigned long currentMillis;
unsigned long heartBeatMillis;
unsigned long lastMillis;

const unsigned long heartBeatDelay = 250UL;
const unsigned long switchDelay    = 10UL;

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

  pinMode (heartBeatLED, OUTPUT);
  pinMode (myLED, OUTPUT);
  digitalWrite(myLED, HIGH);

  pinMode (myButton, INPUT_PULLUP);

} //END of      s e t u p ( )

//***************************************************************************
void loop()
{
  currentMillis = millis(); //for milli second timing

  //***************************
  //The ‘Heart Beat LED’, should toggle every heartBeatDelay milliseconds if code is nonblocking
  if (currentMillis - heartBeatMillis >= heartBeatDelay)
  {
    //reset timing
    heartBeatMillis = heartBeatMillis + heartBeatDelay;
    //Toggle heartBeatLED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //***************************
  //checkSwitches every switchDelay ms  (10ms)
  if (currentMillis - lastMillis >= switchDelay)
  {
    //reset timing
    lastMillis = lastMillis + switchDelay;

    checkSwitches();
  }

  //***************************
  //Other nonblocking code
  //***************************

} //END of       l o o p  ( )


//***************************************************************************
void checkSwitches()
{
  //***************************
  //Checks for no minimum press time, i.e. no noise filter

  currentButton = digitalRead(myButton);

  //Has there been a button change?
  if (lastButton != currentButton)
  {
    //Was the button pushed?
    if (currentButton == LOW) //Pushed makes the pin LOW
    {
      //do something
      counter++;
      Serial.println(counter);
      digitalWrite(myLED, !digitalRead(myLED));
    }

    //update the lastButton state for the next read
    lastButton = currentButton;
  }

  //***************************

} //END of   c h e c k S w i t c h e s ( )

//***************************************************************************

Give this simple sketch a try, the while() loop should be done with a millis() timer, but works as is. Connect your pushbutton between pin 4 and GND. Press the button as many times as you want, then pause 1.5 seconds.

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(9600);
  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;
}