p06_LightControlBreathingLED.ino 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. Demo code for Project 6 - Light Control Breathing LED
  3. This example shows the LED breathing in different speed
  4. based on the light sensed
  5. by Maker Studio
  6. http://makerstudio.cc
  7. */
  8. int ledPin = 9; // LED connected to digital pin 9
  9. int analogInPin = A0; // Light sensor pin
  10. int sensorValue = 0;// value read from the sensor
  11. void setup() {
  12. // nothing happens in setup
  13. }
  14. void loop() {
  15. // read the light sensor value:
  16. sensorValue = analogRead(analogInPin);
  17. // get the meaningful fading value
  18. int gapValue = map(sensorValue,10,1024,2,30);
  19. // fade in from min to max in increments of 5 points:
  20. for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
  21. // sets the value (range from 0 to 255):
  22. analogWrite(ledPin, fadeValue);
  23. // wait for some milliseconds to see the dimming effect
  24. delay(gapValue);
  25. }
  26. // fade out from max to min in increments of 5 points:
  27. for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
  28. // sets the value (range from 0 to 255):
  29. analogWrite(ledPin, fadeValue);
  30. // wait for some milliseconds to see the dimming effect
  31. delay(gapValue);
  32. }
  33. }