-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROVER.ino
120 lines (113 loc) · 1.65 KB
/
ROVER.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
/* Copy and paste the code below into the Arduino software */
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control
int pingpin = 4;
void setup()
{
int i;
for(i=5;i<=8;i++)
pinMode(i, OUTPUT);
Serial.begin(9600);
}
void loop()
{
while (Serial.available() < 1) {} // Wait until a character is received
char val = Serial.read();
if (val == 'w')
{
forward();
}
else if(val == 'a')
{
left();
}
else if(val == 's')
{
reverse();
}
else if(val == 'd')
{
right();
}
else if(val == 'q')
{
stopall();
}
/*switch(val) // Perform an action depending on the command
{
case 'w'://Move Forward
{
forward ();
break;
}
case 's'://Move Backwards
{
reverse ();
break;
}
case 'a'://Turn Left
{
left ();
break;
}
case 'd'://Turn Right
{
right ();
break;
}
default:
{
stopall();
break;
}
}*/
}
long ping(long time)
{
pinMode(pingpin, OUTPUT);
digitalWrite(pingpin, LOW);
delayMicroseconds(2);
digitalWrite(pingpin, HIGH);
delayMicroseconds(5);
digitalWrite(pingpin, LOW);
pinMode(pingpin, INPUT);
time=pulseIn(pingpin, HIGH);
return(time);
}
void forward(void)
{
analogWrite (E1,255);
digitalWrite(M1,HIGH);
analogWrite (E2,255);
digitalWrite(M2,HIGH);
}
void reverse(void)
{
analogWrite (E1,255);
digitalWrite(M1,LOW);
analogWrite (E2,255);
digitalWrite(M2,LOW);
}
void stopall(void)
{
analogWrite (E1,0);
digitalWrite(M1,HIGH);
analogWrite (E2,0);
digitalWrite(M2,HIGH);
}
void left(void)
{
analogWrite (E1,255);
digitalWrite(M1,LOW);
analogWrite (E2,255);
digitalWrite(M2,HIGH);
}
void right(void)
{
analogWrite (E1,255);
digitalWrite(M1,HIGH);
analogWrite (E2,255);
digitalWrite(M2,LOW);
}