ambersy314

Untitled

Jun 15th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. const int photoPin=A0;
  2. int photoValue=0;
  3. int lightVal=0;
  4. const int PinRed=9;
  5. const int PinGreen=10;
  6. const int PinBlue=11;
  7. const int pinButt=6;
  8. int newButt=0;
  9. int lastButt=0;
  10. int minimum = 1000;
  11. int maximum = -1000;
  12. unsigned long timeVal;
  13.  
  14. void setup()
  15. {
  16. // put your setup code here, to run once:
  17.  
  18. Serial.begin(9600);
  19. pinMode(photoPin, INPUT);
  20. pinMode (PinRed, OUTPUT);
  21. pinMode (PinBlue, OUTPUT);
  22. pinMode(PinGreen, OUTPUT);
  23. analogWrite(PinRed, 255);
  24. analogWrite(PinGreen, 0);
  25. analogWrite(PinBlue, 0);
  26. delay (500);
  27. analogWrite(PinRed, 0);
  28. analogWrite(PinGreen, 255);
  29. analogWrite(PinBlue, 0);
  30. delay(500);
  31. analogWrite(PinRed, 0);
  32. analogWrite(PinGreen, 0);
  33. analogWrite(PinBlue, 255);
  34. }
  35. void calibrate ()
  36. {
  37. minimum = 1000;
  38. maximum = -1000;
  39. analogWrite (PinGreen, HIGH);
  40. timeVal =millis();
  41. while (millis ()-timeVal<5000)
  42. {
  43. photoValue = analogRead(photoPin);
  44. if (photoValue > maximum)
  45. {
  46. maximum = photoValue;
  47. }
  48. else if (photoValue < minimum)
  49. {
  50. minimum = photoValue;
  51. }
  52. }
  53. Serial.println(minimum);
  54. Serial.println(maximum);
  55. digitalWrite (PinGreen, LOW);
  56. }
  57. boolean debounce (boolean last)
  58. {
  59. boolean current = digitalRead(pinButt);
  60. if (last != current)
  61. {
  62. delay(5);
  63. current = digitalRead(pinButt);
  64. }
  65. return current;
  66. }
  67.  
  68. void loop()
  69. {
  70. // put your main code here, to run repeatedly:
  71. newButt = debounce (lastButt);
  72. if(newButt==HIGH && lastButt ==LOW)
  73. {
  74. calibrate();
  75. }
  76. lastButt=newButt;
  77.  
  78. photoValue=analogRead (photoPin);
  79. lightVal=map (photoValue, minimum, maximum, 255, 0);
  80. lightVal=constrain (lightVal, 0, 255);
  81. analogWrite (PinRed, lightVal);
  82. analogWrite (PinGreen,lightVal);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment