Go Down

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

ichris93




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.


Is there a way I can not ignore the change if the time isn't up so that it will change from off to on after the time has elapsed? I appreciate your help. 

Rework it. Make a flowchart (just a simple one) of what you expect to happen when.

You have a couple of things:


  • Button press

  • Time elapsed



If you don't want one dependent on the other, don't put the test for time under the test for the button press.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

ichris93


Rework it. Make a flowchart (just a simple one) of what you expect to happen when.

You have a couple of things:


  • Button press

  • Time elapsed



If you don't want one dependent on the other, don't put the test for time under the test for the button press.


Thank you for your help.  I have it working now.  Here is what I ended up with. 
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;
boolean pthree = 0;

Timer timer1;
Timer timer2;

Bounce bouncer = Bounce( BUTTON,5 );

void setup() {
  button1.begin();
  button2.begin();
  button3.begin();
  contactor1.begin();
  contactor2.begin();
  contactor3.begin();
  pinMode(BUTTON,INPUT);
}

void loop() {
  if (bouncer.update()){
    if (bouncer.read() == HIGH){
      if (button1.read() == HIGH && button2.read() == HIGH && button3.read() == HIGH)
      {
       contactor1.on();
       pthree = 1;
       timer1.resetTimer();
       timer2.resetTimer();
      }
     }
  }
if (pthree == 1){
    if(timer1.timeDelay(3000)){
        contactor2.on();}
    if(timer2.timeDelay(6000)){
        contactor3.on();}
  }
}

GoForSmoke

I dunno what the delay is for.

Simplest thing I can come up with is to keep track of all the switches and buttons (including debouncing which may involve time or just counting consecutive positives) every time through loop() and whenever the button has been for sure pushed use the current switch state data to control your code branches.

All your I/O collected separately, always collected no matter what (not only when the button is pressed) and in separate blocks of code is your logic for decision and do code, including debounce for the switches and button. Then you won't have parts of the process tangled up and hanging up in the branch and level logic of other parts of the process.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up