Counting pulses and using them

Thank you for reading!

Hi I'm trying to make a pulse counter and with those counted pulses, make a led blinking the same number of times

I'm new in Arduino, this will be a gift for my little daughter, please if you guys could help me

my idea start from this example

// this constant won't change:
const int  buttonPin = 50;    // the pin that the pushbutton is attached to
const int ledPin = 22;       // 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
int Contador = 0;

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;

with this code, i have the number of pulses, but now, how can i use that data to make blink a LED

How do you want it to blink relative to the pulse count? One per pulse? Like a heart rate monitor?

if you want simply to toggle with each press of the button, place

digitalWrite(ledPin,!(digitalRead(ledPin)));

into your code as you detect a button push or a change of buttonState.

Your sketch however, seems only to increment the button counter when the state changes from low to high. Id that what you intended?

my idea start from this example

The example is a good place to start.

The buttonPushCounter variable holds the number of button presses. You need a way to trigger the LED flashing such as another button input. When the start button is pressed you can call a function flash the LED. If the Arduino is not required to do anything else this will be easy. Are you familiar with for loops ?

DKWatson: How do you want it to blink relative to the pulse count? One per pulse? Like a heart rate monitor?

Hi Watson, thank you for reply

for example if i press 5 times the button, 5 times blink the led

UKHeliBob:
The example is a good place to start.

The buttonPushCounter variable holds the number of button presses. You need a way to trigger the LED flashing such as another button input. When the start button is pressed you can call a function flash the LED. If the Arduino is not required to do anything else this will be easy. Are you familiar with for loops ?

Not, arduino not going to do anything else, i’m not a pro but i know about loops…can you give me a hand ? =D

Then you need to specify: once the button has been detected as having been ‘pushed’ you switch the LED on and for how long do you want it to remain on before turning off again. FYI 30ms is generally regarded as the minimum length of time required for the human eye/mind to really register a ‘blink’. Less than that and I guess we’re not really sure.

DKWatson: Then you need to specify: once the button has been detected as having been 'pushed' you switch the LED on and for how long do you want it to remain on before turning off again. FYI 30ms is generally regarded as the minimum length of time required for the human eye/mind to really register a 'blink'. Less than that and I guess we're not really sure.

I need that while I give the pulses, just record the amount of pulses, then in another part of the sketch that blinks the number of times that pulses

alfguardia: I need that while I give the pulses, just record the amount of pulses, then in another part of the sketch that blinks the number of times that pulses

Huh?

sorry for my english =´D

I wanted to say, I need to store the counted pulses and then the LED will blinking the number of times that i pressed

What action is going to trigger the start of the LEDs blinking ?

could be , time, like 5 seconds to count pulses and save the number of pulses and just blink the LED same number times

Or you could wait until the button hasn't been pressed for maybe 3 or 5 seconds and then store the count and start blinking.

But you have to decide. A lot of the skill in programming is working out in detail what you want to happen and when.

Steve

slipstick: Or you could wait until the button hasn't been pressed for maybe 3 or 5 seconds and then store the count and start blinking.

But you have to decide. A lot of the skill in programming is working out in detail what you want to happen and when.

Steve

I was thinking in this, i press the button 10 times...then i wait for 5 seconds and the LED has to blink for 10 times, it makes sense?

You can set the timer to start counting after every button is pushed. If the timer runs out before the next button push, flash the LED, reset the counter and wait for the next button push.

If the countdown after a button push is interrupted by a new button push, increment your counter and reset the timer.

Try this sketch with a pushbutton between GND and pin 4, press the button x number of times, if you pause for 1.5 seconds, the onboard led will flash as many times as you pressed.

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;
}

Thank you Outsider!, i will try and i will let you know!

outsider: Try this sketch with a pushbutton between GND and pin 4, press the button x number of times, if you pause for 1.5 seconds, the onboard led will flash as many times as you pressed.

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; }


outsider:
Try this sketch with a pushbutton between GND and pin 4, press the button x number of times, if you pause for 1.5 seconds, the onboard led will flash as many times as you pressed.


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; }

Hi, outsider!...i tried this sketch and works perfectly...i was wondering if this sketch works to count fast? and if so, how can i modify it?

Thank you my friend