Advertisement
manvi_m

nightlight with non primary colors

Jun 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 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. int lightLevel = 0;
  11. int brightness = 0;
  12.  
  13. void setup() {
  14. // put your setup code here, to run once:
  15. pinMode (pinPhoto, INPUT);
  16. pinMode (pinLedRed, OUTPUT);
  17. pinMode (pinLedGreen, OUTPUT);
  18. pinMode (pinLedBlue, OUTPUT);
  19. Serial.begin (9600);
  20.  
  21. analogWrite (pinLedRed, 100);
  22. delay (500);
  23. analogWrite (pinLedRed, 0);
  24. delay (500);
  25.  
  26. analogWrite (pinLedGreen, 100);
  27. delay (500);
  28. analogWrite (pinLedGreen, 0);
  29. delay (500);
  30.  
  31. analogWrite (pinLedBlue, 100);
  32. delay (500);
  33. analogWrite (pinLedBlue, 0);
  34. delay (500);
  35.  
  36.  
  37. }
  38.  
  39. void loop() {
  40.  
  41. // put your main code here, to run repeatedly:
  42. lightLevel = analogRead (pinPhoto);
  43. brightness = map(lightLevel, 1, 150, 255, 0);
  44. //NEW FUNCTION: map -- map (variable, minimum of variable, maximum of variable, corresponding value to minimum of variable, corresponding valuable to maximum of variable)
  45. brightness = constrain (brightness, 0, 255);
  46. // NEW FUNCTION: constrain -- constrain (variable, min, max) -- so that there are no negative numbers
  47.  
  48. analogWrite (pinLedRed, brightness/2);
  49. analogWrite (pinLedBlue, brightness*3);
  50. analogWrite (pinLedGreen, brightness/20);
  51.  
  52. Serial.print (" Light Level = ");
  53. Serial.print (lightLevel);
  54. Serial.print (" Brightness = ");
  55. Serial.println (brightness);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement