Advertisement
iyera20

Night Light with Button

Jun 15th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. /*
  2. * Atiriya Iyer
  3. * Photocell Tests
  4. * 6-15-17
  5. * Version 2
  6. */
  7.  
  8. const int pinPhoto= A0;
  9. int lightLevel =0;
  10. const int pinLEDblue= 9;
  11. const int pinLEDgreen= 10;
  12. const int pinLEDred= 11;
  13. const int pinButton=2;
  14. int brightness= 0;
  15. int lastButt= 0;
  16. int newButt= 0;
  17. unsigned long timeStart= 0; //long can hold a much larger number and unsigned is only positive
  18. int minimum= 5000;
  19. int maximum= -5000;
  20.  
  21.  
  22. void setup()
  23. {
  24. // put your setup code here, to run once:
  25. pinMode (pinPhoto, INPUT);
  26. pinMode (pinLEDblue, OUTPUT);
  27. pinMode (pinLEDgreen, OUTPUT);
  28. pinMode (pinLEDred, OUTPUT);
  29. pinMode (pinButton, INPUT);
  30.  
  31. Serial.begin (9600);
  32.  
  33. analogWrite (pinLEDblue, 255);
  34. delay(500);
  35. analogWrite (pinLEDblue, 0);
  36. delay(500);
  37. analogWrite (pinLEDgreen, 255);
  38. delay(500);
  39. analogWrite (pinLEDgreen, 0);
  40. delay(500);
  41. analogWrite (pinLEDred, 255);
  42. delay(500);
  43. analogWrite (pinLEDred, 0);
  44. }
  45.  
  46. void loop()
  47. {
  48. // put your main code here, to run repeatedly:
  49. lightLevel= analogRead (pinPhoto);
  50. brightness= map(lightLevel, minimum, maximum, 255, 0);
  51. //established proportions, uses values we know to values we need
  52. brightness= constrain(brightness, 0,255);
  53.  
  54. analogWrite (pinLEDblue, brightness);
  55. analogWrite (pinLEDred, brightness);
  56.  
  57. //if (brightness > minimum && brightness < (maximum-minimum)/5)
  58. // {
  59. //analogWrite (pinLEDgreen, 50/255*brightness);
  60. //analogWrite (pinLEDred, 50/255*brightness);
  61. //analogWrite (pinLEDblue, 255/255*brightness);
  62. //}
  63.  
  64. // analogWrite (pinLEDblue, brightness/2);
  65. // analogWrite (pinLEDred, brightness/2);
  66. // analogWrite (pinLEDgreen, brightness/2);
  67.  
  68. newButt= debounce(lastButt);
  69. if (newButt== HIGH && lastButt== LOW)
  70. {
  71. Serial.println("Calibrating...");
  72. calibrate();
  73. }
  74. lastButt= newButt;
  75.  
  76. Serial.print ("Light level= ");
  77. Serial.println (lightLevel);
  78. Serial.print ("brightness= ");
  79. Serial.println (brightness);
  80. }
  81.  
  82. boolean debounce (boolean last)
  83. {
  84. boolean current = digitalRead(pinButton);
  85. if (last != current)
  86. {
  87. delay(5);
  88. current= digitalRead(pinButton);
  89. }
  90. return current;
  91. }
  92.  
  93. void calibrate()
  94. {
  95. analogWrite (pinLEDred, 255);
  96. analogWrite (pinLEDblue, 0);
  97. analogWrite (pinLEDgreen, 0);
  98. delay (200);
  99. analogWrite (pinLEDred, 0);
  100. analogWrite (pinLEDblue, 0);
  101. analogWrite (pinLEDgreen, 0);
  102.  
  103. timeStart= millis();
  104. while (millis()-timeStart < 5000)
  105. {
  106. lightLevel= analogRead (pinPhoto);
  107. if (lightLevel > maximum)
  108. {
  109. maximum= lightLevel;
  110. }
  111. else if (lightLevel < minimum)
  112. {
  113. minimum= lightLevel;
  114. }
  115. }
  116. Serial.println (maximum);
  117. Serial.println (minimum);
  118.  
  119. analogWrite (pinLEDred, 160);
  120. analogWrite (pinLEDblue, 0);
  121. analogWrite (pinLEDgreen, 100);
  122. delay (200);
  123. analogWrite (pinLEDred, 0);
  124. analogWrite (pinLEDblue, 0);
  125. analogWrite (pinLEDgreen, 0);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement