servo.ino (819B)
1 #include <Servo.h> 2 3 /* photo resistor values tested and found on tinkercad */ 4 #define LIGHT_NONE 640 5 #define LIGHT_FULL 33 6 #define SEVENTY_PERC (180 * (70 / (float)100)) 7 8 Servo servo; 9 10 void 11 setup() 12 { 13 servo.attach(9); 14 Serial.begin(9600); 15 } 16 17 void 18 loop() 19 { 20 int pr_out, pr_in, pos = 0; 21 22 pr_out = analogRead(A0); /* exterior photoresistor */ 23 pr_in = analogRead(A1); /* interior photoresistor */ 24 25 if (pr_out == LIGHT_NONE) { 26 pos = 0; 27 servo_write_and_delay(pos); 28 Serial.println("night"); 29 } else if (pr_out < pr_in) { 30 pos = map(pr_out, LIGHT_NONE, LIGHT_FULL, 0, 180); 31 servo_write_and_delay(pos); 32 Serial.println("day"); 33 } else { 34 pos = SEVENTY_PERC; 35 servo_write_and_delay(pos); 36 Serial.println("peak"); 37 } 38 Serial.println(pos); 39 } 40 41 void 42 servo_write_and_delay(int pos) 43 { 44 servo.write(pos); 45 delay(15); 46 }