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 {
}
}
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.
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?
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?
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.
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.