I'm creating a nightlight for my autistic daughter. It will consist of 3x RGB LEDs and 3x tactile buttons. Each button will cycle its LED through the 7 rainbow colours then return to off on the eighth press. The 3 LEDs must work independently of each other, for example:
LED1 could be yellow for example, and the other 2 LEDs can still cycle through colours whilst not affecting LED1.
I'm a complete beginner with arduino code and have mainly put my code together with snippets of other relevant code.
The end result from the following code is messed up, only one LED will cycle through the colours, but the other 2 LEDS do nothing. The first LED also does not return to off on the eighth press.
I suspect the issue is with the order of code within loop, but I cannot figure out how to do it properly.
const int blue1 = 13;
const int green1 = 12;
const int red1 = 14;
const int blue2 = 27;
const int green2 = 26;
const int red2 = 25;
const int blue3 = 1;
const int green3 = 22;
const int red3 = 23;
const int button1 = 33;
const int button2 = 32;
const int button3 = 35;
const int freq = 5000;
const int blue1ch = 0;
const int green1ch = 1;
const int red1ch = 2;
const int blue2ch = 3;
const int green2ch = 4;
const int red2ch = 5;
const int blue3ch = 6;
const int green3ch = 7;
const int red3ch = 8;
const int resolution = 8;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
void IRAM_ATTR b1() {
counter1++;
delay(250);
}
void IRAM_ATTR b2() {
counter2++;
delay(250);
}
void IRAM_ATTR b3() {
counter3++;
delay(250);
}
void setup(){
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
ledcSetup(blue1ch, freq, resolution);
ledcSetup(green1ch, freq, resolution);
ledcSetup(red1ch, freq, resolution);
ledcSetup(blue2ch, freq, resolution);
ledcSetup(green2ch, freq, resolution);
ledcSetup(red2ch, freq, resolution);
ledcSetup(blue3ch, freq, resolution);
ledcSetup(green3ch, freq, resolution);
ledcSetup(red3ch, freq, resolution);
attachInterrupt(digitalPinToInterrupt(button1), b1, RISING);
attachInterrupt(digitalPinToInterrupt(button2), b2, RISING);
attachInterrupt(digitalPinToInterrupt(button3), b3, RISING);
ledcAttachPin(blue1, blue1ch);
ledcAttachPin(green1, green1ch);
ledcAttachPin(red1, red1ch);
ledcAttachPin(blue2, blue2ch);
ledcAttachPin(green2, green2ch);
ledcAttachPin(red2, red2ch);
ledcAttachPin(blue3, blue3ch);
ledcAttachPin(green3, green3ch);
ledcAttachPin(red3, red3ch);
}
void loop() {
if (counter1 == 0) {
digitalWrite(red1, LOW);
digitalWrite(green1, LOW);
digitalWrite(blue1, LOW);
}
else if (counter1 == 1) {
ledcWrite(red1ch, 255);
ledcWrite(green1ch, 0);
ledcWrite(blue1ch, 0);
}
else if (counter1 == 2) {
ledcWrite(red1ch, 255);
ledcWrite(green1ch, 127);
ledcWrite(blue1ch, 0);
}
else if (counter1 == 3) {
ledcWrite(red1ch, 255);
ledcWrite(green1ch, 255);
ledcWrite(blue1ch, 0);
}
else if (counter1 == 4) {
ledcWrite(red1ch, 0);
ledcWrite(green1ch, 255);
ledcWrite(blue1ch, 0);
}
else if (counter1 == 5) {
ledcWrite(red1ch, 0);
ledcWrite(green1ch, 0);
ledcWrite(blue1ch, 255);
}
else if (counter1 == 6) {
ledcWrite(red1ch, 75);
ledcWrite(green1ch, 0);
ledcWrite(blue1ch, 130);
}
else if (counter1 == 7) {
ledcWrite(red1ch, 148);
ledcWrite(green1ch, 0);
ledcWrite(blue1ch, 211);
}
else if (counter1 == 8) {
digitalWrite(red1, LOW);
digitalWrite(green1, LOW);
digitalWrite(blue1, LOW);
int counter1 = 0;
}
if (counter2 == 0) {
digitalWrite(red2, LOW);
digitalWrite(green2, LOW);
digitalWrite(blue2, LOW);
}
else if (counter2 == 1) {
ledcWrite(red2ch, 255);
ledcWrite(green2ch, 0);
ledcWrite(blue2ch, 0);
}
else if (counter2 == 2) {
ledcWrite(red2ch, 255);
ledcWrite(green2ch, 127);
ledcWrite(blue2ch, 0);
}
else if (counter2 == 3) {
ledcWrite(red2ch, 255);
ledcWrite(green2ch, 255);
ledcWrite(blue2ch, 0);
}
else if (counter2== 4) {
ledcWrite(red2ch, 0);
ledcWrite(green2ch, 255);
ledcWrite(blue2ch, 0);
}
else if (counter2 == 5) {
ledcWrite(red2ch, 0);
ledcWrite(green2ch, 0);
ledcWrite(blue2ch, 255);
}
else if (counter2 == 6) {
ledcWrite(red2ch, 75);
ledcWrite(green2ch, 0);
ledcWrite(blue2ch, 130);
}
else if (counter2 == 7) {
ledcWrite(red2ch, 148);
ledcWrite(green2ch, 0);
ledcWrite(blue2ch, 211);
}
else if (counter2 == 8) {
digitalWrite(red2, LOW);
digitalWrite(green2, LOW);
digitalWrite(blue2, LOW);
int counter2 = 0;
}
if (counter3 == 0) {
digitalWrite(red3, LOW);
digitalWrite(green3, LOW);
digitalWrite(blue3, LOW);
}
else if (counter3 == 1) {
ledcWrite(red3ch, 255);
ledcWrite(green3ch, 0);
ledcWrite(blue3ch, 0);
}
else if (counter3 == 2) {
ledcWrite(red3ch, 255);
ledcWrite(green3ch, 127);
ledcWrite(blue3ch, 0);
}
else if (counter3 == 3) {
ledcWrite(red3ch, 255);
ledcWrite(green3ch, 255);
ledcWrite(blue3ch, 0);
}
else if (counter3== 4) {
ledcWrite(red3ch, 0);
ledcWrite(green3ch, 255);
ledcWrite(blue3ch, 0);
}
else if (counter3 == 5) {
ledcWrite(red3ch, 0);
ledcWrite(green3ch, 0);
ledcWrite(blue3ch, 255);
}
else if (counter3 == 6) {
ledcWrite(red3ch, 75);
ledcWrite(green3ch, 0);
ledcWrite(blue3ch, 130);
}
else if (counter3 == 7) {
ledcWrite(red3ch, 148);
ledcWrite(green3ch, 0);
ledcWrite(blue3ch, 211);
}
else if (counter3 == 8) {
digitalWrite(red3, LOW);
digitalWrite(green3, LOW);
digitalWrite(blue3, LOW);
int counter3 = 0;
}
}