-
Notifications
You must be signed in to change notification settings - Fork 0
/
WB_MBOC.ino
95 lines (90 loc) · 1.82 KB
/
WB_MBOC.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
#define lmotorf 10
#define lmotorb 9
#define rmotorf 11
#define rmotorb 12
int enA = 6;
int enB = 5;
const int buzzer = 2;
//HIGH white
//LOW black
void setup() {
pinMode(lmotorf,OUTPUT);
pinMode(rmotorf,OUTPUT);
pinMode(lmotorb,OUTPUT);
pinMode(rmotorb,OUTPUT);
pinMode(enA,OUTPUT);
pinMode(enB,OUTPUT);
pinMode(8,INPUT);
pinMode(7,INPUT);
pinMode(4,INPUT);
pinMode(3,INPUT);
pinMode(buzzer,OUTPUT);
}
void loop() {
// dynamic line follower code
// if the sensor is on white it returns LOW value to the Arduino
// if it is on black it returns a HIGH value to the Arduino
int lsensor=digitalRead(8);
int rsensor=digitalRead(7);
int tsensor=digitalRead(4);
int fsensor=digitalRead(3);
if(fsensor==LOW)
{
tone(buzzer, 1000);
delay(1000);
digitalWrite(lmotorf,LOW);
digitalWrite(rmotorf,LOW);
digitalWrite(lmotorb,LOW);
digitalWrite(rmotorb,LOW);
noTone(buzzer);
}
else if(tsensor==HIGH)
{
tone(buzzer, 1000);
delay(1000);
analogWrite(enA,200);
analogWrite(enB,200);
digitalWrite(lmotorf,HIGH);
digitalWrite(rmotorf,HIGH);
digitalWrite(lmotorb,LOW);
digitalWrite(rmotorb,LOW);
noTone(buzzer);
}
else if((lsensor==HIGH)&&(rsensor==HIGH))
{
//both sensors on white
// go forward
digitalWrite(lmotorf,HIGH);
digitalWrite(rmotorf,HIGH);
digitalWrite(lmotorb,LOW);
digitalWrite(rmotorb,LOW);
}
else if((lsensor==HIGH)&& (rsensor==LOW))
{
//right sensor on black line
// turn right
digitalWrite(lmotorf,HIGH);
digitalWrite(rmotorf,LOW);
digitalWrite(lmotorb,LOW);
digitalWrite(rmotorb,HIGH);
}
else if((lsensor==LOW)&&(rsensor==HIGH))
{
//left sensor on black line
// turn left
digitalWrite(lmotorf,LOW);
digitalWrite(rmotorf,HIGH);
digitalWrite(lmotorb,HIGH);
digitalWrite(rmotorb,LOW);
}
else
{
tone(buzzer, 1000);
delay(1000);
digitalWrite(lmotorf,HIGH);
digitalWrite(rmotorf,HIGH);
digitalWrite(lmotorb,LOW);
digitalWrite(rmotorb,LOW);
noTone(buzzer);
}
}