Pages: [1] 2   Go Down
Author Topic: Delay without delay help  (Read 1569 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

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

Code:
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!
« Last Edit: February 27, 2013, 04:00:43 pm by ichris93 » Logged

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

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

http://www.gammon.com.au/blink
Logged


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

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

http://www.gammon.com.au/blink


I have tried that liked this
Code:
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. 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Code:
#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 {
 }
 }
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3632
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

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

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

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3632
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

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

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?

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3632
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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. 

Logged

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

Code:
 // 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.
Logged


Pages: [1] 2   Go Up
Jump to: