-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_moteur2.ino
149 lines (128 loc) · 2.61 KB
/
test_moteur2.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//Standard PWM DC control
int MA1 = 2; //M1 Control
int MA2 = 3; //M1 Control
int MB1 = 4; //M2 Control
int MB2 = 5; //M2 Control
float speedRight=0.0;
float speedLeft=0.0;
float speedStep = 0.1;
void increaseLeft()
{
speedLeft += speedStep;
if(speedLeft > 1.0) speedLeft = 1.0;
}
void increaseRight()
{
speedRight += speedStep;
if(speedRight > 1.0) speedRight = 1.0;
}
void decreaseLeft()
{
speedLeft -= speedStep;
if(speedLeft < -1.0) speedLeft = -1.0;
}
void decreaseRight()
{
speedRight -= speedStep;
if(speedRight < -1.0) speedRight = -1.0;
}
void stopAll () //STOP
{
speedRight=0.0;
speedLeft=0.0;
}
void setMotors()
{
// LEFT
if(speedLeft >= 0)
{
digitalWrite(MA1,LOW);
analogWrite(MA2,speedLeft*255);
}
else
{
digitalWrite(MA1,HIGH);
analogWrite(MA2,speedLeft*255);
}
// RIGHT
if(speedRight >= 0)
{
digitalWrite(MB1,LOW);
analogWrite(MB2,speedRight*255);
}
else
{
digitalWrite(MB1,HIGH);
analogWrite(MB2,speedRight*255);
}
}
void setup(void)
{
pinMode(MA1, OUTPUT);
pinMode(MA2, OUTPUT);
pinMode(MB1, OUTPUT);
pinMode(MB2, OUTPUT);
Serial.begin(9600); //Set Baud Rate
Serial.println("Run keyboard control");
}
void loop(void)
{
if(Serial.available()){
char val = Serial.read();
if(val != -1)
{
switch(val)
{
case '8':
increaseLeft();
increaseRight();
Serial.println("Forward");
break;
case '2':
decreaseLeft();
decreaseRight();
Serial.println("Backward");
break;
case '4':
decreaseLeft();
increaseRight();
Serial.println("Left");
break;
case '6':
increaseLeft();
decreaseRight();
Serial.println("Right");
break;
case '7':
increaseLeft();
Serial.println("More left");
break;
case '1':
decreaseLeft();
Serial.println("Less left");
break;
case '9':
increaseRight();
Serial.println("More right");
break;
case '3':
decreaseRight();
Serial.println("Less right");
break;
case '5':
stopAll ();
Serial.println("StopAll");
break;
}
}
else {
stopAll ();
Serial.println("Stoped because error -----------------");
}
Serial.print("Speed left=");
Serial.print(speedLeft);
Serial.print(" right=");
Serial.println(speedRight);
setMotors();
}
}