Arduino Smooth Random RGB LED Colour

11 september 2011 | Classified in: Electronic | Tags: Arduino, RGB, LED

Here is a simple code to smoothly generate some random colours using an RGB LED and an Arduino.

Video:


Code:


#define redPin 9
#define greenPin 10
#define bluePin 11

int redPulse   = 0;
int greenPulse = 0;
int bluePulse  = 0;

void setup(){
  randomSeed(analogRead(0));
}

void loop() {
  redPulse = random(0,256); // Randomly generate 1 to 255
  greenPulse = random(0,256); // Randomly generate 1 to 255
  bluePulse = random(0,256); // Randomly generate 1 to 255
  analogWrite(redPin,redPulse);
  analogWrite(greenPin,greenPulse);
  analogWrite(bluePin,bluePulse);

  for(redPulse = 0; redPulse < 255; redPulse += 1){
    analogWrite(redPin,redPulse);
    analogWrite(greenPin,greenPulse);
    analogWrite(bluePin,bluePulse);
    delay(10);
  }
  for(redPulse = 255; redPulse > 0; redPulse -= 1){
    analogWrite(redPin,redPulse);
    analogWrite(greenPin,greenPulse);
    analogWrite(bluePin,bluePulse);
    delay(10);
  }
  for(greenPulse = 0; greenPulse < 255; greenPulse += 1){
    analogWrite(redPin,redPulse);
    analogWrite(greenPin,greenPulse);
    analogWrite(bluePin,bluePulse);
    delay(10);
  }
  for(greenPulse = 255; greenPulse > 0; greenPulse -= 1){
    analogWrite(redPin,redPulse);
    analogWrite(greenPin,greenPulse);
    analogWrite(bluePin,bluePulse);
    delay(10);
  }
  for(bluePulse = 0; bluePulse < 255; bluePulse += 1){
    analogWrite(redPin,redPulse);
    analogWrite(greenPin,greenPulse);
    analogWrite(bluePin,bluePulse);
    delay(10);
  }
  for(bluePulse = 255; bluePulse > 0; bluePulse -= 1){
    analogWrite(redPin,redPulse);
    analogWrite(greenPin,greenPulse);
    analogWrite(bluePin,bluePulse);
    delay(10);
  }
}

Write a comment

What is the fourth letter of the word iwpnz? : 

Categories

Last articles

Last comments