Pages: [1]   Go Down
Author Topic: reading pushbutton. only if HIGH for 5 sec or more, start next routine  (Read 675 times)
0 Members and 1 Guest are viewing this topic.
Belgium
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
with the first part of this softwareprogram I use a pushbutton (will later on be replaced by a PIRsensor) as trigger to start the main routine called Rumple. The goal is that: if button is not pushed, nothing happens + if button is pushed quickly, nothing happens + if button is pushed several times quickly, nothing happens + if button is pushed for 5sec or longer, the main routine starts. By adding the command serial.println I could find out that all possibilities work, except when button is pushed for 5sec or longer. It then seems to recognize the long push as fragmented, thus similar to a few short pushes. I tried 3 different ways of programming to get the long push recognised as such, even combining the bouncing command into the pulseIn software (not added as not it was not working either); none was working.
As I presume the problem will remain when later on I replace the pushbutton with a PIRsensor, I really would like to find a solution.
extra info: working with an arduino micro on a breadboard.
thanks in advance !

Code A: debouncing
Code:
#include <Bounce.h>

const int pir = 2;                         // the number of the PIRsensor pin
const int PirDelay = 10000;          // 5sec someone in view of pirsensor.
long StartTijd;                           // starttime
Bounce Sensor = Bounce(pir, PirDelay);
int val              = LOW;               // variable for reading the pin status

void setup() {
  pinMode(pir, INPUT);            // initialize the PIRsensor pin as an input:
  Serial.begin(9600);             // open the serial port at 9600 bps
}

void loop(){
  Sensor.update();                   // check if the pirsensor is triggered = HIGH:
  val = Sensor.read();               // read input value
  if (val == HIGH) {                  // start check time 
    Bounce(pir, PirDelay);
  // test per onderdeel. read the input pin
  //digitalRead(pir);
  // print out the state of the button:
  //Serial.println("bounce sensor");
    {
      Rumple();
    }
  }
  else {
    Sensor.update();
  }
}

Code B: pulseIn
Code:
const int pir = 2;                             // the number of the PIRsensor pin
const int TriggerTijd = 10000000;    // 10sec (in MICROseconds!!!!!)
long StartTijd;                               // starttime
unsigned long TriggerSignal;           // measure time input=high
int val              = LOW;                  // variable for reading the pin status

void setup() {
  pinMode(pir, INPUT);                  // initialize the PIRsensor pin as an input:
  Serial.begin(9600);                     // open the serial port at 9600 bps
}


void loop() {
  if (digitalRead(pir) == LOW) {
  }                                                 // do nothing
  if (digitalRead(pir) == HIGH) {
    TriggerSignal = pulseIn(pir, HIGH);
      // test. read the input pin
      //digitalRead(TriggerSignal);     
      // test. print out the state of the button:     
      //Serial.println(TriggerSignal); 
    if (TriggerSignal > TriggerTijd);               
    {                                               // start void Rumple
      // test. read the input pin     
      //digitalRead(pir);     
      // test. print out the state of the button     
      //Serial.println("sensor >10sec OK"); 
      Rumple();
    }
  }
}

Code C: pulseIn, but different programming
Code:
const int pir = 2;                            // the number of the PIRsensor pin
const int TriggerTijd = 5000000;     // 5sec (in MICROseconds!!!!!)
long StartTijd;                              // starttijd = bij elk nieuw commando gekoppeld aan actuele interne timer
unsigned long TriggerSignal;          // measure time input=high
int val              = LOW;                 // variable for reading the pin status

void setup() {
  pinMode(pir, INPUT);                 // initialize the PIRsensor pin as an input:
  Serial.begin(9600);                      // open the serial port at 9600 bps
}

void loop() {
  if (digitalRead(pir) == LOW)
  {                                               // do nothing
    // test. read the input pin
    //digitalRead(pir);     
    // test. print out the state of the button:     
    //Serial.println("nothing yet"); 
  }                                                 
  if (digitalRead(pir) == HIGH)
  {
    TriggerSignal = pulseIn(pir, HIGH);
    if (pulseIn(pir, HIGH) < TriggerSignal) {            //do nothing
      // test. read the input pin
      //digitalRead(TriggerSignal);     
      // test. print out the state of the button:     
      //Serial.println("too small to start rumple");   
    }
    if (pulseIn(pir, HIGH) >= TriggerSignal) {           // start void Rumple
      // test. read the input pin     
      //digitalRead(pir);     
      // test. print out the state of the button     
      //Serial.println("sensor >5sec OK"); 
      Rumple();
    }
  }
}
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26956
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not try it blink without delay style? Sample 10 times a second, do other stuff while waiting
Code:
void loop(){
if  ((digitalRead(pir) == 1) && (startFlag == 0) ){ // start new time sequence
   startFlag = 1;
   startTime = millis();
   previousTime = startTime;
}
if ( (startFlag ==1) && ((millis() - previousTime) >= duration) ) {  // duration = 100, sample 10 times a second
   previousTime = previousTime + duration;
   if ((millis() - startTime )<=5000){ // still waiting it out
    if (digitalRead(pir) == 0){
       // pin went low
       startFlag = 0;
       }
    }
   if ((millis() - startTime) >5000){
       if (digitalRead(pir) == 0){
       // pin went low
       startFlag = 0;
       }
      else{  // made it!
      rumple = 1;
      startFlag = 0;
      }
   }
// do other stuff while time is passing
}
Time elements are all unsigned long
everything else can be byte
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Belgium
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you CrossRoads for the quick reply ! I applied it this morning. It works perfect.
Think I was making it more complicated then needed. Thank You !
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26956
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool. Not bad for making it up on the fly, eh!
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Central Indiana, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 203
So many projects, so little time...and money!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did something similar to this once.  I had a routine that would check to see if a button is being held.  If it was, a counter would decrement during each cycle of the loop.  Once the counter reached zero, the button behavior was different.

I wanted to do what you see with a lot of clocks.  When you push a button and hold it, it counts slowly at first, then very quickly after the button has been held for a few seconds.  Worked great.
Logged

Pages: [1]   Go Up
Jump to: