Advertisement
ChristineWu

Button Photoresistor

Dec 8th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 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. const int ButtonPin=12;
  6. int ButtonValue, ReadValue=0;
  7. int MinValue=1000;
  8. int MaxValue=0;
  9. //these two int max and min;
  10. unsigned long MyTime;
  11. bool Check;
  12.  
  13. void calibrate ()
  14. {
  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. }
  31.  
  32. void setup()
  33. {
  34. pinMode (PhotoPin, INPUT);
  35. pinMode (LEDPin,OUTPUT);
  36. pinMode (ButtonPin,INPUT);
  37. Serial.begin(9600);
  38. //serial initializes it
  39. // put your setup code here, to run once:
  40.  
  41. }
  42.  
  43. void loop()
  44. {
  45. ButtonValue=digitalRead(ButtonPin);
  46. if (Check)
  47. {
  48. if (ButtonValue==1)
  49. {
  50. calibrate();
  51. }
  52. }
  53. else
  54. {
  55. calibrate();
  56. Check=true;
  57. }
  58. ReadValue=analogRead(PhotoPin);
  59. ReadValue=map (ReadValue, MinValue, MaxValue,255,0);
  60. ReadValue=constrain(ReadValue, 0, 255);
  61. //constrain helps make sure it doesnt flicker anymore
  62. //these numbers indicate min (0) and max (170) and it inserts it into the readvalue)
  63. Serial.println(ReadValue);
  64. analogWrite (LEDPin, ReadValue);
  65. // put your main code here, to run repeatedly:
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement