Greetings!

If any of the info on this website was useful for your projects or made your head spin with creative ideas, and you would like to share a token of your appreciation- a donation would be massively appreciated!

Your donation will go to my Robotics Fund, straight towards more sensors, actuators, books, and journeys. It takes a lot to continue building these robots, and I’m really thankful of everyone who helps encourage me along the way.


USD


CAD

“A wise robot once said to me through Serial.println- that robots teach us about ourselves.”

Friday Night Robotics – Fading Multiple RGB LEDs

I mentioned a while ago last week that I wanted to have MANOI’s RGB LEDs fade in and out at different rates from different starting points to different ending points. Luckily, it was a pretty easy task to accomplish, so now I can share the code with everyone! :)

The setup I have for MANOI’s RGB LEDs are using a Sanguino for a microcontroller. It has 6 PWM pins, so I use 3 pins for two LEDs. (The RGB LEDs have 3 leads for red green and blue). On MANOI, this ends up looking like this:

Friday Night Robotics - RGB LEDs + IR Sensor

I define left to be the LED #1 and #3, and right to be LED #2 and #4.

You need 1K ohm resistors, otherwise the red LED will use all of the power, and not save any for green and blue. :(

Friday Night Robotics - RGB LEDs + IR Sensor

I kept the wiring constant, since non-constant would be too confusing. Just a personal preference when dealing with RGB LEDs.

Friday Night Robotics - RGB LEDs + IR Sensor

Friday Night Robotics - RGB LEDs + IR Sensor

That is all for the RGB LEDs. Next part is the Fade function!

  1. void fade ( int startL_R, int startL_G, int startL_B,
  2.             int finishL_R, int finishL_G, int finishL_B,
  3.             int startR_R, int startR_G, int startR_B,
  4.             int finishR_R, int finishR_G, int finishR_B,
  5.             int stepTime ) {
  6.    
  7.   // Defining the ‘skip everys’ for different rates        
  8.   int skipEveryL_R = 256/abs(startL_R-finishL_R);
  9.   int skipEveryL_G = 256/abs(startL_G-finishL_G);
  10.   int skipEveryL_B = 256/abs(startL_B-finishL_B);
  11.   int skipEveryR_R = 256/abs(startR_R-finishR_R);
  12.   int skipEveryR_G = 256/abs(startR_G-finishR_G);
  13.   int skipEveryR_B = 256/abs(startR_B-finishR_B);
  14.  
  15.   // Count up to 255
  16.   for(int i=0; i<256; i++) {
  17.  
  18.     // If it’s fading down…
  19.     if(startL_R<finishL_R) {
  20.       // If we haven’t reached the final point yet…
  21.       if(i<=finishL_R) {
  22.         // If it’s a skip every…
  23.         if(i%skipEveryL_R == 0) {
  24.           // Colour the LED!
  25.           analogWrite(redL, i);
  26.         }
  27.       }
  28.     // If it’s fading up…
  29.     } else if(startL_R>finishL_R) {
  30.       // If we haven’t reached the final point yet…
  31.       if(i>=(256-startL_R)) {
  32.         // If it’s a skip every…
  33.         if(i%skipEveryL_R == 0) {
  34.           // Colour the LED!
  35.           analogWrite(redL, 256-i);
  36.         }
  37.       }
  38.     }
  39.    
  40.     if(startL_G<finishL_G) {
  41.       if(i<=finishL_G) {
  42.         if(i%skipEveryL_G == 0) {
  43.           analogWrite(greenL, i);
  44.         }
  45.       }
  46.     } else if(startL_G>finishL_G) {
  47.       if(i>=(256-startL_G)) {
  48.         if(i%skipEveryL_G == 0) {
  49.           analogWrite(greenL, 256-i);
  50.         }
  51.       }
  52.     }
  53.          
  54.     if(startL_B<finishL_B) {
  55.       if(i<=finishL_B) {
  56.         if(i%skipEveryL_B == 0) {
  57.           analogWrite(blueL, i);
  58.         }
  59.       }
  60.     } else if(startL_B>finishL_B) {
  61.       if(i>=(256-startL_B)) {
  62.         if(i%skipEveryL_B == 0) {
  63.           analogWrite(blueL, 256-i);
  64.         }
  65.       }
  66.     }
  67.    
  68.     if(startR_R<finishR_R) {
  69.       if(i<=finishR_R) {
  70.         if(i%skipEveryR_R == 0) {
  71.           analogWrite(redR, i);
  72.         }
  73.       }
  74.     } else if(startR_R>finishR_R) {
  75.       if(i>=(256-startR_R)) {
  76.         if(i%skipEveryR_R == 0) {
  77.           analogWrite(redR, 256-i);
  78.         }
  79.       }
  80.     }
  81.    
  82.     if(startR_G<finishR_G) {
  83.       if(i<=finishR_G) {
  84.         if(i%skipEveryR_G == 0) {
  85.           analogWrite(greenR, i);
  86.         }
  87.       }
  88.     } else if(startR_G>finishR_G) {
  89.       if(i>=(256-startR_G)) {
  90.         if(i%skipEveryR_G == 0) {
  91.           analogWrite(greenR, 256-i);
  92.         }
  93.       }
  94.     }
  95.          
  96.     if(startR_B<finishR_B) {
  97.       if(i<=finishR_B) {
  98.         if(i%skipEveryR_B == 0) {
  99.           analogWrite(blueR, i);
  100.         }
  101.       }
  102.     } else if(startR_B>finishR_B) {
  103.       if(i>=(256-startR_B)) {
  104.         if(i%skipEveryR_B == 0) {
  105.           analogWrite(blueR, 256-i);
  106.         }
  107.       }
  108.     }
  109.  
  110.     // Delay an amount of time between steps of colouring the LED
  111.     delay(stepTime);
  112.      
  113.   }
  114.  
  115. }

I added in some comments to make it clear what is going on. The if statement ‘block’ is repeated for RGB on left and right. You could use an array if you wanted to make it more complicated and more condensed.

That code by itself is pretty fun. But, it’s even more fun if you get to interact with it!

I created a headband with an IR sensor for MANOI last week (but neglected to blog it :( ).

Friday Night Robotics - RGB LEDs + IR Sensor

It’s very handy. The IR sensor is from Adafruit! It is really simple, and works ‘out of the box’ without any pull up or pull down resistor needed.

Friday Night Robotics - RGB LEDs + IR Sensor

Friday Night Robotics - RGB LEDs + IR Sensor

It goes straight into Analog 0.

Now I just need a simple function to map the analog reading back to an appropriate time :D

  1. int theTime() {
  2.   int result = map(analogRead(0), 0, 1023, 500, 10);
  3.   /*Serial.print(analogRead(0));
  4.   Serial.print(", ");
  5.   Serial.println(result);*/
  6.   return result;
  7. }

The reason why the Serial.print’s are all commented out is because if you leave the Sanguino running with the Serial.print’s, it will eventually run out of memory and just stop. However, for debugging, the Serial.print’s are very handy.

Here is the loop function that I use to give MANOI its magic:

  1. void loop() {
  2.  
  3.   LR = int(random(50, 255));
  4.   LG = int(random(50, 255));
  5.   LB = int(random(50, 255));
  6.   RR = int(random(50, 255));
  7.   RG = int(random(50, 255));
  8.   RB = int(random(50, 255));
  9.  
  10.   fade( preLR,      preLG,      preLB, // L Start
  11.                 LR,           LG,            LB, // L Finish
  12.            preRR,     preRG,       preRB, // R Start
  13.                  RR,          RG,            RB, // R Finish
  14.         1);
  15.        
  16.   delay(theTime());
  17.  
  18.   preLR = LR;
  19.   preLG = LG;
  20.   preLB = LB;
  21.   preRR = RR;
  22.   preRG = RG;
  23.   preRB = RB;
  24.  
  25. }

This code means that the LEDs fade from the previous value to the current value, over and over again. At the beginning of the program, the previous values are said to be 0.

I had to use 50 as a lower bound on the random as everything below it looks off :( Of course, you could add some features where you check to see if all three are below 50, if they are you would re-reandomize the values and check again. If not, you would send them to the fade function.

So all of this results in a really cool effect where if something gets close to MANOI, then its ‘antennai’ will start changing colours rapidly. Here are some videos:

The next thing now is to have MANOI dance and communicate to the Sanguino, Arduino + Motor shield (for MANOI’s ears), and the Arduino + WaveShield (for music). That would mean that MANOI would have FOUR cores!! :D ^_^ =)

You can see more photos on flickr.

Trackbacks for this post

  1. RobotGrrl.com » Blog Archive » Friday Night Robotics – Hula Hooping Motion?

Leave a CommentPlease be polite. We appreciate that.

Your Comment