-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWallAvoidingCarCode.ino
More file actions
169 lines (144 loc) · 3.84 KB
/
WallAvoidingCarCode.ino
File metadata and controls
169 lines (144 loc) · 3.84 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
164
165
166
167
168
169
//Ultrasonic sensor pins
#define TRIG 9
#define ECHO 10
//motor module pins
const int channel_a_enable = 6;
const int channel_a_input_1 = 4;
const int channel_a_input_2 = 7;
const int channel_b_enable = 5;
const int channel_b_input_3 = 3;
const int channel_b_input_4 = 2;
int a=0; //motor a speed and direction
int b=0; //motor b speed and direction
int phase=0; //what stage are we up to
long timeout=0; //how long have we been doing it for
#define PROX 30
#define FORWARDSPEED 150
#define REVERSESPEED 150
//set TURNSPEED negative to turn the other way
#define TURNSPEED 150
#define REVERSETIME 1000
#define TURNTIME 1000
void setup()
{
pinMode(11,OUTPUT);
digitalWrite(11,LOW); //GND for ultrasonic
pinMode(8,OUTPUT);
digitalWrite(8,HIGH); //5V for ultrasonic
usonicsetup();
pinMode( channel_a_enable, OUTPUT ); // Channel A enable
pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1
pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2
pinMode( channel_b_enable, OUTPUT ); // Channel B enable
pinMode( channel_b_input_3, OUTPUT ); // Channel B input 3
pinMode( channel_b_input_4, OUTPUT ); // Channel B input 4
//everything off
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, LOW);
digitalWrite( channel_a_enable, LOW);
digitalWrite( channel_b_input_3, LOW);
digitalWrite( channel_b_input_4, LOW);
digitalWrite( channel_b_enable, LOW);
delay(1000); //wait a bit
Serial.begin( 9600 ); //serial debug
Serial.println("Starting up");
}
void loop()
{
//read ultrasonic sensor
int d;
d=usonic(11600)/58; // distance in cm
if(d==0){d=200;}
Serial.println("Usonic"); //print d on serial monitor
Serial.println(d);
//react based on d
switch(phase){
case 0:
if(d<PROX){phase=1;timeout=millis();} //if too close, reverse
break;
case 1:
if(millis()>timeout+REVERSETIME){phase=2;timeout=millis();} //reversed for REVERSETIME, now turn
break;
case 2:
if(millis()>timeout+TURNTIME){phase=0;} //turned for TURNTIME, now back to forward
break;
}
//print phase for debugging
Serial.print("PHASE:");
Serial.println(phase);
//set motors based on stage
switch(phase){
case 0: //normal going forward
a=FORWARDSPEED;
b=FORWARDSPEED;
break;
case 1: //backing up
a=-(REVERSESPEED);
b=-(REVERSESPEED);
break;
case 2: //turn a little
a=TURNSPEED;
b=-(TURNSPEED);
break;
default: //something wrong happened
a=0;
b=0;
break;
}
setmotor();
delay(200); //wait a bit
}
void setmotor(){
int s;
if(a>0){
digitalWrite( channel_a_input_1, HIGH);
digitalWrite( channel_a_input_2, LOW);
}
if(a<0){
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, HIGH);
}
if(b>0){
digitalWrite( channel_b_input_3, HIGH);
digitalWrite( channel_b_input_4, LOW);
}
if(b<0){
digitalWrite( channel_b_input_3, LOW);
digitalWrite( channel_b_input_4, HIGH);
}
s=abs(a);
if(s>255){s=255;}
if(s<0){s=0;}
analogWrite( channel_a_enable, s);
s=abs(b);
if(s>255){s=255;}
if(s<0){s=0;}
analogWrite( channel_b_enable, s);
}
void allInputsOff()
{
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, LOW);
digitalWrite( channel_a_enable, LOW);
digitalWrite( channel_b_input_3, LOW);
digitalWrite( channel_b_input_4, LOW);
digitalWrite( channel_b_enable, LOW);
}
void usonicsetup(void){
pinMode(ECHO, INPUT);
pinMode(TRIG, OUTPUT);
digitalWrite(TRIG, LOW);
}
long usonic(long utimeout){ //utimeout is maximum time to wait for return in us
long b;
if(digitalRead(ECHO)==HIGH){return 0;} //if echo line is still low from last result, return 0;
digitalWrite(TRIG, HIGH); //send trigger pulse
delay(1);
digitalWrite(TRIG, LOW);
long utimer=micros();
while((digitalRead(ECHO)==LOW)&&((micros()-utimer)<1000)){} //wait for pin state to change- return starts after 460us typically
utimer=micros();
while((digitalRead(ECHO)==HIGH)&&((micros()-utimer)<utimeout)){} //wait for pin state to change
b=micros()-utimer;
return b;
}