timer

hi,

how to set a specific timer with specific feedback? i want to use a button, for example how many times the button pressed in 5 sec. if in 5 sec the button pressed less than 5 times led light is on, if more than 5 times led light is blinking. how to use the coding in arduino for this issue?

thanks

There are a few things you need to decide on:

  • Are your program going to do anything else?
  • How do you want to divide the timeline?

  • Divide timeline evenly in 5 seconds

  • Start from detected press, and count presses the next 5 seconds

Every detail about your specific constraints/ideas will help. :)

hi,

Actually i want to try two different programs.

  1. I want use button for every 5 sec, it record how many times button is pressed, for example for every 5 sec if button is pressed less than 5 led light is on.

  2. other, i want to try for multiple buttons, in 5 sec, how many button pressed (the timer should started from the first button pressed)

thanks

  1. I want use button for every 5 sec, it record how many times button is pressed, for example for every 5 sec if button is pressed less than 5 led light is on.

This is one possible solution for you. :slight_smile:

It uses some classes from my Playground Page

#include <TimedAction.h> //http://www.arduino.cc/playground/uploads/Code/TimedAction.zip
#include <Button.h> //http://www.arduino.cc/playground/uploads/Code/Button.zip
#include <LED.h> //http://www.arduino.cc/playground/uploads/Code/LED.zip

//this initializes a TimedAction class that will change the state of an LED every second.
TimedAction resetAction = TimedAction(5000,resetButtonCount);
TimedAction updateButtonAction = TimedAction(20,updateButtons);

Button countButton = Button(12,PULLUP);
Button startCountButton = Button(11,PULLUP);

LED led = LED(13);

byte pressed = 0;

void setup(){/nothing to setup/}

void loop(){
resetAction.check();
updateButtonAction.check();
(pressed<5) ? led.on() : led.off(); //if a button has been pressed over 5 times, turn led off
}

//reset counter
void resetButtonCount(){
pressed = 0;
}

//update buttons and increase pressed if suitable
void updateButtons(){
//if the button is pressed AND the state has changed, increase counter
countButton.uniquePress() ? pressed++ : pressed;
}

hi,

if i just want to use one button, so every 5 sec it counted how many times that button is pressed. i did like below, but seems like i press the button on time and the light on for 5 sec.

#include <TimedAction.h> //http://www.arduino.cc/playground/uploads/Code/TimedAction.zip
#include <Button.h> //http://www.arduino.cc/playground/uploads/Code/Button.zip
//#include <LED.h> //http://www.arduino.cc/playground/uploads/Code/LED.zip

//this initializes a TimedAction class that will change the state of an LED every second.
TimedAction resetAction = TimedAction(5000,resetButtonCount);
TimedAction updateButtonAction = TimedAction(20,updateButtons);

Button countButton = Button(12,PULLUP);
//Button startCountButton = Button(11,PULLUP);

//LED led = LED(13);

int pressed = 0;

void setup(){
pinMode(13,OUTPUT);
Serial.begin(9600);

}

void loop(){
resetAction.check();
updateButtonAction.check();
//(pressed<5) ? led.on() : led.off(); //if a button has been pressed over 5 times, turn led off

if (pressed > 5){
//digitalWrite(13,HIGH);
Serial.println(“on”);
}else{
//digitalWrite(13,LOW);
Serial.println(“off”);
}

}

//reset counter
void resetButtonCount(){
pressed = 0;
}

//update buttons and increase pressed if suitable
void updateButtons(){
//if the button is pressed AND the state has changed, increase counter
countButton.isPressed() ? pressed++ : pressed;
}

You may want to do something like the following:

int countButton  = 12;

void setup()
{
  pinMode(13, OUTPUT); 
  pinMode(countButton,INPUT); 
  digitalWrite(countButton,HIGH); //turn on pull-up resistor
}

void loop()
{
    if( switchTime() > 5000)
       digitalWrite(13,HIGH); 
    else   
       digitalWrite(13,LOW);
}


// return the time in milliseconds that the switch has been in pressed (LOW)
long switchTime()
{
  static unsigned long startTime = 0;         // the time the switch state change was first detected
  static boolean state;              // the current state of the switch

  if(digitalRead(countButton) != state) // check to see if the switch has changed state
  {
    state = ! state;       //yes,  invert the state
    startTime = millis();  // store the time
  }
  if( state == LOW)
    return millis() - startTime;   // return the time in milliseconds   
  else
    return 0; // return 0 if the switch is not pushed (in the LOW state);   
}

