Advertisement
brownj

Untitled

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