Offline
Newbie
Karma: 0
Posts: 46
|
 |
« on: January 25, 2013, 05:32:57 pm » |
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!
|
|
|
|
« Last Edit: February 27, 2013, 04:00:43 pm by ichris93 »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #1 on: January 25, 2013, 05:41:02 pm » |
You need some sort of state machine, or keeping track of elapsed time. Try reading this: http://www.gammon.com.au/blink
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #2 on: January 25, 2013, 05:56:52 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #3 on: January 25, 2013, 06:11:31 pm » |
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
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #4 on: January 25, 2013, 06:23:35 pm » |
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 { } }
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 28
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #5 on: January 25, 2013, 06:28:57 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #6 on: January 26, 2013, 11:54:48 pm » |
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
Edison Member
Karma: 28
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #7 on: January 27, 2013, 12:15:44 am » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #8 on: January 27, 2013, 12:58:57 pm » |
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
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #9 on: January 27, 2013, 01:42:29 pm » |
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
Edison Member
Karma: 28
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #10 on: January 27, 2013, 02:03:41 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #11 on: February 20, 2013, 03:52:34 pm » |
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; }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #12 on: February 20, 2013, 04:56:16 pm » |
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
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #13 on: February 22, 2013, 12:15:57 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #14 on: February 22, 2013, 03:56:05 pm » |
// 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
|
|
|
|
|
|