Go Down

Topic: RGB LED project - need help (Read 400 times) previous topic - next topic

bigmac5753

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.

Code: [Select]

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;
    } 
}

Paul__B

#1
Jun 24, 2019, 01:14 pm Last Edit: Jun 24, 2019, 01:16 pm by Paul__B
Use WS2812 LEDs aka "Neo-Pixels".








Oops!

I just noticed interrupts in the code!

Get rid of those!

bigmac5753

Use WS2812 LEDs aka "Neo-Pixels".


How will different LEDs solve the problem?

Grumpy_Mike

#3
Jun 24, 2019, 03:27 pm Last Edit: Jun 24, 2019, 03:28 pm by Grumpy_Mike
 Because your code could be written properly instead of the mess you have written.

It will reduce the number of pins needed to drive the LEDs from the Arduino from nine to one.

Don't use interrupts for push buttons, don't write the same lines over and over. Learn to use for loops and arrays.

bigmac5753


Grumpy_Mike

#5
Jun 24, 2019, 04:16 pm Last Edit: Jun 24, 2019, 04:18 pm by Grumpy_Mike
If you want more specific help then engage in a conversation. If you want to carry on doing it all wrong then that is up to you and I will leave you to it.

Given you say
Quote
I'm a complete beginner with arduino code
I would have thought you might be keen to learn but who knows?

bigmac5753

#6
Jun 24, 2019, 04:27 pm Last Edit: Jun 24, 2019, 04:28 pm by bigmac5753 Reason: typo
If you want more specific help then engage in a conversation.
The conversation started with my first post and came to an abrupt end with the terse responses.

Clearly, you don't know the solution to this problem, so why bother replying?

Perhaps a self-confessed "grumpy" person shouldn't really be responding to posts.

Grumpy_Mike

#7
Jun 24, 2019, 04:48 pm Last Edit: Jun 24, 2019, 04:51 pm by Grumpy_Mike
Quote
Clearly, you don't know the solution to this problem, so why bother replying?
Oh come on you have a simple problem and you are making a pigs ear of it. I could write the code for you but where would that get us?

We don't write code for people here we try and teach them to write it for themselves. This is a lot harder than simply writing code but it is worthwhile when it helps you understand things.
However you seem to be one of the new generation of people who can't stand being told they are wrong. Unless you can admit you are wrong you will never learn anything.

Quote
Perhaps a self-confessed "grumpy" person shouldn't really be responding to posts.
Check out the Kama points, they are an indication of how helpful I can be but it looks like your attitude is going to be a big stumbling block, so I will spend my time helping somebody a bit more receptive.

Good luck with your life.
Goodbye.

bigmac5753

We don't write code for people here we try and teach them to write it for themselves.
Code has been used to answer the majority of posts on here.

Check out the Kama points,
Ego, nothing more.

Goodbye
The single, most helpful word you could've written, hallelujah.

Paul__B

Oh dear!

I see you referred to "cycling" through the basic colours; I thought you wanted to fade through them.

Never mind.  As you were ... carry on!  :smiley-lol:

Go Up