/
sketch_apr03a.ino
166 lines (156 loc) · 4.11 KB
/
sketch_apr03a.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
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
int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int motor1Pin1 = 4;
int motor1Pin2 = 5;
int motor1Pin3 = 6;
int motor1Pin4 = 7;
int delayTime = 5;
int prevx = 0;
int prevy = 0;
int newx = 0;
int newy = 0;
float n1 = 0;
float n2 = 0;
float prevn1 = 0;
float prevn2 = 0;
int L = 295;
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor1Pin3, OUTPUT);
pinMode(motor1Pin4, OUTPUT);
}
void forward1(int time){
for(int x=0;x<time/20;x++){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
}
}
void forward2(int time){
for(int x=0;x<time/20;x++){
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
delay(delayTime);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
delay(delayTime);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, HIGH);
digitalWrite(motor1Pin4, LOW);
delay(delayTime);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, HIGH);
delay(delayTime);
}
}
void backward1(int time){
for(int x=0;x<time/20;x++){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
}
}
void backward2(int time){
for(int x=0;x<time/20;x++){
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
delay(delayTime);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, HIGH);
delay(delayTime);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, HIGH);
digitalWrite(motor1Pin4, LOW);
delay(delayTime);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
delay(delayTime);
}
}
void loop() {
if(Serial.available()){ // only send data back if data has been sent
String inByte = Serial.readString(); // read the incoming data
Serial.println(inByte); // send the data back in a new line so that it is not all one long line
int commaIndex = inByte.indexOf(',');
// Search for the next comma just after the first
int secondCommaIndex = inByte.indexOf(',', commaIndex+1);
String firstValue = inByte.substring(0, commaIndex);
String secondValue = inByte.substring(commaIndex+1, secondCommaIndex);
String thirdValue = inByte.substring(secondCommaIndex);
newx=firstValue.toInt();
newy=secondValue.toInt();
sqrt(sq(prevx)+sq(prevy))=prevn1;
sqrt(sq(prevx)+sq(L-prevy))=prevn2;
sqrt(sq(newx)+sq(newy))=n1;
sqrt(sq(newx)+sq(L-newy))=n2;
if ((n1-prevn1)>1){
forward((n1-prevn1)*20);
}
else{
backward((prevn1-n1)*20);
}
if ((n2-prevn2)>1){
forward((n2-prevn2)*20);
}
else{
backward((prevn2-n2)*20);
}
newx=prevx;
newy=prevy;
}
}