Created
October 13, 2010 17:11
-
-
Save corinnas/624460 to your computer and use it in GitHub Desktop.
Analog Inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Show It | |
Demonstrates analog input by reading a light sensor on analog pin 0. | |
Also records the minimum and maximum sensor values that the sensor sees. | |
Whenever the program reads an analog sensor value that is smaller than the | |
current minimum, it replaces the recorded minimum with the new value. | |
Likewise for the maximum value. | |
Prints out in the form: | |
current minimum maximum | |
Lights an LED with an intensity proportional to the light level | |
that the photocell senses, with LED intensity ranging from 0-255. | |
Created by Corinna Sherman | |
22 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 | |
String output; // variable to hold the String output: (current max min) | |
String space; // space character | |
String intensityLabel; // label for intensity printout | |
void setup() { | |
// Initialize the serial communication. | |
Serial.begin(9600); | |
// Initialize the sensor value. | |
sensorValue = maxSensorValue = minSensorValue = analogRead(sensorPin); | |
// Initialize strings for output. | |
output = String(); | |
space = String(" "); | |
intensityLabel = String("ledIntensity = "); | |
} | |
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); | |
// Light the LED with intensity proportional to the light level sensed, | |
// ranging from 0 to 255. | |
int ledIntensity = map(sensorValue, minSensorValue, maxSensorValue, 0, 255); | |
analogWrite(ledPin, ledIntensity); | |
Serial.println(intensityLabel + ledIntensity); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment