Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Pulse It
- Pulse an LED, with intensity increasing and decreasing
- at a frequency that corresponds to the light level
- that the photocell senses.
- LED intensity ranges from 0-255 from the bottom of the pulse to the top.
- LED pulse delay ranges from 0 to the
- light level reading as delay in milliseconds.
- Created by Corinna Sherman
- 23 Sep 2010
- */
- int sensorPin = 0; // analog input pin for the photocell
- int ledPin = 11; // analog output (PWM) pin for the LED
- int sensorValue = 0; // variable to hold the current sensor value
- int maxSensorValue = 0; // variable to hold the maximum sensor value
- int minSensorValue = 0; // variable to hold the minimum sensor value
- int fadeInterval = 10; // variable to hold the rate of LED fading in or out in milliseconds
- int pulseDelay = 0; // variable to hold the delay between fading and fading out in milliseconds
- int minPulseDelay = 0; // variable to hold the minimum delay between fading and fading out in milliseconds
- int maxPulseDelay = 800; // variable to hold the maximum delay between fading and fading out in milliseconds
- String output; // variable to hold the String output: (current max min)
- String space; // space character
- String pulseDelayLabel; // label for pulse delay printout
- void setup() {
- // Initialize the serial communication.
- Serial.begin(9600);
- // Initialize the sensor value.
- sensorValue = maxSensorValue = analogRead(sensorPin);
- // Initialize strings for output.
- output = String();
- pulseDelayLabel = String("pulseDelay = ");
- }
- void loop() {
- // Read the value from the sensor.
- sensorValue = analogRead(sensorPin);
- // Update the max and min sensor values if applicable.
- if (sensorValue > maxSensorValue) {
- maxSensorValue = sensorValue;
- }
- else if (sensorValue < minSensorValue) {
- minSensorValue = sensorValue;
- }
- // Print the current, min, and max sensor values.
- output = sensorValue + space + minSensorValue + space + maxSensorValue;
- Serial.println(output);
- // Calculate the pulse delay using the current light level as seen by the sensor.
- // The max and min pulse delay are reversed in the map function to achieve
- // a slower pulse when the light level is low and
- // a faster pulse when the light level is high.
- pulseDelay = map(sensorValue, minSensorValue, maxSensorValue, maxPulseDelay, minPulseDelay);
- Serial.println(pulseDelayLabel + pulseDelay);
- // Pulse the LED with a frequency proportional to the light level sensed.
- fadeIn();
- delay(pulseDelay);
- fadeOut();
- delay(pulseDelay);
- }
- void fadeIn() {
- // Fade the LED intensity from 0 to 255 in increments of 5 points.
- for (int fadeValue = 0; fadeValue <= 255; fadeValue +=5) { analogWrite(ledPin, fadeValue); delay(fadeInterval); // Wait some interval to see the dimming effect. } }
- void fadeOut() {
- // Fade the LED intensity from 250 to 0 in decrements of 5 points.
- for (int fadeValue = 250; fadeValue >= 0; fadeValue -=5) {
- analogWrite(ledPin, fadeValue);
- delay(fadeInterval); // Wait some interval to see the dimming effect.
- }
- }
Add Comment
Please, Sign In to add comment