-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoV_arduino_code.ino
93 lines (69 loc) · 2.28 KB
/
PoV_arduino_code.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
#include <Servo.h>
const int servoHead = 9;
const int servoLeft = 10;
const int servoRight = 11;
Servo servoHead1;
Servo servoLeft1;
Servo servoRight1;
int headMotorValue = 0;
int bottomMotorValue = 0;
int bottomMotorValueSides = 0;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
Serial.begin(9600);
//inputString.reserve(200);
}
void loop() {
if (stringComplete) {
inputString = ""; // clear the string:
stringComplete = false;
if (headMotorValue >= 0 && headMotorValue <= 180) {
servoHead1.attach(servoHead);
servoHead1.write(headMotorValue);
} else {
servoHead1.detach();
}
if (bottomMotorValue >= -60 && bottomMotorValue <= -30) {
servoLeft1.attach(servoLeft);
servoRight1.attach(servoRight);
servoRight1.write(0);
servoLeft1.write(180);
//go Forward
} else if (bottomMotorValue >= 10 && bottomMotorValue <= 40) {
servoLeft1.attach(servoLeft);
servoRight1.attach(servoRight);
servoRight1.write(180);
servoLeft1.write(0);
//go Backward
} else if (bottomMotorValueSides >= 30 && bottomMotorValueSides <= 50){
servoLeft1.attach(servoLeft);
servoLeft1.write(180);
servoHead1.detach();
//turn Right
} else if (bottomMotorValueSides <= -30 && bottomMotorValueSides >= -50){
servoRight1.attach(servoRight);
servoRight1.write(0);
servoHead1.detach();
//turn Left
} else {
servoLeft1.detach();
servoRight1.detach();
}
}
}
void serialEvent() {
while (Serial.available()) {
Serial.write("OK"); //handshake
char inChar = (char)Serial.read(); // get the new byte
inputString += inChar; // add it to the inputString:
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
headMotorValue = inputString.substring(0, inputString.indexOf(',')).toInt();
bottomMotorValue = inputString.substring(inputString.indexOf(',') + 1).toInt();
bottomMotorValueSides = inputString.substring(inputString.lastIndexOf(',') +1).toInt();
stringComplete = true;
}
}
}