Advertisement
manvi_m

Nightlight Code

Jun 14th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 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. // put your main code here, to run repeatedly:
  41. lightLevel = analogRead (pinPhoto);
  42. brightness = map(lightLevel, 1, 240, 255, 0);
  43. //NEW FUNCTION: map -- map (variable, minimum of variable, maximum of variable, corresponding value to minimum of variable, corresponding valuable to maximum of variable)
  44. brightness = constrain (brightness, 0, 255);
  45. // NEW FUNCTION: constrain (variable, min, max)
  46.  
  47. analogWrite (pinLedRed, brightness);
  48.  
  49. Serial.print (" Light Level = ");
  50. Serial.print (lightLevel);
  51. Serial.print (" Brightness = ");
  52. Serial.println (brightness);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement