Hi everyone,
I'm trying to figure this out but am starting to get frustrated. I'm making a control panel for an led driver using the dim function and a transistor and an arduino nano. Basically I have one output pin controlled by three switches. The first is a momentary button that should turn the light on (put the output pin in LOW) for a desired amount of time eg 55000 ms. The second is a momentary switch with a different preset timer, and the third is an on/off switch used to manually control the light. I got the thing working great with delay but of course I need to also be able to cancel the operation and turn it off with any button press, and delay won't allow for that, so I've been trying to examine and edit BlinkWithoutDelay and use millis to create the timers, but I'm stuck for a few days with that. I deleted most of what I did and started over about a dozen times and have googled enough to be crosseyed so I'm hoping someone here is kind enough to point me in the right direction. Here's my code:
const int buttonA = 5;
const int buttonB = 4;
const int buttonC = 3;
const int luxOut = LED_BUILTIN;
const long intervalA = 5000;
const long intervalB = 2000;
int buttonStateA = 1;
int buttonStateB = 1;
int buttonStateC = 1;
int ledState = 0;
static unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
void setup() {
// initialize the LED pin as an output:
pinMode(luxOut, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(buttonC, INPUT_PULLUP);
}
void loop() {
digitalWrite(luxOut, HIGH);
buttonStateA = digitalRead(buttonA);
buttonStateB = digitalRead(buttonB);
buttonStateC = digitalRead(buttonC);
ledState = digitalRead(luxOut);
if (buttonStateA == LOW) {
previousMillis = currentMillis;
digitalWrite(luxOut, LOW);
}
if (currentMillis - previousMillis >= intervalA) // this stops the timer
{
digitalWrite (luxOut, HIGH);
}
if (buttonStateB == LOW) {
previousMillis = currentMillis;
digitalWrite(luxOut, LOW);
}
if (currentMillis - previousMillis >= intervalB) // this stops the timer
{
digitalWrite (luxOut, HIGH);
}
if (buttonStateC == LOW) {
digitalWrite(luxOut, LOW);
delay(10);
} else {
digitalWrite(luxOut, HIGH);
}
}
My problem is the code stopping the timers don't seem to know which button was pressed to start with, so how do I make this work? Any help is greatly appreciated.
That part works fine. It's my on/off switch. There's no need to interrupt that part because as soon as the switch goes high the light turns off. My issue is the momentary timers
Here is my code which works like this. when you press a bottun you need to unpress it before delay(pauza) is over. If you continue to hold it it will automaticly go to last if statment.
I declared pauza as int pauza=500;
with this code you can trigger some function and stop it when you want with same bottun.
Scan your switches ≈50ms.
Look for a change in state in your switch.
If there is a change, initialize a timer, set a variable or cancel an operation as needed.
Repeat for remaining switches.
In loop(), examine timers to see if they are enabled and finished, operate on things accordingly.
Depends on application of the code. This delay will happen only when you press a bottun and it will start doing something and scanning switches just after 500 ms.
larryd, that was awesome, thank you! The code you posted works for the timers and buttons pretty much perfectly, but the thing I need to add now is the ability to cancel any of the timers (turn off the light or interrupt the timed lit period) by pressing any button. Should I start a new void section or try to add a new button function to the existing code? I know it's a stupid question to you but I don't want to break your awesome sketch. Thanks again!!
Oh believe me I'm breaking it. Hints? I'm learning a lot reading what you did, and I really appreciate the help. If I figure it out I'll post here. Thanks