Delay without delay help

Hey guys. I have a pretty long sketch for a project I am working on. Currently it reads the status of one button and then when that button goes high it reads the status of three other switches to see what it needs to do. For example if switch 1 and 2 are high and the button is pushed it would turn on things for 1 and 2. Everything works, however, I have a delay built into it and I want to eliminate with Millis(); I have tried several things with it and haven't gotten it to work. It would work once, but I would have to hold down the button until my "interval" had passed. Here is part of my sketch

 bouncer.update ( );
 
 // Get the update value
 int value = bouncer.read();
 if ( value == HIGH) {
else  if (button1.read() == HIGH && button2.read() == HIGH && button3.read() == LOW){
     xport.PCF8574_SetPin(0, 0);
     xport.PCF8574_SetPin(1, 0);
     contactor1.on();
     xport.PCF8574_SetPin(2, 1);
     contactor3.off();
     delay(3000);
      contactor2.on();
  }
   }
else {
}
}

Thanks for any input!

You need some sort of state machine, or keeping track of elapsed time. Try reading this:

I have tried that liked this

else  if (button1.read() == HIGH && button2.read() == HIGH && button3.read() == LOW){
     xport.PCF8574_SetPin(0, 0);
     xport.PCF8574_SetPin(1, 0);
     contactor1.on();
     xport.PCF8574_SetPin(2, 1);
     contactor3.off();
     if ((millis() - onetimer) >= oneinterval){
      contactor2.on();
      onetimer = millis();
     }
  }

However, it only works one time and I have to hold the button down. The idea of the button is to be a set button that wouldn't have to be held down to get everything on.

snippets-r-us.com is down the internet a ways. You are more likely to get help with snippets there. Here, we expect to see ALL of your code.

PaulS:
snippets-r-us.com is down the internet a ways. You are more likely to get help with snippets there. Here, we expect to see ALL of your code.

Ask and you shall receive

#include <DAC6573.h>
#include <SoftwareSerial.h>
#include <PCF8574.h>
#include <Wire.h>
#include <CONVECTRON.h>
#include <LiquidCrystal_I2C.h>
#include <Relay.h>
#include <Button.h>
#include <Bounce.h>


CONVECTRON sensor(A0);
Button button1(5);
Button button2(6);
Button button3(7);
Relay contactor1(2, true);
Relay contactor2(3, true);
Relay contactor3(4, true);
#define BUTTON 13
#define RTS 8
#define CTS 9
LiquidCrystal_I2C lcd(0b0111000,20,4);
PCF8574 xport(0b0111111);
SoftwareSerial IGC(10, 11);
DAC6573 myDAC6573(DAC6573_DEFAULT_ADDRESS);
const unsigned long oneinterval=3000;
const unsigned long twointeveral=6000;

unsigned long onetimer;
unsigned long twotimer;

// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer = Bounce( BUTTON,5 ); 

void setup() {
  Serial.begin(9600);
  pinMode(RTS, INPUT);
  pinMode(CTS, OUTPUT);
  Wire.begin();
  button1.begin();
  button2.begin();
  button3.begin();
  contactor1.begin();
  contactor2.begin();
  contactor3.begin();
  pinMode(BUTTON,INPUT);
  IGC.begin(9600);
  onetimer = millis();
  twotimer = millis();
}

void loop() {
  
  myDAC6573.DAC6573_SetChannel(sensor.DAC() , 0);
  
 
  
  if (IGC.available()) {Serial.write(IGC.read());}
  if (Serial.available() && digitalRead(RTS) == LOW)
  {
    digitalWrite(CTS, HIGH);
    IGC.write(Serial.read());
    digitalWrite(CTS, LOW);
  }
  
  lcd.init();
  lcd.backlight();
   char buffer[10];
dtostre(sensor.readpressure(), buffer, 1, 0x04);
  lcd.setCursor(0,0);
  lcd.print("Funnel");
  lcd.setCursor(7,0);
  lcd.print(buffer);
  lcd.setCursor(15,0);
  lcd.print("Torr");
  
 // Update the debouncer
  bouncer.update ( );
 
 // Get the update value
 int value = bouncer.read();
 if ( value == HIGH) {
   if (button1.read() == LOW && button2.read() == LOW && button3.read() == LOW) {
   xport.PCF8574_SetPin(0, 1);
   contactor1.off();
   xport.PCF8574_SetPin(1, 1);
   xport.PCF8574_SetPin(2, 1);
   contactor2.off();
   contactor3.off();
   }
  else if (button1.read() == HIGH && button2.read() == LOW && button3.read() == LOW) {
   xport.PCF8574_SetPin(0, 0);
   contactor1.on();
   xport.PCF8574_SetPin(1, 1);
   xport.PCF8574_SetPin(2, 1);
   contactor2.off();
   contactor3.off();
 }

  else if (button2.read() == HIGH && button3.read() == LOW && button1.read() == LOW){
     xport.PCF8574_SetPin(1, 0);
     contactor2.on();
     xport.PCF8574_SetPin(0, 1);
     xport.PCF8574_SetPin(2, 1);
     contactor1.off();
     contactor3.off();
   }
  
  else if (button3.read() == HIGH && button1.read() == LOW && button2.read() == LOW){
     xport.PCF8574_SetPin(2, 0);
     contactor3.on();
     xport.PCF8574_SetPin(1, 1);
     xport.PCF8574_SetPin(0, 1);
     contactor2.off();
     contactor1.off();
   }
  
 else  if (button1.read() == HIGH && button2.read() == HIGH && button3.read() == LOW){
     xport.PCF8574_SetPin(0, 0);
     xport.PCF8574_SetPin(1, 0);
     contactor1.on();
     xport.PCF8574_SetPin(2, 1);
     contactor3.off();
     if ((millis() - onetimer) >= oneinterval){
      contactor2.on();
      onetimer = millis();
     }
  }
   
  else if (button1.read() == HIGH && button2.read() == HIGH && button3.read() == HIGH){
    xport.PCF8574_SetPin(0, 0);
    xport.PCF8574_SetPin(1, 0);
    xport.PCF8574_SetPin(2, 0);
    contactor1.on();
   delay(3000);
   contactor2.on();
  delay(3000); 
    contactor3.on();
 }
 }
 else {
 }
 }

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.

HazardsMind:
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.

The button will still work in the way I want it to with this feature?

Also, do you think this will help fix my problem with it only working one time?

Thank you.

If it works only one time, then it is because of your delay taken so long and not being able to constantly loop the program. Look at the example blink without delay, that example shows that you can run other things in the background and still blink an LED at 1 second intervals.

HazardsMind:
If it works only one time, then it is because of your delay taken so long and not being able to constantly loop the program. Look at the example blink without delay, that example shows that you can run other things in the background and still blink an LED at 1 second intervals.

I know I can run other things in the background without the delay. With the delay, it looped fine and I could do it repeatedly. It wasn't an issue until I switched it to without a delay that there was a problem. It would stagger startup one time but if I would turn it off then turn it back on the same way I turned it on the first time, it wouldn't stagger the startup. Does that make sense?

Does that make sense?

No, it doesn't. The Arduino doesn't have a background thread in which it can operate other code. As soon as you imply that it does, the rest of your statement, whatever it is, is false.

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.

HazardsMind:
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.

#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;
  }

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.

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.

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.

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

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.

Thank you for your help. I have it working now. Here is what I ended up with.

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

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.