Hi all, I'm kinda new to this Arduino stuff so if I'm posting wrong let me know.
What I'm wanting to do is use two buttons to turn three LED's on and off. I'm trying to make a puzzle, though, so I want a single button to turn an LED off and another on. I'd essentially like the switch to invert whatever the existing HIGH/LOW condition is.
The current program I'm using is:
int ledPin = 13; int ledPin2 = 12; int ledPin3 = 11; int inputPin1 = 3; int inputPin2 = 2; void setup() {
pinMode(ledPin, OUTPUT); //declare LED as output*
pinMode(ledPin2, OUTPUT);*
pinMode(ledPin3, OUTPUT);*
pinMode(inputPin1, INPUT); //make button 1 an input*
pinMode(inputPin2, INPUT); //make button 2 an input* } void loop(){
if (digitalRead(inputPin1) == LOW) {*
digitalWrite(ledPin, HIGH); //turn LED off*
if (digitalRead(ledPin) == HIGH) {*
digitalWrite(ledPin, LOW);*
}*
digitalWrite(ledPin2, HIGH);*
digitalWrite(ledPin3, LOW);*
} else if (digitalRead(inputPin2) == LOW) {*
digitalWrite(ledPin, HIGH); //turn LED on*
digitalWrite(ledPin2, HIGH);*
digitalWrite(ledPin3, HIGH);*
}* }
So far, I've only tried to edit ledPin, as you can see, but the result is that by trying to change ledPin to LOW whenever it might be HIGH, it just keeps it at LOW rather than ever inverting it. I'm sure I've explained this poorly, so if you need further explanation let me know. I'd really appreciate any help you can give.
Press button1: (inverts LED1/2)
LED 1: Off
LED 2: Off
LED 3: On
Press button 2: (inverts LED2/3)
LED 1: Off
LED 2:On
LED 3:Off
etc
Essentially, it's less about switching the LED's from HIGH to LOW and more about inverting the previous condition. That might not be possible, though, and if that's the case, just let me know. Thanks!
The 2 button pushes are momentary push buttons? Or toggle switches that you flip back & forth?
You indicate 'etc' in your post - implying an On-Off-Off contition next - which button push for that?
Does a 4 button push (and which one) then take you back to On-On-On? Is there Off-Off-Off in there somewhere?
What you could do is this - have 1 momentary push button, every time it is pressed it counts thru some #, then resets back to the start state:
void loop(){
if (digitalRead(inputPin1) == LOW) {
state = state +1;
delay(300); // crude debounce of button push, assuming momentary button
}
if (state == 6){ // for example; could be higher: 000, 001, 010, 011, 100, 101, 110, 111 - so reset if hit 8
state = 0; //reset
}
switch (state0){
case 0:
// some collection of on/off LEDs
break;
case 1:
// next collection
break;
:
:
case 5:
// final collection
break;
} // end switch(case)
} // end void loop