-
Notifications
You must be signed in to change notification settings - Fork 0
/
AUTO_GO.ino
84 lines (74 loc) · 2.19 KB
/
AUTO_GO.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
// The direction of the car's movement
// ENA ENB IN1 IN2 IN3 IN4 Description
// HIGH HIGH HIGH LOW LOW HIGH Car is runing forward
// HIGH HIGH LOW HIGH HIGH LOW Car is runing back
// HIGH HIGH LOW HIGH LOW HIGH Car is turning left
// HIGH HIGH HIGH LOW HIGH LOW Car is turning right
// HIGH HIGH LOW LOW LOW LOW Car is stoped
// HIGH HIGH HIGH HIGH HIGH HIGH Car is stoped
// LOW LOW N/A N/A N/A N/A Car is stoped
//define L298n module IO Pin
#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
void forward(){
digitalWrite(ENA,HIGH); //enable L298n A channel
digitalWrite(ENB,HIGH); //enable L298n B channel
digitalWrite(IN1,HIGH); //set IN1 hight level
digitalWrite(IN2,LOW); //set IN2 low level
digitalWrite(IN3,LOW); //set IN3 low level
digitalWrite(IN4,HIGH); //set IN4 hight level
Serial.println("Forward");//send message to serial monitor
}
void back(){
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
Serial.println("Back");
}
void left(){
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
Serial.println("Left");
}
void right(){
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
Serial.println("Right");
}
//before execute loop() function,
//setup() function will execute first and only execute once
void setup() {
Serial.begin(9600);//open serial and set the baudrate
pinMode(IN1,OUTPUT);//before useing io pin, pin mode must be set first
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
}
//Repeat execution
void loop() {
forward(); //go forward
delay(1000);//delay 1000 ms
back(); //go back
delay(1000);
left(); //turning left
delay(1000);
right(); //turning right
delay(1000);
}