I need to modify program where output (led) gets HIGH if input gets HIGH and stays high until reactivated (input gets high again), but can't become reactivated (LOW) first 3 seconds, now is there a way to make it also stay low for 3 seconds, so it can't become high again until 3 seconds passed. I did it with delay, but i need to do it with millis function and that i can't do. For now i only have program where led can't become low in first 3 seconds but i need to make it so it also can't be high for 3 seconds (after deactivation). If you have idea how to use millis to do it i would really appreciate it!
Elaz1234:
I need to modify program where output (led) gets HIGH if input gets HIGH and stays high until reactivated (input gets high again), but can't become reactivated (LOW) first 3 seconds, now is there a way to make it also stay low for 3 seconds, so it can't become high again until 3 seconds passed.
Tell me if I have interpreted your requirement correctly (or correct me if I am wrong)
You want to make a LED turn on when an I/O pin changes from LOW to HIGH
You want it stay on until the I/O pin again goes LOW and changes to HIGH in which case the LED should turn off
However when the LED is on you want it to stay on for at least 3 seconds no matter what happens to the I/O pin.
And when the LED is off you want it to stay off for at least 3 seconds.
One way to do that is to save the value of millis() when the LED changes (either on or off) and don't read the I/O pin until 3 seconds have elapsed. Something like this
if (ledState != prevLedState) {
ledChangeTime = millis();
}
if (millis() - ledChangeTime > 3000) {
prevInputState = inputState;
inputState = digitalRead(inputPin);
if (inputState != prevInputState and inputState == HIGH) {
prevLedState = ledState;
ledState = not ledState;
digitalWrite(ledPin, ledState);
}
}
Robin2:
Tell me if I have interpreted your requirement correctly (or correct me if I am wrong)
You want to make a LED turn on when an I/O pin changes from LOW to HIGH
You want it stay on until the I/O pin again goes LOW and changes to HIGH in which case the LED should turn off
However when the LED is on you want it to stay on for at least 3 seconds no matter what happens to the I/O pin.
And when the LED is off you want it to stay off for at least 3 seconds.
One way to do that is to save the value of millis() when the LED changes (either on or off) and don't read the I/O pin until 3 seconds have elapsed. Something like this
if (ledState != prevLedState) {
ledChangeTime = millis();
}
if (millis() - ledChangeTime > 3000) {
prevInputState = inputState;
inputState = digitalRead(inputPin);
if (inputState != prevInputState and inputState == HIGH) {
prevLedState = ledState;
ledState = not ledState;
digitalWrite(ledPin, ledState);
}
}
...R
so i tried to do it like this but led is just high
int led1 = 6;
int inputPin = 3;
int ledState = 0;
int prevLedState = 0;
int inputState = 0;
int prevInputState =0;
unsigned long ledChangeTime;
void setup() {
}
void loop() {
if (ledState != prevLedState) {
ledChangeTime = millis();
}
if (millis() - ledChangeTime > 3000) {
prevInputState = inputState;
inputState = digitalRead(inputPin);
if (inputState != prevInputState and inputState == HIGH) {
prevLedState = ledState;
ledState = not ledState;
digitalWrite(led1, ledState);
}
}
Could also use an attachinterrupt library, which means you could set the interrupt to falling, rising or change, which can make your code a lot simpler.
GreyArea:
I use do{} while() loops for this kind of thing..
Could also use an attachinterrupt library, which means you could set the interrupt to falling, rising or change, which can make your code a lot simpler.
IMHO neither is appropriate.
WHILE is a blocking function and generally prevents the Arduino from doing other things. It can work with simple programs but when the code gets more complex it just gets in the way except for something that completes in at most a few millisecs.
Interrupts are only needed to detect a state that lasts for a short space of time or something that repeats at very short intervals.
My inexperience showing. It sounds like I've fallen for the lure of interrupts because they can counter my (over) use of do..while.
So you suggest better not to use do..while in the first place?
Edit: trouble is I think they suit what I do, which is basically suites of mathematically dependant led animations, usually on a countdown timer of some sort.
Hey, at least I stopped using delay()... Cos the do_while loop can at least accept a button input while it's running...
You're also missing the "pin mode" in your setup for both the LED output and push button input.
Serial print messages within your code would prove to be useful to understand the state of everything.
Perhaps a simpler code would be to check that you can read the button and output a serial message that shows the state of the button before you try further things? Arduino projects are like lego, it's much easier to start from the start and build up to a more complex final design.
int led1 = 6; // This should be 'const byte"
int button1 = 3; // This should be "const byte"
int buttonState = 0;. // This doesn't need global scope
int lastButtonState = 0; // This doesn't need explicit initalisation, but should be initialised in setup with a digitalRead