1int motor1Pin1 = 3;
2int motor1Pin2 = 4;
3int enable1Pin = 6;
4int motor2Pin1 = 8;
5int motor2Pin2 = 9;
6int enable2Pin = 11;
7int state;
8int flag=0;
9int stateStop=0;
10void setup() {
11
12 pinMode(motor1Pin1, OUTPUT);
13 pinMode(motor1Pin2, OUTPUT);
14 pinMode(enable1Pin, OUTPUT);
15 pinMode(motor2Pin1, OUTPUT);
16 pinMode(motor2Pin2, OUTPUT);
17 pinMode(enable2Pin, OUTPUT);
18
19 digitalWrite(enable1Pin, HIGH);
20 digitalWrite(enable2Pin, HIGH);
21
22 Serial.begin(9600);
23}
24
25void loop() {
26
27 if(Serial.available() > 0){
28 state = Serial.read();
29 flag=0;
30 }
31
32 if (state == '1') {
33 digitalWrite(motor1Pin1, HIGH);
34 digitalWrite(motor1Pin2, LOW);
35 digitalWrite(motor2Pin1, LOW);
36 digitalWrite(motor2Pin2, HIGH);
37 if(flag == 0){
38 Serial.println("Go Forward!");
39 flag=1;
40 }
41 }
42
43
44 else if (state == '2') {
45 digitalWrite(motor1Pin1, HIGH);
46 digitalWrite(motor1Pin2, LOW);
47 digitalWrite(motor2Pin1, LOW);
48 digitalWrite(motor2Pin2, LOW);
49 if(flag == 0){
50 Serial.println("Turn LEFT");
51 flag=1;
52 }
53 delay(1500);
54 state=3;
55 stateStop=1;
56 }
57
58 else if (state == '3' || stateStop == 1) {
59 digitalWrite(motor1Pin1, LOW);
60 digitalWrite(motor1Pin2, LOW);
61 digitalWrite(motor2Pin1, LOW);
62 digitalWrite(motor2Pin2, LOW);
63 if(flag == 0){
64 Serial.println("STOP!");
65 flag=1;
66 }
67 stateStop=0;
68 }
69
70 else if (state == '4') {
71 digitalWrite(motor1Pin1, LOW);
72 digitalWrite(motor1Pin2, LOW);
73 digitalWrite(motor2Pin1, LOW);
74 digitalWrite(motor2Pin2, HIGH);
75 if(flag == 0){
76 Serial.println("Turn RIGHT");
77 flag=1;
78 }
79 delay(1500);
80 state=3;
81 stateStop=1;
82 }
83
84 else if (state == '5') {
85 digitalWrite(motor1Pin1, LOW);
86 digitalWrite(motor1Pin2, HIGH);
87 digitalWrite(motor2Pin1, HIGH);
88 digitalWrite(motor2Pin2, LOW);
89 if(flag == 0){
90 Serial.println("Reverse!");
91 flag=1;
92 }
93 }
94
95
96}