ChristineWu

Calibrate Photoresistor

Dec 7th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. //const int Button=7;
  2. const int PhotoPin=A0;
  3. //this way, it behaves like analog instead of other stuff
  4. const int LEDPin=5;
  5. int ReadValue=0;
  6. int MinValue=1000;
  7. int MaxValue=0;
  8. //these two int max and min;
  9. unsigned long MyTime;
  10. void setup()
  11. {
  12. pinMode (PhotoPin, INPUT);
  13. Serial.begin(9600);
  14. //serial initializes it
  15. digitalWrite(LEDPin,HIGH);
  16. MyTime=millis();
  17. while (millis()-MyTime<=5000)
  18. {
  19. ReadValue=analogRead(PhotoPin);
  20. if (ReadValue<MinValue)
  21. {
  22. MinValue=ReadValue;
  23. }
  24. if (ReadValue>MaxValue)
  25. {
  26. MaxValue=ReadValue;
  27. }
  28. }
  29. digitalWrite(LEDPin,LOW);
  30. // put your setup code here, to run once:
  31.  
  32. }
  33.  
  34. void loop()
  35. {
  36. ReadValue=analogRead(PhotoPin);
  37. ReadValue=map (ReadValue, MinValue, MaxValue,255,0);
  38. ReadValue=constrain(ReadValue, 0, 255);
  39. //constrain helps make sure it doesnt flicker anymore
  40. //these numbers indicate min (0) and max (170) and it inserts it into the readvalue)
  41. Serial.println(ReadValue);
  42. analogWrite (LEDPin, ReadValue);
  43. // put your main code here, to run repeatedly:
  44.  
  45. }
Add Comment
Please, Sign In to add comment