Advertisement
manvi_m

nightlight changes as environment changes

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