JNET

Photoresistor - Re-calibration

Dec 8th, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. const int PhotoR = A0;
  2. const int LEDpin = 6;
  3. int ReadValue = 0;
  4. unsigned long Time;
  5. int MinVal = 1000;
  6. int MaxVal = 0;
  7. const int Button = 3;
  8. int ButtonVal = 0;
  9. boolean check = false;
  10.  
  11. void setup()
  12. {
  13. pinMode (PhotoR, INPUT);
  14. pinMode (LEDpin, OUTPUT);
  15. pinMode (Button, INPUT);
  16. Serial.begin(9600);
  17. }
  18.  
  19. void calibrate()
  20. {
  21. Serial.println("in calibrate");
  22. digitalWrite (LEDpin, HIGH);
  23. Time = millis();
  24. while (millis() - Time <=5000)
  25. {
  26. ReadValue = analogRead(PhotoR);
  27. if (ReadValue < MinVal)
  28. {
  29. MinVal = ReadValue;
  30. }
  31. if (ReadValue > MaxVal)
  32. {
  33. MaxVal = ReadValue;
  34. }
  35. }
  36. digitalWrite(LEDpin, LOW);
  37. }
  38.  
  39.  
  40. void loop()
  41. {
  42. ButtonVal = digitalRead (Button);
  43. if (check)
  44. {
  45. Serial.println("Check is true");
  46. if (ButtonVal == 1)
  47. {
  48. Serial.println("Button is on");
  49. calibrate();
  50. }
  51. }
  52. else
  53. {
  54. Serial.println("Check is false");
  55. calibrate();
  56. check = true;
  57. }
  58. ReadValue = analogRead(PhotoR);
  59. ReadValue = map(ReadValue, MinVal, MaxVal, 255, 0);
  60. ReadValue = constrain(ReadValue, 0, 255);
  61. Serial.println(ReadValue);
  62. analogWrite(LEDpin, ReadValue);
  63. }
Add Comment
Please, Sign In to add comment