Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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);
- }
Add Comment
Please, Sign In to add comment