belrey10

Lesson #6: Photoresistor Night Light v2

Nov 4th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. /*******************************************************************
  2. * File: Photoresistor V2
  3. *
  4. * (Standard Night-Light)
  5. *
  6. * Non-Constant LED light, where it is max brightness if
  7. * the amount of light read in with photoresistor (or room)
  8. * is less than a specific value (128 in our case).
  9. *
  10. /******************************************************************/
  11. // Start by naming the pins we will use
  12. const int sensorPin = 0;
  13. const int ledPin = 9;
  14. const int ContrainedMaxLightLevel = 255;
  15.  
  16. // We'll also set up some global variables for the light level:
  17. int lightLevel;
  18. int maxThreshold = 0; // used for setting the "max" light level
  19. int minThreshold = 1023; // used for setting the "min" light level
  20.  
  21. void setup()
  22. {
  23. pinMode(ledPin, OUTPUT); // Set up the LED pin to be an output.
  24. Serial.begin(9600); // 9600 is the baud rate, used in the Serial Monior to read light levels.
  25. }
  26.  
  27. void loop()
  28. {
  29. lightLevel = analogRead(sensorPin); // reads the voltage on the sensorPin
  30. calibrateToRoom(); // sets the the min / max values for light value ranges you see in your room.
  31. Serial.print("\t"); // tab character for better output formatting
  32. Serial.print(lightLevel); // prints to the "Serial Monitor" button in top right
  33.  
  34. if(lightLevel < ContrainedMaxLightLevel/2){
  35. analogWrite(ledPin, ContrainedMaxLightLevel); // turn LED on
  36. }
  37. else{
  38. analogWrite(ledPin, 0); // turn LED off
  39. }
  40. }
  41. /******************************************************************
  42. * void calibrateToRoom()
  43. *
  44. * This function sets a minThreshold and maxThreshold value for the
  45. * light levels in your setting. Move your hand / light source / etc
  46. * so that your light sensor sees a full range of values. This will
  47. * "autoCalibrate" to your range of input values.
  48. /*****************************************************************/
  49.  
  50. void calibrateToRoom()
  51. {
  52. if (lightLevel < minThreshold) // minThreshold was initialized to 1023 -- so, if it's less, reset the threshold level.
  53. minThreshold = lightLevel;
  54.  
  55. if (lightLevel > maxThreshold) // maxThreshold was initialized to 0 -- so, if it's bigger, reset the threshold level.
  56. maxThreshold = lightLevel;
  57.  
  58. // Once we have the highest and lowest values, we can stick them
  59. // directly into the map() function.
  60. // This function must run a few times to get a good range of bright and dark values in order to work.
  61.  
  62. lightLevel = map(lightLevel, minThreshold, maxThreshold, 0, ContrainedMaxLightLevel);
  63. lightLevel = constrain(lightLevel, 0, ContrainedMaxLightLevel);
  64. }
Add Comment
Please, Sign In to add comment