-
Notifications
You must be signed in to change notification settings - Fork 14
/
Motor Controller Coding.ino
91 lines (87 loc) · 2.13 KB
/
Motor Controller Coding.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
int
left_motor_forward=4,
left_motor_backward=8,
right_motor_forward=2,
right_motor_backward=12,
left_motor_pwm=10,
right_motor_pwm=6;
char c;
void setup()
{
Serial.begin(9600);
pinMode(left_motor_forward,OUTPUT);
pinMode(left_motor_backward,OUTPUT);
pinMode(right_motor_forward,OUTPUT);
pinMode(right_motor_backward,OUTPUT);
}
void loop()
{
while(Serial.available())
{
c=Serial.read();
if(c=='f')
forward();
else if(c=='b')
backward();
else if(c=='l')
left_turn();
else if(c=='r')
right_turn();
else if(c=='s')
Stop();
}
}
void forward()
{
Serial.println("the bot will move forward");
digitalWrite(left_motor_forward,1);
digitalWrite(right_motor_forward,1);
digitalWrite(left_motor_backward,0);
digitalWrite(right_motor_backward,0);
analogWrite(left_motor_pwm,150);
analogWrite(right_motor_pwm,150);
delay(1000);
Stop();
}
void backward()
{
Serial.println("the bot will move backward");
digitalWrite(left_motor_backward,1);
digitalWrite(right_motor_backward,1);
digitalWrite(left_motor_forward,0);
digitalWrite(right_motor_forward,0);
analogWrite(left_motor_pwm,150);
analogWrite(right_motor_pwm,150);
delay(1000);
Stop();
}
void right_turn() //hard right turn
{
Serial.println("the bot will take a hard right turn");
digitalWrite(left_motor_forward,1);
digitalWrite(right_motor_forward,0);
digitalWrite(left_motor_backward,0);
digitalWrite(right_motor_backward,1);
analogWrite(left_motor_pwm,150);
analogWrite(right_motor_pwm,100);
delay(300); //you may have to vary the delay for perfect right turn..
Stop();
}
void left_turn() //hard left turn
{
Serial.println("the bot will take a hard left turn");
digitalWrite(left_motor_forward,0);
digitalWrite(right_motor_forward,1);
digitalWrite(left_motor_backward,1);
digitalWrite(right_motor_backward,0);
analogWrite(left_motor_pwm,100);
analogWrite(right_motor_pwm,150);
delay(300); //you may have to vary the delay for perfect right turn..
Stop();
}
void Stop()
{
Serial.println("the bot will stop");
analogWrite(left_motor_pwm,0);
analogWrite(right_motor_pwm,0);
}