Advertisement
Guest User

Light Changer Photoresistor

a guest
Sep 18th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. int photoResistor = A4;
  2. const int BlueBoy = 3;
  3. const int RedBoy = 11;
  4. const int GreenBoy = 5;
  5. const int LED1 = 9;
  6. const int Button = 12;
  7. int buttonRead;
  8. int photoVal;
  9.  
  10. unsigned long timecount;
  11. int minVal = 1000;
  12. int maxVal = 0;
  13. bool currentValue = false;
  14. bool lastValue = false;
  15.  
  16.  
  17. void setup() {
  18. // put your setup code here, to run once:
  19. Serial.begin(9600);
  20. pinMode (Button, INPUT);
  21. pinMode (photoResistor, INPUT);
  22. pinMode (BlueBoy, OUTPUT);
  23. pinMode (RedBoy, OUTPUT);
  24. pinMode (GreenBoy, OUTPUT);
  25. pinMode (LED1, OUTPUT);
  26. calibrate();
  27. }
  28.  
  29. void calibrate() {
  30. digitalWrite(LED1, HIGH);
  31. delay(50);
  32. digitalWrite(LED1, LOW);
  33. timecount = millis();
  34. while (millis() - timecount < 5000) {
  35. photoVal = analogRead(photoResistor);
  36. if (photoVal > maxVal) {
  37. maxVal = photoVal;
  38. }
  39. if (photoVal < minVal) {
  40. minVal = photoVal;
  41. }
  42. }
  43. }
  44.  
  45. void loop() {
  46. buttonRead = analogRead(Button);
  47. boolean current = digitalRead(Button);
  48. if (lastValue != current)
  49. {
  50. delay(5);
  51. currentValue = digitalRead(Button);
  52. }
  53. if (lastValue == LOW && currentValue == HIGH){
  54. calibrate();
  55. }
  56. photoVal = analogRead(photoResistor);
  57. Serial.println(photoVal);
  58. photoVal = map(photoVal, minVal, maxVal, 255, 0);
  59. photoVal = constrain(photoVal, 0, 255);
  60. analogWrite(BlueBoy, photoVal);
  61. analogWrite(RedBoy, photoVal);
  62. analogWrite(GreenBoy, photoVal);
  63. lastValue = currentValue;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement