Pages: 1 [2]   Go Down
Author Topic: Delay without delay help  (Read 1434 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



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. 
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4773
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: 1 [2]   Go Up
Jump to: