Advertisement
evenjc

Untitled

Oct 19th, 2020
2,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int lightResistor;
  2.  
  3. bool calibrate_is_finished = false;
  4.  
  5. int threshold;
  6.  
  7. void setup() {
  8.  
  9.   threshold = calibrateLightSensor(lightResistor);
  10. }
  11.  
  12. void loop() {
  13.  
  14.   if (checkThreshold(readSensor, 120)) {
  15.     //Lightshow
  16.   }
  17.  
  18. }
  19.  
  20. int readSensor(int sensorPin) {
  21.   return analogRead(sensorPin);
  22. }
  23. int calibrateLightSensor(int sensorPin) {
  24.   int maxValue = 0;
  25.   int minValue = 1023;
  26.    while (!calibrate_is_finished) {
  27.        int readValue = readSensor(lightResistor);
  28.  
  29.        if (readValue > maxValue) maxValue = readValue;
  30.        if (readValue < maxValue) minValue = readValue;
  31.    }
  32. }
  33.  
  34. bool checkThreshold(int value, int threshold) {
  35.   if (value > threshold) {
  36.     return true;
  37.   }
  38.   else if (value < threshold) {
  39.     return false;
  40.   }
  41. }
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement