Guest User

Untitled

a guest
Jun 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. /*
  2. Pulse It
  3.  
  4. Pulse an LED, with intensity increasing and decreasing
  5. at a frequency that corresponds to the light level
  6. that the photocell senses.
  7.  
  8. LED intensity ranges from 0-255 from the bottom of the pulse to the top.
  9.  
  10. LED pulse delay ranges from 0 to the
  11. light level reading as delay in milliseconds.
  12.  
  13. Created by Corinna Sherman
  14. 23 Sep 2010
  15. */
  16.  
  17. int sensorPin = 0; // analog input pin for the photocell
  18. int ledPin = 11; // analog output (PWM) pin for the LED
  19.  
  20. int sensorValue = 0; // variable to hold the current sensor value
  21. int maxSensorValue = 0; // variable to hold the maximum sensor value
  22. int minSensorValue = 0; // variable to hold the minimum sensor value
  23.  
  24. int fadeInterval = 10; // variable to hold the rate of LED fading in or out in milliseconds
  25.  
  26. int pulseDelay = 0; // variable to hold the delay between fading and fading out in milliseconds
  27. int minPulseDelay = 0; // variable to hold the minimum delay between fading and fading out in milliseconds
  28. int maxPulseDelay = 800; // variable to hold the maximum delay between fading and fading out in milliseconds
  29.  
  30. String output; // variable to hold the String output: (current max min)
  31. String space; // space character
  32. String pulseDelayLabel; // label for pulse delay printout
  33.  
  34. void setup() {
  35.  
  36. // Initialize the serial communication.
  37. Serial.begin(9600);
  38.  
  39. // Initialize the sensor value.
  40. sensorValue = maxSensorValue = analogRead(sensorPin);
  41.  
  42. // Initialize strings for output.
  43. output = String();
  44. pulseDelayLabel = String("pulseDelay = ");
  45. }
  46.  
  47. void loop() {
  48.  
  49. // Read the value from the sensor.
  50. sensorValue = analogRead(sensorPin);
  51.  
  52. // Update the max and min sensor values if applicable.
  53. if (sensorValue > maxSensorValue) {
  54. maxSensorValue = sensorValue;
  55. }
  56. else if (sensorValue < minSensorValue) {
  57. minSensorValue = sensorValue;
  58. }
  59.  
  60. // Print the current, min, and max sensor values.
  61. output = sensorValue + space + minSensorValue + space + maxSensorValue;
  62. Serial.println(output);
  63.  
  64. // Calculate the pulse delay using the current light level as seen by the sensor.
  65. // The max and min pulse delay are reversed in the map function to achieve
  66. // a slower pulse when the light level is low and
  67. // a faster pulse when the light level is high.
  68. pulseDelay = map(sensorValue, minSensorValue, maxSensorValue, maxPulseDelay, minPulseDelay);
  69. Serial.println(pulseDelayLabel + pulseDelay);
  70.  
  71. // Pulse the LED with a frequency proportional to the light level sensed.
  72. fadeIn();
  73. delay(pulseDelay);
  74. fadeOut();
  75. delay(pulseDelay);
  76. }
  77.  
  78. void fadeIn() {
  79.  
  80. // Fade the LED intensity from 0 to 255 in increments of 5 points.
  81. for (int fadeValue = 0; fadeValue <= 255; fadeValue +=5) { analogWrite(ledPin, fadeValue); delay(fadeInterval); // Wait some interval to see the dimming effect. } }
  82.  
  83. void fadeOut() {
  84.  
  85. // Fade the LED intensity from 250 to 0 in decrements of 5 points.
  86.  
  87. for (int fadeValue = 250; fadeValue >= 0; fadeValue -=5) {
  88. analogWrite(ledPin, fadeValue);
  89. delay(fadeInterval); // Wait some interval to see the dimming effect.
  90. }
  91.  
  92. }
Add Comment
Please, Sign In to add comment