Hello everyone, I'm new to Arduino and I would appreciate it if there's someone out there that could help me with this problem.
For my project I needed 3 buttons that control a different number of LED's. So, 3 LED's will light up when I push the first button. then, when the second button is being pushed while the first button is also being pushed at the same time, 5 LED's will light up and finally when I push the third button along with the first button, 7 LED's will light up.
So, just to make things easy to understand, the first button needs to be pushed every time.
I guess that it's important to mention that I'm working with an addressable LED Strip WS2812.
And that I wired all my buttons without any resistors, so just the signal and the ground.
I really do hope that my problem has come to it's end. And I will appreciate every answer.
Here's my code:
const int pushButton1 = 4;
const int pushButton2 = 5;
const int pushButton3 = 6;
int count = 0;
int firstTime = 1;
#include <FastLED.h>
#define NUM_LEDS 7
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
pinMode(pushButton1, INPUT_PULLUP);
pinMode(pushButton2, INPUT_PULLUP);
pinMode(pushButton3, INPUT_PULLUP);
delay(50);
}
void loop(){
while (!digitalRead(pushButton1)== HIGH) {
if (firstTime) {
if (count <3) {
count++;
}
}
firstTime = 0;
}
if (!firstTime) {
if (count == 1) {
leds[0] = CRGB::Green;
FastLED.show();
leds[3] = CRGB::Black;
} else if (count == 2) {
leds[2] = CRGB::Green;
FastLED.show();
leds[1] = CRGB::Black;
FastLED.show();
} else if (count == 3) {
leds[3] = CRGB::Green;
FastLED.show();
leds[2] = CRGB::Black;
FastLED.show();
}
firstTime = 1;
}
while (!digitalRead(pushButton1)== HIGH && digitalRead(pushButton2)== HIGH) {
if (firstTime) {
if (count <5) {
count++;
}
}
firstTime = 0;
}
if (!firstTime) {
if (count == 1) {
leds[0] = CRGB::Green;
FastLED.show();
leds[4] = CRGB::Black;
FastLED.show();
} else if (count == 2) {
leds[1] = CRGB::Green;
FastLED.show();
leds[0] = CRGB::Black;
FastLED.show();
} else if (count == 3) {
leds[2] = CRGB::Green;
FastLED.show();
leds[1] = CRGB::Black;
FastLED.show();
} else if (count == 4) {
leds[3] = CRGB::Green;
FastLED.show();
leds[2] = CRGB::Black;
FastLED.show();
} else if (count == 5) {
leds[4] = CRGB::Green;
FastLED.show();
leds[3] = CRGB::Black;
FastLED.show();
}
firstTime = 1;
}
while (!digitalRead(pushButton1)== HIGH && digitalRead(pushButton3) == HIGH) {
if (firstTime) {
if (count <7) {
count++;
}
}
firstTime = 0;
}
if (!firstTime) {
if (count == 1) {
leds[0] = CRGB::Green;
FastLED.show();
leds[6] = CRGB::Black;
FastLED.show();
} else if (count == 2) {
leds[1] = CRGB::Green;
FastLED.show();
leds[0] = CRGB::Black;
FastLED.show();
} else if (count == 3) {
leds[2] = CRGB::Green;
FastLED.show();
leds[1] = CRGB::Black;
FastLED.show();
} else if (count == 4) {
leds[3] = CRGB::Green;
FastLED.show();
leds[2] = CRGB::Black;
FastLED.show();
} else if (count == 5) {
leds[4] = CRGB::Green;
FastLED.show();
leds[3] = CRGB::Black;
FastLED.show();
} else if (count == 6) {
leds[5] = CRGB::Green;
FastLED.show();
leds[4] = CRGB::Black;
FastLED.show();
} else if (count == 7) {
leds[6] = CRGB::Green;
FastLED.show();
leds[5] = CRGB::Black;
FastLED.show();
}
firstTime = 1;
}
}