switchTime is a function that returns the number of milliseconds a button has been pushed

  1. I want use button for every 5 sec, it record how many times button is pressed, for example for every 5 sec if button is pressed less than 5 led light is on.

This is what my sketch does.

If the button is pressed more than five times, inside an interval of 5 seconds. Turn an LED off, otherwise turn if on.

This sketch prints to console the number of presses made the previous 5 seconds:
[TESTED CODE]

#include <TimedAction.h> //http://www.arduino.cc/playground/uploads/Code/TimedAction.zip
#include <Button.h> //http://www.arduino.cc/playground/uploads/Code/Button.zip
#include <LED.h> //http://www.arduino.cc/playground/uploads/Code/LED.zip

//this initializes a TimedAction class that will change the state of an LED every second.
TimedAction resetAction = TimedAction(5000,resetButtonCount);
TimedAction updateButtonAction = TimedAction(20,updateButtons);

Button countButton = Button(12,PULLUP);

LED led = LED(13);

byte pressed = 0;

void setup(){/nothing to setup/}

void loop(){
resetAction.check();
updateButtonAction.check();
(pressed<5) ? led.on() : led.off(); //if a button has been pressed over 5 times, turn led off
Serial.begin(9800);
}

//reset counter
void resetButtonCount(){
Serial.print(“You pressed the button “);
Serial.print(pressed);
Serial.println(” times”);
pressed = 0;
}

//update buttons and increase pressed if suitable
void updateButtons(){
//if the button is pressed AND the state has changed, increase counter
countButton.uniquePress() ? pressed++ : pressed;
}

[edit]

if i just want to use one button, so every 5 sec it counted how many times that button is pressed. i did like below, but seems like i press the button on time and the light on for 5 sec.

 countButton.isPressed() ? pressed++ : pressed;

This code will execute so often that a press will be registered as many.
Be sure to download the new: Button Library.

 countButton.uniquePress() ? pressed++ : pressed;

It might be that mem’s sketch does what you want. [The above post.]
[/edit]

hi,

seems like i still don't get the latest button library. it showed error, error: 'class Button' has no member named 'uniquePress'. i had downloaded from the link you gave but the word "uniquePress" written in black color. i tried by add 'uniquePress KEYWORD2' in 'keyword' text document, it can change the color to brown, but the error still the same, i think need to modify in c++ file. can u show me the latest library? thanks.

The zip contains the latest library.

Both the .h and the .cpp is correct.

Be sure to delete the library, then download the new one. And then recompile the sketch. :)

[edit]LED.h LED.cpp keywords.txt[/edit]

Hi,

Thanks, i already can use that, but now i am trying another function. i have tried below code but seem like something wrong. i want to use 2 buttons, first button to start the 5 sec function, if it pressed it should execute 5 sec function, and in such 5 sec it record how many times second button is pressed.

thanks

#include <TimedAction.h> //http://www.arduino.cc/playground/uploads/Code/TimedAction.zip
#include <Button.h> //http://www.arduino.cc/playground/uploads/Code/Button.zip
//#include <LED.h> //http://www.arduino.cc/playground/uploads/Code/LED.zip

//this initializes a TimedAction class that will change the state of an LED every second.
TimedAction updateButtonAction = TimedAction(1000,updateButtons);
TimedAction resetAction = TimedAction(5000,resetButtonCount);
TimedAction updateButtonAction1 = TimedAction(20,updateButtons1);

Button countButton = Button (11,PULLUP);
Button countButton1 = Button(12,PULLUP);

int pressed = 0;

void setup(){

Serial.begin(9600);
}

void loop(){
updateButtonAction.check();
}

//if the button 11 is pressed execute timer 5 sec
void updateButtons(){
if (countButton.uniquePress()){
resetAction.check();
Serial.println(“check”);
} else{
Serial.println(“nothing”);
}
}

//in 5 sec call another button
void resetButtonCount(){

updateButtonAction1.check();

Serial.print(“You pressed the button “);
Serial.print(pressed);
Serial.println(” times”);

}

//update buttons and increase pressed if suitable
void updateButtons1(){
countButton1.uniquePress() ? pressed++ : pressed;

}