Hey Guys,
Spent a couple weeks on this code already. New to arduino but have basic programming understanding. Issues I am having is that I have to push the on/off button twice for the led to start working only the first time. Second issue is when I want to turn off buttonC, it will leave the LED on or off depending on where it happens to stop in the loop. I need to be able to make sure the LED always turns off but doesn't affect any of the other buttons. I am manipulating 1 LED with multiple buttons.
const int ledPin1 = 12;
const int buttonA = 9;
const int buttonB = 10;
const int buttonC = 11;
int state = LOW;
int flashStateA = LOW;
int flashStateB = LOW;
int flashStateC = LOW;
int lastFlashStateA = LOW;
int lastFlashStateB = LOW;
int flashLedC = LOW;
int flashLedA = LOW;
const int off = 250;
const int on = 500;
long previousMillis = 0;
long interval = 50;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
pinMode(buttonC, INPUT);
}
void loop() {
unsigned long currentMillis = millis();
if(digitalRead(buttonC) == HIGH){
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (flashLedC == LOW)
flashLedC = HIGH;
else
flashLedC = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, flashLedC);
}
}
flashStateA = digitalRead(buttonA);
if (flashStateA != lastFlashStateA)
{
onOff();
lastFlashStateA = flashStateA;
}
flashStateB = digitalRead(buttonB);
if (flashStateB != lastFlashStateB)
{
dblFlash();
lastFlashStateB = flashStateB;
}
}
void onOff() {
if (flashStateA == LOW)
flashLedA = HIGH;
else
flashLedA = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, flashLedA);
}
void dblFlash() {
if(digitalRead(ledPin1) == LOW){
state = HIGH;
digitalWrite(ledPin1, state);
delay(on);
state = LOW;
digitalWrite(ledPin1, state);
delay(off);
state = HIGH;
digitalWrite(ledPin1, state);
delay(on);
state = LOW;
digitalWrite(ledPin1, state);
}
else {
state = LOW;
digitalWrite(ledPin1, state);
delay(off);
state = HIGH;
digitalWrite(ledPin1, state);
delay(on);
state = LOW;
digitalWrite(ledPin1, state);
delay(off);
state = HIGH;
digitalWrite(ledPin1, state);
}
}
UKHeliBob: if (digitalRead(buttonC) == HIGH)If this line detects that buttonC is pressed then you need an else clause to turn off the LED when it is not pressed.
If I put an else clause then I can never have on/off functionality because every time it runs through the loop and buttonC is LOW then it turns off the LED.
So create a boolean for each button that is true if the LED is flashing due to the corresponding button being pressed and only turn it off when the button input is LOW and is not being flashed by any of the button presses.
Incidentally, what should happen if 2 buttons are pressed ?
I was trying to design the program so that the actual state of the pin doesn't matter to save button clicks otherwise I would have to press buttonB off then on for it to run the double flash function. Two buttons being HIGH doesn't provide any extra functionality for my goal.
Could you please provide a little more explanation, it is not making sense to me. Also any insight on why I have to press buttonA twice for onOff to actually command the led would be appreciated, it worked fine when I was using an external interrupt so I assume there is some logic I do not understand. **If buttonA is HIGH when I reset the Arduino then it works as expected when the first press makes the state go LOW but I guess a little backwards.
const int ledPin1 = 12;
const int onOffBtn = 9;
const int flashBtn = 10;
const int strobeBtn = 11;
int state = LOW;
int onOffBtnState = LOW;
int flashBtnState = LOW;
int strobeBtnState = LOW;
int lastonOffBtnState = LOW;
int lastFlashBtnState = LOW;
int strobeLed = LOW;
int onOffLed = LOW;
const int off = 250;
const int on = 500;
long previousMillis = 0;
long interval = 50;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(onOffBtn, INPUT);
pinMode(flashBtn, INPUT);
pinMode(strobeBtn, INPUT);
}
void loop() {
unsigned long currentMillis = millis();
if(digitalRead(strobeBtn) == HIGH){
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
strobeLed = !strobeLed;
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, strobeLed);
}
}
onOffBtnState = digitalRead(onOffBtn);
if (onOffBtnState != lastonOffBtnState)
{
onOff();
lastonOffBtnState = onOffBtnState;
}
flashBtnState = digitalRead(flashBtn);
if (flashBtnState != lastFlashBtnState)
{
dblFlash();
lastFlashBtnState = flashBtnState;
}
}
void onOff() {
if (onOffBtnState == LOW)
onOffLed = HIGH;
else
onOffLed = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, onOffLed);
}
void dblFlash() {
if(digitalRead(ledPin1) == LOW){
state = HIGH;
digitalWrite(ledPin1, state);
delay(on);
state = LOW;
digitalWrite(ledPin1, state);
delay(off);
state = HIGH;
digitalWrite(ledPin1, state);
delay(on);
state = LOW;
digitalWrite(ledPin1, state);
}
else {
state = LOW;
digitalWrite(ledPin1, state);
delay(off);
state = HIGH;
digitalWrite(ledPin1, state);
delay(on);
state = LOW;
digitalWrite(ledPin1, state);
delay(off);
state = HIGH;
digitalWrite(ledPin1, state);
}
}
Found a friend to help me with this code. Fixed/working code below.
const int ledPin1 = 12;
const int onOffBtn = 9;
const int flashBtn = 10;
const int strobeBtn = 11;
int state = LOW;
int onOffBtnState = LOW;
int flashBtnState = LOW;
int strobeBtnState = LOW;
int lastStrobeBtnState = LOW;
int lastonOffBtnState = LOW;
int lastFlashBtnState = LOW;
int strobeLed = LOW;
int onOffLed = LOW;
const int off = 250;
const int on = 500;
long previousMillis = 0;
long interval = 50;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(onOffBtn, INPUT);
pinMode(flashBtn, INPUT);
pinMode(strobeBtn, INPUT);
}
void loop() {
unsigned long currentMillis = millis();
if(digitalRead(strobeBtn) == HIGH)
{
lastStrobeBtnState = HIGH;
if(currentMillis - previousMillis > interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
strobeLed = !strobeLed;
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, strobeLed);
}
}
else
{
if(lastStrobeBtnState == HIGH)
{
if(digitalRead(onOffBtn) == HIGH)
{
digitalWrite(ledPin1, HIGH);
}
else
{
digitalWrite(ledPin1, LOW);
}
}
strobeLed = LOW;
lastStrobeBtnState = LOW;
}
onOffBtnState = digitalRead(onOffBtn);
if (onOffBtnState != lastonOffBtnState)
{
onOff();
lastonOffBtnState = onOffBtnState;
}
flashBtnState = digitalRead(flashBtn);
if (flashBtnState != lastFlashBtnState)
{
dblFlash();
lastFlashBtnState = flashBtnState;
}
}
void onOff() {
if (onOffBtnState == LOW)
onOffLed = LOW;
else
onOffLed = HIGH;
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, onOffLed);
}
void dblFlash() {
if(digitalRead(ledPin1) == LOW){
state = HIGH;
digitalWrite(ledPin1, state);
delay(on);
state = LOW;
digitalWrite(ledPin1, state);
delay(off);
state = HIGH;
digitalWrite(ledPin1, state);
delay(on);
state = LOW;
digitalWrite(ledPin1, state);
}
else {
state = LOW;
digitalWrite(ledPin1, state);
delay(off);
state = HIGH;
digitalWrite(ledPin1, state);
delay(on);
state = LOW;
digitalWrite(ledPin1, state);
delay(off);
state = HIGH;
digitalWrite(ledPin1, state);
}
}