Pages: [1]   Go Down
Author Topic: check frequency by pressing button  (Read 215 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello people
I wish that when a button was pressed in a 3-second interval the program to continue to do what he's doing, if he were not in a tight range of 3 seconds it does something else.

Pressed once, after first checking if it was pressed again at an interval of 3 seconds, doing this until he ceases to be pressed in a 3-second interval.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13894
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

something like this
Code:
int f = 1;  // f = function to execute
void setup()
{
  Serial.begin(9600);
  unsigned long start = millis();
  f = 1;
}

void loop()
{
  // CHECK BUTTON
  if (digitalRead(button) == HIGH && millis() - start < 3000UL)
  {
    start = millis();  // fast enough => restart timeout
    f = 1;
  }
  else
  {
    f = 2; // too late :(
  }
 
  if (f == 1) f1();
  else f2();
}

void f1()
{
  Serial.print('1');
}

void f2()
{
  Serial.print('2');
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: