Go Down

Topic: Delay without delay help (Read 1 time) previous topic - next topic

HazardsMind

By running in the background, I meant you don't need to devote everything to that one particular code. You can run other things and still monitor the condition. If the time is not, say 3 seconds, you can go to another part of the code, and continue to check the time. If the time is 3 seconds then, you run that particular line of code.

With delay(), your loop has to wait until the delay is done to go on, but without an actual delay, it can check other things until the condition is true.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

ichris93


The reason you have to hold the button, is because you don't have a Latch. Look at the state change detection code (I can't think of the actual name right now) it's in your example codes. Just implement that into this code and you will be good to go.


I implemented it in this way, but it doesn't work still.  If I repeatedly push the button it will not turn on until the timer has reached it time, but I have to continually push the bottom until the 6 seconds is up.

Code: [Select]

#include <Timer.h>
#include <Relay.h>
#include <Button.h>
#include <Bounce.h>

Button button1(5);
Button button2(6);
Button button3(7);
Relay contactor1(2, true);
Relay contactor2(3, true);
Relay contactor3(4, true);
#define BUTTON 13

int lastButtonState = 0; 
int buttonState = 0;// previous state of the button

Timer timer1; // Instantiate a Timer.
Timer timer2;
unsigned long elapsedTime = 3000; // Length of timer in milliseconds.
Bounce bouncer = Bounce( BUTTON,5 );
void setup() {
button1.begin();
  button2.begin();
  button3.begin();
  contactor1.begin();
  contactor2.begin();
  contactor3.begin();
  pinMode(BUTTON,INPUT);// Initialize the serial monitor.
}


void loop() {
  bouncer.update();
  int buttonState = bouncer.read();
  // read the pushbutton input pi

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
     if (buttonState == HIGH && button1.read() == HIGH && button2.read() == HIGH && button3.read() == HIGH){
    contactor1.on();
   if(timer1.timeDelay(3000)){
   contactor2.on();
   }
   if(timer2.timeDelay(6000)){
   contactor3.on();
  }
    }
     
    else {
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
  }

PaulS

Time to read, understand, and embrace the blink without delay example. You don't need timers, and you don't need to wait for the timer to expire to read switches again.

ichris93


Time to read, understand, and embrace the blink without delay example. You don't need timers, and you don't need to wait for the timer to expire to read switches again.


The timers are just the millis functions in a library.  I understand that I don't need to wait for the count to expire before it reads the switches again.  It is constantly reading the switches, but will only work when time has reached 3 or 6 seconds. 


Nick Gammon

Code: [Select]

 // compare the buttonState to its previous state
 if (buttonState != lastButtonState)
   {
   // if the state has changed, increment the counter
   if (buttonState == HIGH && button1.read() == HIGH && button2.read() == HIGH && button3.read() == HIGH)
     {
     contactor1.on();
     if(timer1.timeDelay(3000))
       contactor2.on();
     if(timer2.timeDelay(6000))
       contactor3.on();
     }
   }
 // save the current state as the last state,
 //for next time through the loop
 lastButtonState = buttonState;


I fixed up your indenting and got rid of the useless "else".

You are checking for buttonState to change, but ignoring the change if the time isn't up. Which is what you are reporting is happening.

Go Up