-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code for Green Hbridge
176 lines (168 loc) · 3.04 KB
/
Code for Green Hbridge
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#define ina 6 //L298n Motor Driver pins
#define inb 7
#define inc 5
#define ind 4
#define LED 13
#define ena 9
#define enb 3
int command; //Int to store app command state.
int Speed = 204; // 0 - 255.
int Speedsec;
int buttonState = 0;
int lastButtonState = 0;
int Turnradius = 0; //Set the radius of a turn, 0 - 255 Note:the robot will malfunction if this is higher than int Speed.
int brakeTime = 45;
int brkonoff = 1; //1 for the electronic braking system, 0 for normal.
void setup() {
pinMode(ina, OUTPUT);
pinMode(inb, OUTPUT);
pinMode(inc, OUTPUT);
pinMode(ind, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
pinMode(LED, OUTPUT); //Set the LED pin.
Serial.begin(9600); //Set the baud rate to your Bluetooth module.
}
void forward() {
digitalWrite(ina, LOW);
digitalWrite(inb, HIGH);
analogWrite(ena, 255);
digitalWrite(inc, HIGH);
digitalWrite(ind, LOW);
analogWrite(enb, 255);
}
void back() {
digitalWrite(ina, HIGH);
digitalWrite(inb, LOW);
analogWrite(ena, 255);
digitalWrite(inc, LOW);
digitalWrite(ind, HIGH);
analogWrite(enb, 255);
}
void left() {
digitalWrite(ina, HIGH);
digitalWrite(inb, LOW);
analogWrite(ena, 0);
digitalWrite(inc, HIGH);
digitalWrite(ind, LOW);
analogWrite(enb, 255);
}
void right() {
digitalWrite(ina, LOW);
digitalWrite(inb, HIGH);
analogWrite(ena, 255);
digitalWrite(inc, LOW);
digitalWrite(ind, HIGH);
analogWrite(enb, 0);
}
void forwardleft() {
digitalWrite(ina, LOW);
digitalWrite(inb, HIGH);
analogWrite(ena, 204);
digitalWrite(inc, HIGH);
digitalWrite(ind, LOW);
analogWrite(enb, 255);
}
void forwardright() {
digitalWrite(ina, LOW);
digitalWrite(inb, HIGH);
analogWrite(ena, 255);
digitalWrite(inc, HIGH);
digitalWrite(ind, LOW);
analogWrite(enb, 204);
}
void Stop() {
analogWrite(ina, 0);
analogWrite(inb, 0);
analogWrite(inc, 0);
analogWrite(ind, 0);
}
void brakeOn() {
//Here's the future use: an electronic braking system!
// read the pushbutton input pin:
buttonState = command;
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == 'S') {
if (lastButtonState != buttonState) {
digitalWrite(ina, HIGH);
digitalWrite(inb, HIGH);
digitalWrite(inc, HIGH);
digitalWrite(ind, HIGH);
delay(brakeTime);
Stop();
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}
void brakeOff() {
}
void loop() {
if (Serial.available() > 0) {
command = Serial.read();
Stop(); //Initialize with motors stoped.
switch(command){
case 'a':
forward();
break;
case 'c':
back();
break;
case 'd':
left();
break;
case 'b':
right();
break;
case 'h':
forwardleft();
break;
case 'f':
forwardright();
break;
case '0':
Speed = 100;
break;
case '1':
Speed = 140;
break;
case '2':
Speed = 153;
break;
case '3':
Speed = 165;
break;
case '4':
Speed = 178;
break;
case '5':
Speed = 191;
break;
case '6':
Speed = 204;
break;
case '7':
Speed = 216;
break;
case '8':
Speed = 229;
break;
case '9':
Speed = 242;
break;
case 'q':
Speed = 255;
break;
}
Speedsec = Turnradius;
if (brkonoff == 1) {
brakeOn();
} else {
brakeOff();
}
}
}