1#include <VarSpeedServo.h>
2VarSpeedServo servo;
3
4int easternLight = 0;
5int westernLight = 0;
6int rainSensed = 0;
7int sunshadePosition = 0;
8int rainThreshold = 512;
9int sunshadeExpanded = 120;
10int sunshadeContracted = 60;
11int ambientLighting = 0;
12int turnOnLightsOn = 150;
13int ledIntensity = 0;
14int ledPower = 0;
15int servoSpeed = 20;
16bool debugging = false;
17
18void setup()
19{
20 pinMode(A4, OUTPUT);
21 pinMode(A5, OUTPUT);
22 pinMode(A1, INPUT);
23 pinMode(A2, INPUT);
24 pinMode(A3, INPUT);
25 pinMode(2, INPUT);
26 servo.attach(A0);
27 servo.write(sunshadeContracted, servoSpeed, true);
28 if (debugging){
29 Serial.begin(9600);
30 Serial.println("*** DEBUGGING STARTED ***");
31 Serial.println("-------------------------");
32 }
33}
34
35void oya_mydebug(String text, int value = NULL)
36{
37
38
39
40 if (debugging){
41 Serial.print(text);
42 if (value){
43 Serial.print(" = ");
44 Serial.println(value);
45 } else {
46 Serial.println(" ");
47 }
48 }
49}
50
51void expand_collapse(int easternLight, int westernLight)
52{
53 if (easternLight <= westernLight) {
54
55 oya_mydebug("It's sunrise");
56 if (sunshadePosition != sunshadeExpanded) {
57 servo.write(sunshadeExpanded, servoSpeed, true);
58 oya_mydebug("Sunshade expanded");
59 }
60 }
61 if (easternLight > westernLight) {
62
63 oya_mydebug("Sun is goind down");
64 if (sunshadePosition != sunshadeContracted) {
65 servo.write(sunshadeContracted, servoSpeed, true);
66 oya_mydebug("Sunshade contracted");
67 }
68 }
69}
70
71void loop()
72{
73
74 easternLight = analogRead(A2);
75 westernLight = analogRead(A1);
76 rainSensed = analogRead(A3);
77 sunshadePosition = servo.read();
78 ambientLighting = ((easternLight + westernLight) / 2);
79
80 oya_mydebug("easternLight", easternLight);
81 oya_mydebug("westernLight", westernLight);
82 oya_mydebug("rainSensed", rainSensed);
83 oya_mydebug("sunshadePosition", sunshadePosition);
84 oya_mydebug("ambientLighting", ambientLighting);
85
86
87 if (rainSensed < rainThreshold) {
88
89 oya_mydebug("It's raining...");
90 if (sunshadePosition != sunshadeExpanded) {
91 servo.write(sunshadeExpanded, servoSpeed, true);
92 oya_mydebug("Sunshade expanded");
93 }
94 } else {
95
96 oya_mydebug("It is not raining...");
97 if (ambientLighting <= turnOnLightsOn) {
98
99 oya_mydebug("It's getting dark, turning on the lights");
100 ledPower = ((turnOnLightsOn - ambientLighting) * 4);
101 oya_mydebug("ledPower", ledPower);
102 analogWrite(A5, ledPower);
103 analogWrite(A4, ledPower);
104 expand_collapse(easternLight, westernLight);
105 } else {
106
107 oya_mydebug("A lot of sunshine, turn off lights");
108 analogWrite(A5, 0);
109 analogWrite(A4, 0);
110 expand_collapse(easternLight, westernLight);
111 }
112 }
113 if (debugging){
114 Serial.println("********************");
115 Serial.println(" ");
116 delay(5000);
117 } else {
118 delay(100);
119 }
120}