adambkehl

Untitled

Mar 7th, 2019
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #define PCELL1_PIN A1
  2. #define LED_PIN 9
  3. #define POT_PIN A0
  4. #define DELAY 525
  5.  
  6. double lightValue, photocellValue;
  7. bool currState = false, prevState = false, changed = false;
  8.  
  9. bool dark(){
  10.   return photocellValue < lightValue;
  11. }
  12.  
  13. void setup() {
  14.   Serial.begin(9600);
  15.   analogWrite(LED_PIN, 0);
  16. }
  17.  
  18. void loop() {
  19.   photocellValue = analogRead(PCELL1_PIN);
  20.   lightValue = analogRead(POT_PIN);
  21.   Serial.print("Photocell:     "); Serial.println(photocellValue);
  22.   Serial.print("Potentiomater: "); Serial.println(lightValue);
  23.  
  24.   fadeTrigger();
  25.   checkPhotoCell();
  26. }
  27.  
  28. void fade(int value){
  29.   if (value == 1){
  30.     for (int i = 0; i < 255; i++) {
  31.       analogWrite(LED_PIN, i+1);
  32.       delay(DELAY/255);
  33.      
  34.     }
  35.   } else {
  36.     for (int i = 255; i >= 0; i--) {
  37.       analogWrite(LED_PIN, i);
  38.       delay(DELAY/255);
  39.      
  40.     }
  41.   }
  42. }
  43.  
  44. void checkPhotoCell(){
  45.   if (dark()) currState = true;
  46.   if (!dark()) currState = false;
  47.   if (currState != prevState) changed = true;
  48. }
  49.  
  50. void fadeTrigger(){
  51.   if (changed){
  52.     changed = false;
  53.     if (currState){
  54.       fade(1);//fade in
  55.     } else {
  56.       fade(0);//fade out
  57.     }
  58.     prevState = currState;
  59.   }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment