I found this code @ Alternative Blink without Delay - Project Guidance - Arduino Forum
I like the fact it's easy for newbies like myself.
However I have ran into 3 issues I'm hoping someone with more experience and knowledge can help me resolve.
inputs are pullup
Here are my issues:
How can I make PB pushed_lightOn act like a toggle switch so I don't have to use extra output.
When Arduino boots LED flashes until I push pushed_lightOff
If I hapen to press pushed_lightOff while LED is let LED stays on without blinking.
I am very new at this with no programming experience (baby boomer) I did not post the whole sketch because it's quite involved. Has taken me a week and a half to get it to do what I want. And that's with lots of help from this forum. THANKS GUYS
}
if (pushed_lightOn) {
digitalWrite (holdLightLed, LOW);//output used to keep LED on beacuse of momentary push button
digitalWrite (lightRelay, HIGH);//relay to turn on light
}
if (pushed_lightOff) {
digitalWrite (holdLightLed, HIGH);////output used to keep LED on beacuse of momentary push button
digitalWrite (lightRelay, LOW);//relay to turn on light
digitalWrite (lightLed, LOW);
}
if (pushed_lightOn2) {
digitalWrite (lightLedBlink, (millis() % 1200 < 125) ? 5000 : 0 ); // flash LED at 2Hz
//lightLedBlink is the output to blinking light.
}
Hi!
If I understand, you have a input in INPUT_PULLUP mode with a PUSH BUTTON that switches to GND when pressed.
Then you have one LED that starts blinking when you turn on the Arduino.
You press one time the PUSH BUTTON, and it sets the flag pushed_lightOff.
Then, if the flag pushed_lightOff is set and you press the PUSH BUTTON, it is cleared and then the flag pushed_lightOn is set.
What's the pushed_lightOn2 flag?
What do you expect to happen and what actually is happening?
I'm sorry I forgot about pushed_lightOn2 as I said I have have pushed_lightOn trigger an input and and out a second output (lightLedBlink) because if I don't the LED goes off as soon as I release the button. pushed_lightOn2 is that input.
OK here is what I want:
push pushed_lightOn The l lightRelay closes and lightLedBlink blinks
Push pushed_lightOff the lightRelay opens and lightLedBlink turns off
Here is what happens:
push pushed_lightOn I have to send the button output (pushed_lightOn) to an input(pushed_lightOn2) and out another output (lightLedBlink) to blinking LED.
push pushed_lightOff if LED is in the high state of blink the LED remains on without blinking instead of turning off
as soon as power is applied or Arduino reset pushed LED flashes until I push pushed_lightOf
Is this what you needed? If not let me know
If you want the LED to go off and stay off when pushed_lightOn2 is LOW then try this:
if (pushed_lightOn2) {
digitalWrite (lightLedBlink, (millis() % 1200 < 125) ? 5000 : 0 ); // flash LED at 2Hz
} else digitalWrite (lightLedBlink, LOW);
Note:
digitalWrite (lightLedBlink, (millis() % 1200 < 125) ? 5000 : 0 ); // flash LED at 2Hz
This turns on the LED for the first 1/8th second (125 ms) of every 1.2 seconds (1200 ms). Not 2 Hz. Since the second argument to digitalWrite is a boolean value (true/HIGH or false/LOW) any non-zero value means "HIGH". The value 5000 is not 0 so it can be replaced by true or HIGH. That line can then be replaced by:
digitalWrite (lightLedBlink, (millis() % 1200 < 125) ? HIGH : LOW ); // flash LED at 0.833 Hz
And since the HIGH is chosen when (millis() % 1200 < 125) is 'true' and LOW is chosen when (millis() % 1200 < 125) is 'false' and true==HIGH and false==LOW it can be simplified more:
digitalWrite (lightLedBlink, millis() % 1200 < 125 ); // flash LED at 0.833 Hz
Why
So, If I understand what you want and with the correction from johnwasser, the code below do what you want:
Note: I'm considerating the variables pushed_lightOn, pushed_lightOff, pushed_lightOn2 are of bool type (or boolean) and are initialized as false.
if (pushed_lightOn) {
digitalWrite (holdLightLed, LOW);//output used to keep LED on beacuse of momentary push button
digitalWrite (lightRelay, HIGH);//relay to turn on light
pushed_lightOff = false;
if (pushed_lightOn2) {
digitalWrite (lightLedBlink, millis() % 1200 < 125 ); // flash LED at 0.833 Hz
//lightLedBlink is the output to blinking light.
}
}
else if (pushed_lightOff) {
digitalWrite (holdLightLed, HIGH);////output used to keep LED on beacuse of momentary push button
digitalWrite (lightRelay, LOW);//relay to turn on light
digitalWrite (lightLed, LOW);
pushed_lightOn = false;
if (pushed_lightOn2) {
digitalWrite (lightLedBlink, true); // Turn on LED
}
}
Yes bool type (or boolean) and are initialized as false. Thanks, guys I'll look at these in the morning.