-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunTrackingMirror.ino
99 lines (77 loc) · 2.67 KB
/
sunTrackingMirror.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "src/sunPosition/sunpos.h"
#include "src/DS3231/DS3231.h"
#include <SoftwareSerial.h>
#include <Servo.h>
//Real time clock
DS3231 rtc(SDA, SCL);
//Bluetooth comms
SoftwareSerial bluetoothSerial(9, 10);
//Servos
Servo azimuthServo,altServo;
//Structs required for sunpos()
cTime currentTime;
cLocation currentLocation;
cSunCoordinates result;
//Servo related variables
String azimuthServoDemandStr,altServoDemandStr;
int azimuthServoDemandInt,altServoDemandInt;
void setup() {
//Setup bluetooth comms
bluetoothSerial.begin(115200);
//Setup servos
azimuthServo.attach(3);
altServo.attach(5);
//Initialise rtc
rtc.begin();
//Get current time and date (for testing)
currentTime.iYear = rtc.getYearInt();
currentTime.iMonth = rtc.getMonthInt();
currentTime.iDay = rtc.getDayInt();
currentTime.dHours = rtc.getHourD();
currentTime.dMinutes =rtc.getMinuteD();
currentTime.dSeconds = rtc.getSecondD();
//Hard coded coordinates, could upgrade to GPS
currentLocation.dLongitude = -1.485520;
currentLocation.dLatitude = 51.783710;
sunpos(currentTime,currentLocation,&result);
/* This is how you set the time on the RTC if needed
rtc.setDOW(THURSDAY); //f Set Day-of-Week to SUNDAY
rtc.setTime(17, 18, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(20, 12, 2018); // Set the date to January 1st, 2014
*/
}
void loop() {
//If manual control has been activated
if (bluetoothSerial.available()) {
getManualServoDemand();
}
}
/*
* Get demand from bluetooth serial and move servos to that point. First character is either
* 'A' or 'B' for either az or alt. Value from bluetooth will be in the range 0 - 4096 so
* map() function is used to map that to the range 500 - 2250 which is roughly 0 - 180 degrees
*/
void getManualServoDemand(){
switch (bluetoothSerial.read()){
case 'A':
while(bluetoothSerial.available()){
azimuthServoDemandStr=bluetoothSerial.readString();
}
azimuthServoDemandInt = azimuthServoDemandStr.toInt();
bluetoothSerial.print("Azimuth to ");
bluetoothSerial.println(map(azimuthServoDemandInt,0,4096,500,2250));
azimuthServo.writeMicroseconds(map(azimuthServoDemandInt,0,4096,500,2250));
break;
case 'B':
while(bluetoothSerial.available()){
altServoDemandStr=bluetoothSerial.readString();
}
altServoDemandInt = altServoDemandStr.toInt();
bluetoothSerial.print("Altitude to ");
bluetoothSerial.println(map(altServoDemandInt,0,4096,500,2250));
altServo.writeMicroseconds(map(altServoDemandInt,0,4096,500,2250));
break;
default:
break;
}
}