Go Down

Topic: check frequency by pressing button (Read 232 times) previous topic - next topic

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.

robtillaart

something like this
Code: [Select]

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');
}
Rob Tillaart

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

Go Up