-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathracer_random_turn.ino
More file actions
163 lines (125 loc) · 3.17 KB
/
racer_random_turn.ino
File metadata and controls
163 lines (125 loc) · 3.17 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
#include <Servo.h>
int trig = 3;
int echo = 4;
/* 20200604 - changed from 2 to 7 to match Ed's hardware */
int led = 7;
/* 20200604 - changed from 8 to 2 to match Ed's hardware */
int button = 2;
/* 20200604 - changed from 12 to 10 to match Ed's hardware */
int mA = 10;
/* 20200604 - changed from 13 to 11 to match Ed's hardware */
int mB = 11;
int button2 = 8;
Servo motorA;
Servo motorB;
long randNumber;
// float so that we can handle decimalse
float speedOfSoundMetersPerSec = 343;
float duration_microSeconds;
float duration_seconds;
float distance_meters;
float distance_cm;
void setup() {
// put your setup code here, to run once:
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
pinMode(led,OUTPUT);
pinMode(button,INPUT);
motorA.attach(mA);
motorB.attach(mB);
stop();
Serial.begin(9600);
randomSeed(analogRead(0));
}
/*************************/
/***** car movements *****/
/*************************/
void stop() {
motorA.write(90);
motorB.write(90);
}
void backward() {
motorA.write(160);
motorB.write(0);
}
void forward() {
motorA.write(0);
motorB.write(180);
}
void turnRight() {
motorA.write(90);
motorB.write(180);
delay(550);
stop();
}
void turnLeft() {
motorA.write(0);
motorB.write(90);
delay(500);
stop();
}
/***********************/
/**** ultrasonic *******/
/***********************/
float ultrasonic() {
// reset the ultrasonic sensor
digitalWrite(trig,LOW);
delayMicroseconds(5);
// send a 10 microsecond pulse out through the trigger
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// wait for the response and store it in duration. It will return in microseconds.
duration_microSeconds = pulseIn(echo,HIGH);
// convert duration to seconds
duration_seconds = duration_microSeconds / 1000000;
// get distance traveled in meters. distance = (speed * time)/2
distance_meters = (speedOfSoundMetersPerSec * duration_seconds)/2;
// convert to cm
distance_cm = distance_meters*100;
//Serial.println(distance_cm);
return distance_cm;
randomSeed(analogRead(0));
}
void loop() {
// print a random number from 10 to 19
/**20200604 -
* changed from random(3) to random(1,3). You need to add the 1 to
* set the lowest random number to 1.
*
* Also turn off the LED when you begin the loop
*/
digitalWrite(led,LOW);
randNumber = random(1,3);
Serial.println(randNumber);
/**20200604 -
* Add logic so that the LED turns on if randNumber = 1and LED turns off if randNumber = 2
*/
if (randNumber == 1) {
digitalWrite(led,HIGH);
}
if (randNumber == 2) {
digitalWrite(led,LOW);
}
delay(50);
//wait here until you press the button
while(digitalRead(button)==HIGH) {
//do nothing
}
//move forward
forward();
//keep moving forward WHILE I am more than 5 cm away from the wall
while (ultrasonic() > 4) {
forward();
}
stop();
delay(1000);
backward();
delay(500);
if(randNumber== 1) {
turnRight();
}
if(randNumber == 2) {
turnLeft();
}
}