Jump to content
43oh

Recommended Posts

Hi guys.

 

I create a program that mix all the 3 basic colors of the RGB leds.

 

It uses 3 buttons (one for RED, other for BLUE and the other is... yes, you're right, GREEN). If you press one single button it rises the brightness of the correspondent color and then it fades (the famous "heartbeat"). If you press two or three buttons it raises the mix between those colors and then fades it.

 

It's a pretty simple program just to show to kids how the light combine colors.

 

Here you have the code:

const int redLED= 9; // assign red LED to Pin #9 (P2.1)
const int greenLED= 10; // assign green LED to Pin #10 (P2.2)
const int blueLED= 12; // assign blue LED to Pin #12 (P2.4)
const int redButton = 5; // assign red button to Pin #5 (P1.3)
const int greenButton = 8; // assign green button to Pin #8 (P2.0)
const int blueButton = 11; // assign blue button to Pin #11 (P2.3)

void setup() { 
  pinMode(redLED, OUTPUT); 
  pinMode(greenLED, OUTPUT); 
  pinMode(blueLED, OUTPUT);
  pinMode(redButton, INPUT_PULLUP);
  pinMode(greenButton, INPUT_PULLUP);
  pinMode(blueButton, INPUT_PULLUP);
}

void loop(){
  int sensorRED = digitalRead(redButton);
  int sensorGREEN = digitalRead(greenButton);
  int sensorBLUE = digitalRead(blueButton);
  int r=0;
  int g=0;
  int b=0;
  
  analogWrite(redLED, 0);
  analogWrite(greenLED, 0);
  analogWrite(blueLED, 0);
  
  if (digitalRead(redButton) == LOW) {
    while(r<255){
      analogWrite(redLED, r);
      delay(30);
      r=r+5;
      if (digitalRead(greenButton) == LOW){
        while (r<255){
          analogWrite(redLED, r);
          analogWrite(greenLED, g);
          delay(30);
          r=r+5;
          g=g+5;
          
          if (digitalRead(blueButton) == LOW){
            while (r<255){
              analogWrite(redLED, r);
              analogWrite(greenLED, g);
              analogWrite(blueLED, ;
              delay(30);
              r=r+5;
              g=g+5;
              b=b+5;
            }
          }
        }
      }
      if (digitalRead(blueButton) == LOW){
        while (r<255){
          analogWrite(redLED, r);
          analogWrite(blueLED, ;
          delay(30);
          r=r+5;
          b=b+5;
          
          if (digitalRead(greenButton) == LOW){
            while (r<255){
              analogWrite(redLED, r);
              analogWrite(greenLED, g);
              analogWrite(blueLED, ;
              delay(30);
              r=r+5;
              g=g+5;
              b=b+5;
            }
          }
        }
      }
    }

    
    while(r>0){
      analogWrite(redLED, r);
      delay(30);
      r=r-5;
    }
    while(g>0){
      analogWrite(greenLED, g);
      delay(30);
      g=g-5;
    }
    while(b>0){
      analogWrite(blueLED, ;
      delay(30);
      b=b-5;
    }
  }
  
  if (digitalRead(greenButton) == LOW) {
    while(g<255){
      analogWrite(greenLED, g);
      delay(30);
      g=g+5;
      if (digitalRead(blueButton) == LOW){
        while (g<255){
          analogWrite(blueLED, ;
          analogWrite(greenLED, g);
          delay(30);
          b=b+5;
          g=g+5;
          
          if (digitalRead(redButton) == LOW){
            while (g<255){
              analogWrite(redLED, r);
              analogWrite(greenLED, g);
              analogWrite(blueLED, ;
              delay(30);
              r=r+5;
              g=g+5;
              b=b+5;
            }
          }
        }
      }
      if (digitalRead(redButton) == LOW){
        while (g<255){
          analogWrite(redLED, r);
          analogWrite(greenLED, g);
          delay(30);
          r=r+5;
          g=g+5;
          
          if (digitalRead(blueButton) == LOW){
            while (g<255){
              analogWrite(redLED, r);
              analogWrite(greenLED, g);
              analogWrite(blueLED, ;
              delay(30);
              r=r+5;
              g=g+5;
              b=b+5;
            }
          }
        }
      }
    }

    
    while(g>0){
      analogWrite(greenLED, g);
      delay(30);
      g=g-5;
    }
    while(b>0){
      analogWrite(blueLED, ;
      delay(30);
      b=b-5;
    }
    while(r>0){
      analogWrite(redLED, r);
      delay(30);
      r=r-5;
    }
  }
  if (digitalRead(blueButton) == LOW) {
    while(b<255){
      analogWrite(blueLED, ;
      delay(30);
      b=b+5;
      if (digitalRead(redButton) == LOW){
        while (b<255){
          analogWrite(blueLED, ;
          analogWrite(redLED, r);
          delay(30);
          b=b+5;
          r=r+5;
          
          if (digitalRead(greenButton) == LOW){
            while (b<255){
              analogWrite(redLED, r);
              analogWrite(greenLED, g);
              analogWrite(blueLED, ;
              delay(30);
              r=r+5;
              g=g+5;
              b=b+5;
            }
          }
        }
      }
      if (digitalRead(greenButton) == LOW){
        while (b<255){
          analogWrite(blueLED, ;
          analogWrite(greenLED, g);
          delay(30);
          b=b+5;
          g=g+5;
          
          if (digitalRead(redButton) == LOW){
            while (b<255){
              analogWrite(redLED, r);
              analogWrite(greenLED, g);
              analogWrite(blueLED, ;
              delay(30);
              r=r+5;
              g=g+5;
              b=b+5;
            }
          }
        }
      }
    }

    
    while(b>0){
      analogWrite(blueLED, ;
      delay(30);
      b=b-5;
    }
    while(r>0){
      analogWrite(redLED, r);
      delay(30);
      r=r-5;
    }
    while(g>0){
      analogWrite(greenLED, g);
      delay(30);
      g=g-5;
    }
  } 
}

Be free to made all the changes you want (if anyone get a way to make the code lighter I'll be very greatfull :D) and post those changes here please. All changes are welcome! :)

 

 

Hope you guys like this!

Link to post
Share on other sites

Yes there's room for improvement, what about using variables to hold color increments, something like

// debounce inputs and leave time to press another button
if (digitalRead(redButton) == LOW || digitalRead(blueButton) == LOW || digitalRead(greenButton) == LOW)
  delay(50);

// rinc, ginc, binc hold the r,g and b increments
if (digitalRead(redButton) == LOW)
  rinc=5;
else
  rinc=0;

if (digitalRead(blueButton) == LOW)
  binc=5;
else
  binc=0;

if (digitalRead(greenButton) == LOW)
  ginc=5;
else
  ginc=0;

// go up to 255
int i;
for (int i=0; i<51; i++) {
  r=r+rinc;
  g=g+ginc;
  b=b+binc;
  analogWrite(redLED, r);
  analogWrite(greenLED, g);
  analogWrite(blueLED, ;
  delay(30);
}

// go back down to 0
for (int i=0; i<51; i++) {
  r=r-rinc;
  g=g-ginc;
  b=b-binc;
  analogWrite(redLED, r);
  analogWrite(greenLED, g);
  analogWrite(blueLED, ;
  delay(30);
}

Not tested, I hope I didn't screw up the loop counts :-)

Link to post
Share on other sites

Yes, thats a great improvement and thank you very much for that :grin: , but mine idea with that code I've posted is to create colors with different basic colors intensity. To make brown you need the same color that you need to get orange/yellow (green+red) but in different intensities.

Link to post
Share on other sites

That's also a great project. Instead the up and down auto cycle of the colors, I can make it manual.

 

That will be my next project with a RGB :D To save space on the breadboard I'll keep the 3 buttons setup and maybe one more button to "reset" the colors. Thank you for the tip! :mrgreen:

Link to post
Share on other sites

The first is manual. The second is automatic. It has both possibilitys

 

if you want a 3 button i have a sugestion.

 

use 2. 1 increasses, another one decreasses.

The 3rd one cycles wich color your actualy controlling.

You can add 1-3 leds to show wich collor is selected to control. Just use a digital pin for them. Or not. It also works without them

 

This about the size of my manual board:

http://lusorobotica.com/index.php?action=dlattach;topic=6087.0;attach=1973;image

Link to post
Share on other sites
const int num_colors = 3; // the number of individual colors
const int LED[num_colors] = {9, 10, 12}; // assign red, green, blue LEDs to pins P2.1, P2.2 and P2.4 respectively
const int Button[num_colors] = {5, 8, 11}; // assign red, green, blue buttons to P1.3, P2.0 and P2.3 respectively

void setup() { 
  for(int i = 0; i < num_colors; ++i) {
    pinMode(LED[i], OUTPUT); 
    pinMode(Button[i], INPUT_PULLUP);
  }
}

void loop(){
  int buttonstates[num_colors];
  int colors[num_colors] = {0};

  // read all buttons
  for(int i = 0; i < num_colors; ++i) {
    buttonstates[i] = digitalRead(Button[i]);
  }
  // increment in 51 steps
  for (int c = 0; c <= 255/5; ++c) {
    for(int i = 0; i < num_colors; ++i) {
      if (buttonstates[i] == LOW) colors[i] += 5;
      analogWrite(LED[i], colors[i]);
    }
    delay(30);
  }
  // decrement in 51 steps
  for (int c = 0; c <= 255/5; ++c) {
    for(int i = 0; i < num_colors; ++i) {
      if (buttonstates[i] == LOW) colors[i] -= 5;
      analogWrite(LED[i], colors[i]);
    }
    delay(30);
  }
}

Since you're doing basically everything in threefold, you can replace a lot of things with for loops to do things three times.

Note that colors[] is always either a common value or zero, in this case you could replace it with a single counter and instead check whether to write zero or that value depening on buttonvalues[].

const int num_colors = 3; // the number of individual colors
const int LED[num_colors] = {9, 10, 12}; // assign red, green, blue LEDs to pins P2.1, P2.2 and P2.4 respectively
const int Button[num_colors] = {5, 8, 11}; // assign red, green, blue buttons to P1.3, P2.0 and P2.3 respectively

void setup() { 
  for(int i = 0; i < num_colors; ++i) {
    pinMode(LED[i], OUTPUT); 
    pinMode(Button[i], INPUT_PULLUP);
  }
}

void loop(){
  static int states[num_colors];
  static int colors[num_colors] = {0};

  // read all buttons and set the incrementer accordingly
  for(int i = 0; i < num_colors; ++i) {
    if (digitalRead(Button[i]) == LOW) states[i] = 5;
    if (states[i] == LOW) colors[i] += states[i];
    if (colors[i] > 255) {
      states[i] = -5;
      colors[i] += states[i]; // add MINUS 5 to colors[i]
    }
    if (colors[i] < 0) {
      states[i] = 0; // stop fading
      colors[i] = 0; // turn LED off
    }
    analogWrite(LED[i], colors[i]);
  }
  delay(30);
}

This variant will fade the LEDs independently, so while an LED is fading out, pressing the button will switch it to fading in again.

Pressing the green button when the red LED is at it's brightest will cross fade from red to green.

A lot of other fancy things can be done too, I think this would be much more fun for kids to mix light.

Also note that this solution will not monopolise the chip; every time after the delay(30) is done the control is given back to Energia. If you'd be able to manage to get Energia to execute the loop() only every 30 delay ticks (ms?) you could get rid of the delay() statement alltogether.

Link to post
Share on other sites

The first is manual. The second is automatic. It has both possibilitys

 

if you want a 3 button i have a sugestion.

 

use 2. 1 increasses, another one decreasses.

The 3rd one cycles wich color your actualy controlling.

You can add 1-3 leds to show wich collor is selected to control. Just use a digital pin for them. Or not. It also works without them

 

This about the size of my manual board:

http://lusorobotica.com/index.php?action=dlattach;topic=6087.0;attach=1973;image

 

That is an excellent idea (add another RGB LED or a couple of LED's to know what collor is being controlled). The problem, once again, is the lack of space on the breadboard (I don't want to solder everithing on a prototype board), but I'll see what I can do.

 

 

@@roadrunner84, Thank you for the improvement. That's a really improve since it still does what it should.

About that last tip, I don't want to do it that way, once I don't have the MCU connected to the computer, so it's impossible to have Energia monitoring the button states...

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...