1
2
3
4
5
6
7
8
9
10
11
12
13#include <LiquidCrystal.h>
14#include <Servo.h>
15LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
16Servo myServo;
17
18int const signPin = 6;
19int const sweepPin = 7;
20int const zeroPin = 8;
21int const pot = A0;
22int potVal;
23int angle;
24int rotDelay;
25int zero = 0;
26int signSwitch = 1;
27int alpha;
28int sweepState = 0;
29int lowLimit = -30;
30int highLimit = 60;
31int newLowLimit;
32int newHighLimit;
33
34void setup() {
35 pinMode(zeroPin, INPUT);
36 pinMode(signPin, INPUT);
37
38 lcd.begin(16, 2);
39 lcd.print("Angle of attack");
40 lcd.setCursor(0, 1);
41 lcd.print("control panel");
42 delay(3000);
43 lcd.clear();
44
45 myServo.attach(9);
46 Serial.begin(9600);
47}
48
49void loop() {
50
51
52 if (digitalRead(zeroPin) == HIGH) {
53 zero = angle;
54
55 newLowLimit = lowLimit + zero;
56 newHighLimit = highLimit + zero;
57 Serial.print("Setting low angle limit: ");
58 Serial.println(newLowLimit);
59 Serial.print("Setting high angle limit: ");
60 Serial.println(newHighLimit);
61 delay(100);
62 }
63
64
65 if (digitalRead(signPin) == HIGH) {
66 signSwitch = -1 * signSwitch;
67 delay(100);
68 }
69
70 alpha = signSwitch * (angle - zero);
71
72
73 if (digitalRead(sweepPin) == HIGH) {
74 sweepState = 1;
75 Serial.print("Auto sweep state = ");
76 Serial.println(sweepState);
77 lcd.clear();
78 delay(100);
79 }
80
81
82 if (sweepState == 1) {
83
84
85 if (newLowLimit < 0 || newLowLimit > 179) {
86 newLowLimit = 0;
87 Serial.print("Actual low angle limit: ");
88 Serial.println(newLowLimit);
89 }
90 if (newHighLimit > 179 || newHighLimit < 0) {
91 newHighLimit = 179;
92 Serial.print("Actual high angle limit: ");
93 Serial.println(newHighLimit);
94 }
95
96
97 if (digitalRead(sweepPin) == LOW) {
98
99 lcd.home();
100 lcd.print(lowLimit);
101 lcd.print(" < AOA < ");
102 lcd.print(highLimit);
103 lcd.setCursor(0, 1);
104 lcd.print("Auto sweep ON");
105
106
107 if (signSwitch == 1) {
108 angle = zero + alpha;
109 }
110 else {
111 angle = zero - alpha;
112 newHighLimit = -lowLimit + zero;
113 newLowLimit = -highLimit + zero;
114 }
115
116 while (angle < newHighLimit) {
117
118 potVal = analogRead(pot);
119 Serial.print("potVal: ");
120 Serial.print(potVal);
121 rotDelay = map(potVal, 0, 1023, 15, 100);
122 Serial.print(", rotDelay: ");
123 Serial.print(rotDelay);
124 Serial.println(" ms");
125 myServo.write(angle);
126 delay(rotDelay);
127
128
129 if (digitalRead(sweepPin) == HIGH) {
130 sweepState = 0;
131 Serial.print("Auto sweep state = ");
132 Serial.println(sweepState);
133 lcd.clear();
134 delay(100);
135 goto manual;
136 }
137 angle++;
138 }
139
140 while (angle > newLowLimit) {
141
142 potVal = analogRead(pot);
143 Serial.print("potVal: ");
144 Serial.print(potVal);
145 rotDelay = map(potVal, 0, 1023, 15, 100);
146 Serial.print(", rotDelay: ");
147 Serial.print(rotDelay);
148 Serial.println(" ms");
149 myServo.write(angle);
150 delay(rotDelay);
151
152
153 if (digitalRead(sweepPin) == HIGH) {
154 sweepState = 0;
155 Serial.print("Auto sweep state = ");
156 Serial.println(sweepState);
157 lcd.clear();
158 delay(100);
159 goto manual;
160 }
161 angle--;
162 }
163
164 }
165 }
166 else {
167 manual:
168 lcd.setCursor(0, 1);
169 lcd.print("Manual control");
170
171 potVal = analogRead(pot);
172 Serial.print("potVal: ");
173 Serial.print(potVal);
174 angle = map(potVal, 0, 1023, 0, 179);
175 Serial.print(", angle: ");
176 Serial.print(angle);
177 myServo.write(angle);
178 delay(15);
179 Serial.print(", zero: ");
180 Serial.print(zero);
181 Serial.print(", AOA: ");
182 Serial.println(alpha);
183
184 lcd.home();
185 lcd.print("AOA = ");
186
187
188
189
190 if (alpha >= -179 && alpha <= -100) {
191 lcd.print(alpha);
192 lcd.print(" deg");
193 }
194
195 else if (alpha >= -99 && alpha <= -10) {
196 lcd.print(alpha);
197 lcd.print(" deg ");
198 }
199
200 else if (alpha >= -9 && alpha <= -1) {
201 lcd.print(alpha);
202 lcd.print(" deg ");
203 }
204
205 else if (alpha >= 0 && alpha <= 9) {
206 lcd.print(alpha);
207 lcd.print(" deg ");
208 }
209
210 else if (alpha >= 10 && alpha <= 99) {
211 lcd.print(alpha);
212 lcd.print(" deg ");
213 }
214
215 else if (alpha >= 100 && alpha <= 179) {
216 lcd.print(alpha);
217 lcd.print(" deg ");
218 }
219
220 else {
221 lcd.home();
222 lcd.print("Error: signal");
223 lcd.setCursor(0,1);
224 lcd.print("out of range.");
225 delay(1000);
226 lcd.clear();
227 }
228 }
229}
230
Anonymous user
4 years ago
May i know wat is the purpose for the button to change the sign?