I mentioned a while agolast 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:
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.
I kept the wiring constant, since non-constant would be too confusing. Just a personal preference when dealing with RGB LEDs.
That is all for the RGB LEDs. Next part is the Fade function!
// Delay an amount of time between steps of colouring the LED
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 ).
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.
It goes straight into Analog 0.
Now I just need a simple function to map the analog reading back to an appropriate time 😀
int result = map(analogRead(0), 0, 1023, 500, 10);
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:
LR =int(random(50, 255));
LG =int(random(50, 255));
LB =int(random(50, 255));
RR =int(random(50, 255));
RG =int(random(50, 255));
RB =int(random(50, 255));
fade( preLR, preLG, preLB,// L Start
LR, LG, LB,// L Finish
preRR, preRG, preRB,// R Start
RR, RG, RB,// R Finish
preLR = LR;
preLG = LG;
preLB = LB;
preRR = RR;
preRG = RG;
preRB = RB;
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!! 😀 ^_^ =)