Go Down

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

ichris93

Jan 25, 2013, 11:32 pm Last Edit: Feb 27, 2013, 10:00 pm by ichris93 Reason: 1
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: [Select]

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!

Nick Gammon

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

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

ichris93


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: [Select]

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. 

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.

ichris93


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: [Select]

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

Go Up