-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduinoside.ino
91 lines (73 loc) · 1.69 KB
/
Arduinoside.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
#include <Servo.h>
#include <avr/pgmspace.h>
Servo servo[4];
String inputString = "";
String servoString = "";
String commandString = "";
boolean stringComplete = false;
void setup(){
Serial.begin(9600);// check that this is similar with the processing side
Serial.println("Go...");
inputString.reserve(10);
servoString.reserve(10);
commandString.reserve(10);
servo[0].attach(2);
delay(100);
servo[1].attach(3);
delay(100);
servo[2].attach(4);
delay(100);
servo[3].attach(10);
delay(100);
servo[4].attach(11);
delay(100);
servo[0].write(90);
delay(100);
servo[1].write(90);
delay(100);
servo[2].write(90);
delay(100);
servo[3].write(90);
delay(100);
servo[4].write(90);
}
void loop(){
parseCommand();
}
void finish(){
while(true){
;
;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
void parseCommand(){
// print the string when a newline arrives:
if (stringComplete) {
inputString.trim();
//Serial.println(inputString);
int separatorIndex = inputString.indexOf(',');
servoString=inputString.substring(0,1);
commandString=inputString.substring(separatorIndex+1);
int servoNum = servoString.toInt();
int servoPos = commandString.toInt();
servo[servoNum].write(servoPos);
// clear the string:
inputString = "";
servoString = "";
commandString = "";
stringComplete = false;
}
}