LED-RGB and LDR

The light intensity received in a LDR (light dependent resistor) controlled intensity in a LED-RGB (LED with red, green and blue colours). Arduino is the controller.

LEDRGBLDR

Source code:

int LDRvalue = 1;
int RedLED = 9;
int GreenLED=10;
int BlueLED=11;
int pinLDR = 1;
//RGB LED uses 3 outputs to control 3 basic colours
void setup()
{
pinMode(RedLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(BlueLED, OUTPUT);
analogReference(EXTERNAL);
//Pins are outputs
}
void loop()
{
LDRvalue = analogRead(pinLDR);
if(LDRvalue >= 1023)
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 0);
// Value corresponds to Colour Intensity
}
else if((LDRvalue >= 959) & (LDRvalue < 1023))
{
digitalWrite(RedLED, 255);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 895) & (LDRvalue < 959))
{
digitalWrite(RedLED, 255);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 831) & (LDRvalue < 895))
{
digitalWrite(RedLED, 255);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 767) & (LDRvalue < 831))
{
digitalWrite(RedLED, 255);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >= 703) & (LDRvalue < 767))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 255);
}
else if((LDRvalue >= 639) & (LDRvalue < 703))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 255);
}
else if((LDRvalue >= 575) & (LDRvalue < 639))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 255);
}
else if((LDRvalue >= 511) & (LDRvalue < 575))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 255);
}
else if((LDRvalue >= 447) & (LDRvalue < 511))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >= 383) & (LDRvalue < 447))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 319) & (LDRvalue < 383))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 255) & (LDRvalue < 319))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 0);
}
else if((LDRvalue >= 191) & (LDRvalue < 255))
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >= 127) & (LDRvalue < 191))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 255);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >= 63) & (LDRvalue < 127))
{
digitalWrite(RedLED, 128);
digitalWrite(GreenLED, 128);
digitalWrite(BlueLED, 128);
}
else if((LDRvalue >=0) & (LDRvalue < 63))
{
digitalWrite(RedLED, 55);
digitalWrite(GreenLED, 55);
digitalWrite(BlueLED, 55);
}
else
{
digitalWrite(RedLED, 0);
digitalWrite(GreenLED, 0);
digitalWrite(BlueLED, 0);
}

}
void color(int red, int green, int blue)

{
analogWrite(RedLED, 255-red);
analogWrite(GreenLED, 255-green);
analogWrite(BlueLED, 255-blue);
// The conditional if-else structure controls colour corresponding to light intensity.
}

 

 

Deixa un comentari

L'adreça electrònica no es publicarà Els camps necessaris estan marcats amb *

 

Un bloc a XTECBlocs