0% found this document useful (0 votes)
6 views3 pages

RGB Led

Uploaded by

dlekgml5555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

RGB Led

Uploaded by

dlekgml5555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

int RED = 9;

int GREEN = 10;


int BLUE = 11;
//#define RED 9
int delayTime = 500;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
//mainColor();
//rgbLight1();
rgbLight2();
}

void mainColor() {
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
delay(delayTime);

digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, HIGH);
delay(delayTime);

digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, LOW);
delay(delayTime);

digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
delay(delayTime);

digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
delay(delayTime);

digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, HIGH);
delay(delayTime);

digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, LOW);
delay(delayTime);

digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
delay(delayTime);

void rgbLight1() {
for(int i=0; i<256; i++) {
for(int j=0; j<256; j++) {
for(int k=0; k<256; k++) {
analogWrite(RED, i);
analogWrite(GREEN, j);
analogWrite(BLUE, k);
delay(delayTime/50);
}
}
}
}

void rgbLight2() {
for(int x=0; x<=767; x++) {
rgb(x);
delay(10);
}
}

void rgb(int color) {


int redIntensity;
int greenIntensity;
int blueIntensity;

color = constrain(color, 0, 767);

if (color <= 255) {


redIntensity = 255-color;
greenIntensity = color;
blueIntensity = 0;
}
else if (color <= 511) {
redIntensity = 0;
greenIntensity = 511-color;
blueIntensity = color-255;
}
else {
redIntensity = color-512;
greenIntensity = 0;
blueIntensity = 767-color;

}
analogWrite(RED, redIntensity);
analogWrite(GREEN, greenIntensity);
analogWrite(BLUE, blueIntensity);
Serial.print(redIntensity);
Serial.print(", ");
Serial.print(greenIntensity);
Serial.print(", ");
Serial.println(blueIntensity);

You might also like