INPUT: Photocell
OUTPUT: RGB LED
DESCRIPTION: The darker the lighting is, the brighter all three colors go.
[code lang=”arduino” light=”true”] int photocellPin = A6;int ledPins[] = {
11, 10, 9 }; // an array of pin numbers to which LEDs are attached
int pinCount = 3; // the number of pins (i.e. the length of the array)
int redPin = 9; // R petal on RGB LED module connected to digital pin 11
int greenPin = 11; // G petal on RGB LED module connected to digital pin 9
int bluePin = 10; // B petal on RGB LED module connected to digital pin 10
void setup() {
Serial.begin(9600);
int thisPin;
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
int photoValue = analogRead (photocellPin);
// Serial.println(photoValue); // raw analog readings
int LEDbrightness = map(photoValue, 0, 30, 0, 255);
Serial.println(LEDbrightness); //print brightness value
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
analogWrite(ledPins[thisPin], LEDbrightness); // brightness of all pins at once
// Address each pin individually
//analogWrite (redPin, LEDbrightness);
// analogWrite (bluePin, LEDbrightness);
// analogWrite (greenPin, LEDbrightness);
// turn the pins off:
// digitalWrite(ledPins[thisPin], HIGH);
}
}
[/code]