Tuesday, March 2, 2010

First Attempt at Using Pulse Width Modulation

Bored (again) and wanted to play around with the RGB LED from the kit. After a bit of research and reading up on PWM with the Arduino I wrote this little program:

boolean go = HIGH;
int red;
int green;
int blue;

void setup() {
randomSeed(analogRead(5));
Serial.begin(9600);
}

void loop() {
if (go == HIGH){
go = !go;
red = random(1,255);
blue = random(1,255);
green = random(1,255);
analogWrite(11,blue);
analogWrite(10,green);
analogWrite(9,red);
Serial.print("red =");
Serial.println(red);
Serial.print("blue =");
Serial.println(blue);
Serial.print("green =");
Serial.println(green);
}
}

Basically the Arduino will generate three random numbers and use those values on the PWM pins to display a random colour with the RGB LED. These values are also sent via serial back to the PC to view. I wrote the program to only run once so to generate a new colour the reset button must be used. I was happy with how the program ran but disappointed with the physical output as the colours do not "blend" well enough.

No comments:

Post a Comment