/* MiddleSisterFINAL * RGB_LED_Fade9_10_11 when LDR Voltage Bridge center leg * drops below a "Trigger" value, YOU specify, due to a * shadow blocking the amount of light getting to the LDR * Code for cross-fading a tri-color RGB LED, * using PWM * The program cross-fades slowly from red to green, green to blue, and blue to red * The debugging code assumes Arduino 0004, as it uses the new Serial.begin()-style functions * originally "dimmingLEDs" by Clay Shirky * * R G B L E D * NEGATIVE (-) "center leg" * Digital Pin 9 * )----------------| 220 ohm Resistor |-------| Red LED|------. * PWM | * | * Digital Pin 10 | * )----------------| 220 ohm Resistor |-------| Blue LED|------|------||: GND * PWM | * | * Digital Pin 11 | * )----------------| 220 ohm Resistor |-------| Green LED|----. * * * GND<---------------. * | * (R) 10K ohms * Analog | * )<------------------. [ VOLTAGE DIVIDER ] * Pin 4 | * (0-1023) - | * (LDR) 0-20K ohms * | * | * +5V ---------------->. * * * RGB_LED_Fade9_10_11 when LDR Voltage Bridge center leg * dops below a "Trigger" value, YOU specify, due to a * shadow blocking the amount of light getting to the LDR * Code for cross-fading a tri-color * RGB LED, using PWM * The program cross-fades slowly from red to green, green to blue, and blue to red * The debugging code assumes Arduino 0004, as it uses the new Serial.begin()-style functions * originally "dimmingLEDs" by Clay Shirky * * */ // // Put your Variables up here - BEFORE "void setup()" // // Components' input and output pin number identification // int LDRpin = 4; // Center Leg of LDR voltage bridge int redPin = 9; // Red LED, connected to digital pin 9 int greenPin = 10; // Green LED, connected to digital pin 10 int bluePin = 11; // Blue LED, connected to digital pin 11 // // Program variables set as integer (number) type // int redLEDValue = 255; // Variables to store the values to send to the pins int greenLEDValue = 1; // Initial values are Red full, Green and Blue off int blueLEDValue = 1; // These values get passed to the analogWrite() function. int i = 0; // Loop counter // int Trigger = 850; // This is the LDR "trigger" value // should be between 0 and 1023 // 800 is about the amount of light from // a desk lamp shinging on the LDR from 2 feet. // 600-650 is about ambient light level in // window lit room // tweek "Trigger" value to tune for your lighint // conditions // int LDRval = 0; // LDRval is the reading of the center leg of // the LDR voltage bridge circuit void setup() { pinMode(redPin, OUTPUT); // sets the pins as output pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600); // comment this out after debugging } void loop () { Serial.print(" Read Sensor. val = "); int LDRval = analogRead(4); Serial.print(LDRval); if(LDRval <= Trigger) { // // Put the code for the EVENT you want "triggered" here // ================ { i += 2; // Increment counter if (i < 255) // First phase of fades { redLEDValue -= 1; // Red down greenLEDValue += 1; // Green up blueLEDValue = 1; // Blue low } else if (i < 509) // Second phase of fades { redLEDValue = 1; // Red low greenLEDValue -= 1; // Green down blueLEDValue += 1; // Blue up } else if (i < 763) // Third phase of fades { redLEDValue += 1; // Red up greenLEDValue = 1; // Green low blueLEDValue -= 1; // Blue down } else // Re-set the counter, and start the fades again { i = 1; } // analogWrite() expects 2 parameters, the object/pin and a value between 0 (off) and 255 (full on) analogWrite(redPin, redLEDValue); // Write current values to LED pins analogWrite(greenPin, greenLEDValue); analogWrite(bluePin, blueLEDValue); Serial.print("redLEDValue = "); Serial.print(redLEDValue); Serial.print(" greenLEDValue = "); Serial.print(greenLEDValue); Serial.print(" blueLEDValue = "); Serial.println(blueLEDValue); delay(10); // Pause for xx milliseconds before resuming the loop } // ================================================ // *********************************************** // end of primary loop beginning of secondary loop // *********************************************** // ================================================ } Serial.println(" Waiting for Trigger Event "); analogWrite(redPin, 0); // Turn RGB LEDs OFF analogWrite(greenPin, 0); analogWrite(bluePin, 0); delay(10); }