Go Down

Topic: reading pushbutton. only if HIGH for 5 sec or more, start next routine (Read 778 times) previous topic - next topic

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: [Select]

#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: [Select]

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: [Select]

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();
    }
  }
}

CrossRoads

Why not try it blink without delay style? Sample 10 times a second, do other stuff while waiting
Code: [Select]

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
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

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 !

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

flyboy

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy