-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode_forPosting_BT.ino
More file actions
234 lines (182 loc) · 11 KB
/
Code_forPosting_BT.ino
File metadata and controls
234 lines (182 loc) · 11 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* • APPLICATION: Code for DSC. This code uses the Encoder library (see the link in
the info panel when you add the library to Arduino IDE).
Date: 9/4/17 by; B. Troyan, GalaxyPoint.com, copyright ©
REVISIONS: Rev -A-, 12/2/17 Added code for Bluetooh display of Alt encoder
COMMENTS: This code contains enough to help you fully complete your project -
I deleted some code that you will need to complete but, careful
inspection and reading the comments will make it easy. In some
instances I left code, in other places I deleted and placed comments.
The code will match up with the Circuit and PCB posted at
http://fritzing.org/projects/telescope-tracker
so, all you need to do is complete what's missing...
In the end, you will have:
Two Encoders for the scope position (Azi and Alt)
A button to set Azi & Alt to 0,0
A button to set Azi to 0 and Alt to 45 deg. ★ change it to your
latitude.
One Encoder for manually setting Azi & Alt to a reference star
(Example: Press setAlt, turn the manual encoder to desired value and
press it's shaft - the spec'd encoder contains a button switch that
will set the value to what you dialed in). You can use the spec'd
encoder or buy one without detents...
You can use the serial monitor but, be aware that this may slow the
responsivness of encoder rotation/updates. Comment them for your
final design with an LCD...
*/
// **************************************************************************************
// * ***** DECLARATIONS ***** *
// **************************************************************************************
#include <Encoder.h>
#include<stdlib.h> // custom C formating of strings
#include <avr/io.h>
#include <util/delay.h> // reqd by C to use delays
Encoder enc_A(3, 19); // Pins D3, A5: D3 intrupt, A5 no intrpt
// Analog A5 + 14 = D19 on Atmel 328p & UNO
Encoder enc_MANUAL(16,17); // Pins A2, A3
#include<SoftwareSerial.h> // for Bluetooth display of Alt
#define TxD 4 // Pin D4
#define RxD 2 // pin D2
SoftwareSerial bluetoothSerial(TxD, RxD);
volatile float Azi = 0.0, Alt = 0.0; // Azi and Alt position
char stringBuff1[8]; // Alt output buffer for LCD
const char degree = char (223); // degree symbol for extended ascii
const short setZERO = 11; // D11 Alt & Azi 0,0 reset btn
const short polarisBtn = 12; // D12
const short setAZI = 13; // D13 = setAZI
const short setALT = 14; // A0 = D14 = setALT
const short setManBtn = 15; // A1 = D15 = SET
/* ★★★ Coefficients for different encoders and gearing ★★★
>> 360/PPRenc/GR/Q << Use '4' for 'Q'uadrature, '1' = None
∴ 360deg / PPR pulses / GR / Q = "X deg/pulse " */
float degSignWise = 0.0375; // √ = 360/600/4/4 (GR 4:1)
float EncA_Coef = degSignWise; // ★★★ Set for desired encoder coef
float EncB_Coef = degSignWise; // ★★★ Set for desired encoder coef
// ★ Also change 'OVER-ROTATION OVERFLOW' below
float oldPosition = 0;
// **************************************************************************************
// * ***** MAIN ***** *
// **************************************************************************************
int main(void) {
pinMode(16, INPUT_PULLUP); // A2 enc_MANUAL encoder
pinMode(17, INPUT_PULLUP); // A3 enc_MANUAL encoder
pinMode(3, INPUT_PULLUP); // Encoder 1
pinMode(19, INPUT_PULLUP); // Encoder 1
/* ★ Your code for Encoder 2 */
pinMode(polarisBtn, INPUT_PULLUP); // D4 Polaris btn
pinMode(setZERO, INPUT_PULLUP); // D5 Alt & Azi reset btn
pinMode(setALT, INPUT_PULLUP); // A0 + 14 = D14, set Alt coord btn
pinMode(setAZI, INPUT_PULLUP); // D13 set Azi coord btn
pinMode(setManBtn, INPUT_PULLUP); // A1 + 14 = D15, set btn
digitalWrite(setZERO, HIGH); // set high
digitalWrite(polarisBtn, HIGH); // set high
digitalWrite(setALT, HIGH); // set high
digitalWrite(setAZI, HIGH); // set high
digitalWrite(setManBtn, HIGH); // set high
long A_position = -999;
long B_position = -999;
Serial.begin(9600); // for serial monitor display
bluetoothSerial.begin(9600); // for Bluetooth display
// ************************************************************************************
// * ***** While LOOP ***** *
// ************************************************************************************
while (1) {
long new_A, new_B, new_C; // keep here or it hiccups!
new_A = enc_A.read(); // A is ALTitude
__asm__("nop\n"); // sync delay
// ★★ your code for the AZImuth
__asm__("nop\n"); // sync delay
new_C = enc_MANUAL.read(); // C is manual encoder
__asm__("nop\n"); // sync delay
if (new_A != A_position || new_B != B_position) { // this is for two encoders
// ---------------------- >>>★★★ OVER-ROTATION OVERFLOW <<<★★★
/* **** Encoder A **** */
if (new_A <= -9600 ) { // ★ Fixes neg over-flow ★
new_A = 9600; // new_B, new_A are signed ints
} // ★★★ There are X counts per Scope rev: ★★★
// X = PPRenc * Q * GR. Be sure to set
// it using your Encoder's PPR per
// encoder rev.
// √ SignWise: 600 * 4 * 4 = 9600
/* ★★ Your code for Encoder B the AZImuth **** */
//-----------------------------------------------
A_position = new_A;
B_position = new_B;
Alt = new_A * EncA_Coef; // ★★★ Be sure to set in Declarations
Azi = new_B * EncB_Coef; // ★★★
// Alt roll over -----------------------
if (Alt >= 360.0 ) {
enc_A.write(0);
Alt = Alt + EncA_Coef;
}
if (Alt < 0.0 ) {
Alt = 360.0 + (new_A * EncA_Coef); // new_A negative when it gets here
}// end Alt roll -----
// Azi roll over ------------------------
/* ★★ Your code for Azimuth rollover goes here */
//-----------------------------------------------
// DISPLAY
dtostrf(Alt, 7, 3, stringBuff1); // 7 char wide with 3 decimals
/* ★★ Also do Your code for displaying theAzimuth */
Serial.println(Alt);
// Serial.print(" "); // for serial monitor display
// Serial.println(Azi);
if (bluetoothSerial.available()) { // for bluetooh display
bluetoothSerial.write(stringBuff1);
bluetoothSerial.write("\n");
}//end if bluetoothserial ======================
}//•end if New Postion at the top -fyi: put cursor in gutter to see fold line start
// BUTTONS
if (digitalRead(setZERO) == LOW) { // • 0,0 btn pressed
enc_A.write(0); // write 0 to enc_A
// ★Your code - do it for Azi encoder too!
}//•end btn if
if (digitalRead(polarisBtn) == LOW) { // • polaris btn pressed
enc_A.write(1210); // • write to enc_A: The value that
// represents your latitude
// Also, write 0 to Azi encoder
//• X = Latitude*PPR*Gr*Q / 360
//• X = 45.4*600*4*4/360 = 1210
}//•end btn if
// Manually Setting Altitude
if (digitalRead(setALT) == LOW) {
__asm__("nop\n");
while(1) { // do this until break/exit
float newPosition = enc_MANUAL.read() * 0.5; //★ mult by 0.5 for 1/2 deg steps
// if desired. Or, don't you decide
__asm__("nop\n");
if (newPosition != oldPosition) {
_delay_ms(2);
oldPosition = newPosition;
// Alt roll over ------------------------
if (newPosition >= 360.0 ) {
enc_MANUAL.write(0);
newPosition = 0;
}//endid
if (newPosition < 0.0 ) {
newPosition = 360.0 + (newPosition); // new is negative when it gets here
}// end Alt roll -----
// Serial.println(newPosition);
enc_A.write(newPosition/EncA_Coef);
Alt = newPosition;
dtostrf(Alt, 7, 3, stringBuff1);
Serial.println(Alt);
if (bluetoothSerial.available()) { // for bluetooh display
bluetoothSerial.write(stringBuff1);
bluetoothSerial.write("\n");
}//end if bluetoothserial ======================
}//endIf
if (digitalRead(setManBtn) == LOW) {
enc_MANUAL.write(0); // reset for new manual counting
newPosition = 0; // reset for new manual counting
break; // exits out of while loop
}//endif
}//end While
}//endif
// ★★★ Manually Setting Azimuth ★★ ----------------------------------
/** Your code for the Azimuth (basically, copy the altitude code and tweak names...) */
}//•end while----------------------------------------
return (0); // never gets here but used by C
}// ••• end main •••
// **************************************************************************************
// * ***** • SUBROUTINES • ***** *
// **************************************************************************************