Guest User

Untitled

a guest
Jun 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. /*
  2. Show It
  3. Demonstrates analog input by reading a light sensor on analog pin 0.
  4.  
  5. Also records the minimum and maximum sensor values that the sensor sees.
  6. Whenever the program reads an analog sensor value that is smaller than the
  7. current minimum, it replaces the recorded minimum with the new value.
  8. Likewise for the maximum value.
  9.  
  10. Prints out in the form:
  11. current minimum maximum
  12.  
  13. Lights an LED with an intensity proportional to the light level
  14. that the photocell senses, with LED intensity ranging from 0-255.
  15.  
  16. Created by Corinna Sherman
  17. 22 Sep 2010
  18. */
  19.  
  20. int sensorPin = 0; // analog input pin for the photocell
  21. int ledPin = 11; // analog output (PWM) pin for the LED
  22.  
  23. int sensorValue = 0; // variable to hold the current sensor value
  24. int maxSensorValue = 0; // variable to hold the maximum sensor value
  25. int minSensorValue = 0; // variable to hold the minimum sensor value
  26.  
  27. String output; // variable to hold the String output: (current max min)
  28. String space; // space character
  29. String intensityLabel; // label for intensity printout
  30.  
  31. void setup() {
  32.  
  33. // Initialize the serial communication.
  34. Serial.begin(9600);
  35.  
  36. // Initialize the sensor value.
  37. sensorValue = maxSensorValue = minSensorValue = analogRead(sensorPin);
  38.  
  39. // Initialize strings for output.
  40. output = String();
  41. space = String(" ");
  42. intensityLabel = String("ledIntensity = ");
  43. }
  44.  
  45. void loop() {
  46.  
  47. // Read the value from the sensor.
  48. sensorValue = analogRead(sensorPin);
  49.  
  50. // Update the max and min sensor values if applicable.
  51. if (sensorValue > maxSensorValue) {
  52. maxSensorValue = sensorValue;
  53. } else if (sensorValue < minSensorValue) {
  54. minSensorValue = sensorValue;
  55. }
  56.  
  57. // Print the current, min, and max sensor values.
  58. output = sensorValue + space + minSensorValue + space + maxSensorValue;
  59. Serial.println(output);
  60.  
  61. // Light the LED with intensity proportional to the light level sensed,
  62. // ranging from 0 to 255.
  63. int ledIntensity = map(sensorValue, minSensorValue, maxSensorValue, 0, 255);
  64. analogWrite(ledPin, ledIntensity);
  65. Serial.println(intensityLabel + ledIntensity);
  66. }
Add Comment
Please, Sign In to add comment