-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoPC2.ino
More file actions
203 lines (150 loc) · 4.07 KB
/
ArduinoPC2.ino
File metadata and controls
203 lines (150 loc) · 4.07 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
#include <Servo.h>
Servo myServo;
byte servoPin = 9;
byte servoMin = 0;
byte servoMax = 180;
byte servoPos = 0;
byte newServoPos = servoMin;
const byte numLEDs = 2;
byte ledPin[numLEDs] = {12, 13};
unsigned long LEDinterval[numLEDs] = {200, 400};
unsigned long prevLEDmillis[numLEDs] = {0, 0};
const byte buffSize = 40;
char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;
char messageFromPC[buffSize] = {0};
int newFlashInterval = 0;
float servoFraction = 0.0; // fraction of servo range to move
unsigned long curMillis;
unsigned long prevReplyToPCmillis = 0;
unsigned long replyToPCinterval = 1000;
//=============
void setup() {
Serial.begin(9600);
// flash LEDs so we know we are alive
for (byte n = 0; n < numLEDs; n++) {
pinMode(ledPin[n], OUTPUT);
digitalWrite(ledPin[n], HIGH);
}
delay(500); // delay() is OK in setup as it only happens once
for (byte n = 0; n < numLEDs; n++) {
digitalWrite(ledPin[n], LOW);
}
// initialize the servo
myServo.attach(servoPin);
moveServo();
// tell the PC we are ready
Serial.println("<Arduino is ready>");
}
//=============
void loop() {
curMillis = millis();
getDataFromPC();
updateFlashInterval();
updateServoPos();
replyToPC();
flashLEDs();
moveServo();
}
//=============
void getDataFromPC() {
// receive data from PC and save it into inputBuffer
if(Serial.available() > 0) {
char x = Serial.read();
// the order of these IF clauses is significant
if (x == endMarker) {
readInProgress = false;
newDataFromPC = true;
inputBuffer[bytesRecvd] = 0;
parseData();
}
if(readInProgress) {
inputBuffer[bytesRecvd] = x;
bytesRecvd ++;
if (bytesRecvd == buffSize) {
bytesRecvd = buffSize - 1;
}
}
if (x == startMarker) {
bytesRecvd = 0;
readInProgress = true;
}
}
}
//=============
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(inputBuffer,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
newFlashInterval = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
servoFraction = atof(strtokIndx); // convert this part to a float
}
//=============
void replyToPC() {
if (newDataFromPC) {
newDataFromPC = false;
Serial.print("<Msg ");
Serial.print(messageFromPC);
Serial.print(" NewFlash ");
Serial.print(newFlashInterval);
Serial.print(" SrvFrac ");
Serial.print(servoFraction);
Serial.print(" SrvPos ");
Serial.print(newServoPos);
Serial.print(" Time ");
Serial.print(curMillis >> 9); // divide by 512 is approx = half-seconds
Serial.println(">");
}
}
//============
void updateFlashInterval() {
// this illustrates using different inputs to call different functions
if (strcmp(messageFromPC, "LED1") == 0) {
updateLED1();
}
if (strcmp(messageFromPC, "LED2") == 0) {
updateLED2();
}
}
//=============
void updateLED1() {
if (newFlashInterval > 100) {
LEDinterval[0] = newFlashInterval;
}
}
//=============
void updateLED2() {
if (newFlashInterval > 100) {
LEDinterval[1] = newFlashInterval;
}
}
//=============
void flashLEDs() {
for (byte n = 0; n < numLEDs; n++) {
if (curMillis - prevLEDmillis[n] >= LEDinterval[n]) {
prevLEDmillis[n] += LEDinterval[n];
digitalWrite( ledPin[n], ! digitalRead( ledPin[n]) );
}
}
}
//=============
void updateServoPos() {
byte servoRange = servoMax - servoMin;
if (servoFraction >= 0 && servoFraction <= 1) {
newServoPos = servoMin + ((float) servoRange * servoFraction);
}
}
//=============
void moveServo() {
if (servoPos != newServoPos) {
servoPos = newServoPos;
myServo.write(servoPos);
}
}