Advertisement
belrey10

Lesson #6: Photoresistor Night Light

Nov 4th, 2024 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************
  2.  * File: Photoresistor V1
  3.  *
  4.  * (Constantly Emmitting Night-Light)
  5.  *
  6.  * Constant LED light, where brightness is the inverse of
  7.  * the amount of light read in with photoresistor (or room).
  8.  *
  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(ContrainedMaxLightLevel - lightLevel); // prints to the "Serial Monitor" button in top right
  33.   analogWrite(ledPin, ContrainedMaxLightLevel - lightLevel);    // set the led level based on the input lightLevel.
  34. }
  35. /******************************************************************
  36.  * void calibrateToRoom()
  37.  *
  38.  * This function sets a minThreshold and maxThreshold value for the
  39.  * light levels in your setting. Move your hand / light source / etc
  40.  * so that your light sensor sees a full range of values. This will
  41.  * "autoCalibrate" to your range of input values.
  42. /*****************************************************************/
  43.  
  44. void calibrateToRoom()
  45. {
  46.   if (lightLevel < minThreshold)  // minThreshold was initialized to 1023 -- so, if it's less, reset the threshold level.
  47.     minThreshold = lightLevel;
  48.  
  49.   if (lightLevel > maxThreshold)  // maxThreshold was initialized to 0 -- so, if it's bigger, reset the threshold level.
  50.     maxThreshold = lightLevel;
  51.  
  52.   // Once we have the highest and lowest values, we can stick them
  53.   // directly into the map() function.
  54.   // This function must run a few times to get a good range of bright and dark values in order to work.
  55.  
  56.   lightLevel = map(lightLevel, minThreshold, maxThreshold, 0, ContrainedMaxLightLevel);
  57.   lightLevel = constrain(lightLevel, 0, ContrainedMaxLightLevel);
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement