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?
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.
other, i want to try for multiple buttons, in 5 sec, how many button pressed (the timer should started from the first button pressed)
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 initializes a TimedAction class that will change the state of an LED every second.
TimedAction resetAction = TimedAction(5000,resetButtonCount);
TimedAction updateButtonAction = TimedAction(20,updateButtons);
void loop(){
resetAction.check();
updateButtonAction.check();
(pressed<5) ? led.on() : led.off(); //if a button has been pressed over 5 times, turn led off
}
//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;
}
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.
//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);
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”);
}
//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;
}
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
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]
//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);
}
//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]
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.
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.
//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);