Arduino beginner and a noob in programing here. After searching for answers in a lot of forums, I'm out of ideas to solve my problem.
I'm using arduino UNO, trying to light 6 red leds in different combinations with 3 different pushbuttons (i.e. if I press button1 2 leds light up, if I press button2 3 leds light up, if I press buttons 2 and 3 other 2 leds light up).
With a basic set of if/else statements I managed to do this but, because I ended up with 6 more free pins (analog ones) and since originally I wanted to have another set of 6 green leds that are always on and only turn OFF when the respective red is ON I tried to incorporate that in my code and my problems started.
At first when I inserted digitalWrite for green leds they, at best, got less bright but stay on all the time, then I created arrays for the red and green leds and called red ones as input simultaneosly and made greens OFF when reds are ON and this is still my best result so far but still greens doesn´t work fine in all button presses.
Here is my code
const int green [] = {A0, A1, A2, A3, A4, A5};
int pinCount = 6;
int redState[6];
const int red [] = {2, 4, 6, 8, 10, 12};
const int button1 = 3;
const int button2 = 5;
const int button3 = 7;// the number of the pushbutton pin
const int led1 = 2;
const int led2 = 4;// the number of the LED pin
const int led3 = 6;
const int led4 = 8;
const int led5 = 10;
const int led6 = 12;
int button1State = 0;
int button2State = 0;
int button3State = 0;
void setup() {
for (int i = 0; i < pinCount; i++) {
pinMode(green[i], OUTPUT);
pinMode(red[i], INPUT);
}
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
}
void loop() {
for (int i = 0; i < pinCount; i++) {
redState[i] = digitalRead(red[i]);
digitalWrite(green[i], !redState[i]);
}
button1State = digitalRead(button1);
button2State = digitalRead(button2);
button3State = digitalRead(button3);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (button1State == HIGH & button2State == LOW & button3State == LOW) {
// turn LED on:
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
} else {
// turn LED off:
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
if (button2State == HIGH & button1State == LOW & button3State == LOW) {
// turn LED on:
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
} else {
// turn LED off:
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
}
if (button3State == HIGH & button1State == LOW & button2State == LOW) {
// turn LED on:
digitalWrite(led1, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
} else {
// turn LED off:
digitalWrite(led1, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
}
if (button1State == HIGH & button2State == HIGH & button3State == LOW) {
// turn LED on:
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
} else {
// turn LED off:
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
}
I'm well aware that this could be much more efficiently done with binary tactics but my efforts were fruitless (couldn't make it work). For extra info I used 220 ohm resistors for green LEDs and 560 ohm resistors for red LEDs (because they were presoldered and they worked fine)(I tried to change resistors of reds and results were the same. Push buttons are connected with 10K resistors.
PS: using bitwise or boolean AND in 'if conditions' gives me the same result