-
Notifications
You must be signed in to change notification settings - Fork 5
/
main code
91 lines (79 loc) · 1.65 KB
/
main code
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
int ir[6],er[6],res[6],preErr[6];
int enR=7 ,enL=10,in1L=16,in2L=17,in1R=14,in2R=15;
void setup()
{
Serial.begin(9600);
pinMode(in1L,OUTPUT);
pinMode(in1R,OUTPUT);
pinMode(in2L,OUTPUT);
pinMode(in2R,OUTPUT);
pinMode(enR,OUTPUT);
pinMode(enL,OUTPUT);
forward();
}
void loop() {
ir [0] = analogRead(A2);
ir [1] = analogRead(A0);
ir [2] = analogRead(A1);
ir [3] = analogRead(A8);
ir [4] = analogRead(A12);
ir [5] = analogRead(A6);
for (int i=0; i<6; ++i)
{
if(ir[i]>200)
ir[i]=1;
else
ir[i]=0;
}
if(ir[5]==0 && ir[3]==1 && ir[0]==1)
left();
if(ir[5]==1 && ir[3]==0 && ir[0]==1)
forward();
if(ir[5]==1 && ir[3]==1 && ir[0]==0)
right();
if(ir[5]==0 && ir[3]==0 && ir[0]==0)
forward();
for(int i=0;i<6;i++)
{
Serial.print(ir[i]);
Serial.print(" ");
}
Serial.println();
}
void right()
{
analogWrite(enR,255);
analogWrite(enL,255);
digitalWrite(in2R, LOW);
digitalWrite(in1R, HIGH);
digitalWrite(in2L, LOW);
digitalWrite(in1L, HIGH);
Serial.println("RIGHT");
}
void left()
{
analogWrite(enR,255);
analogWrite(enL,255);
digitalWrite(in2R, HIGH);
digitalWrite(in1R, LOW);
digitalWrite(in2L, HIGH);
digitalWrite(in1L, LOW);
Serial.println("LEFT");
}
void forward()
{
analogWrite(enR,255);
analogWrite(enL,255);
digitalWrite(in1L, LOW);
digitalWrite(in2L, HIGH);
digitalWrite(in2R, LOW);
digitalWrite(in1R, HIGH);
Serial.println("forward2");
}
void Stop(){
digitalWrite(in1L, LOW);
digitalWrite(in2L, LOW);
digitalWrite(in2R, LOW);
digitalWrite(in1R, LOW);
Serial.println("Stop");
}