-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_android_controlled_car_v1.ino
169 lines (144 loc) · 4.04 KB
/
simple_android_controlled_car_v1.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***************************************************************************
Example Code of an Android Phone(Tab) controlled mobile robot
using the Saleng Mobile Robot Shield, a bluetooth module and a 2-wheel (or 4-wheel) robot chassis.
This software is free provided that this notice is not removed and proper attribution
is accorded to Layad Circuits and its Author(s).
Layad Circuits invests resources in producing free software. By purchasing Layad Circuits'
products or utilizing its services, you support the continuing development of free software
for all.
The Saleng Mobile Robot Shield is designed and made by Layad Circuits.
By supporting our products, you support local innovation and share in the movement
to spur local ingenuity.
Author(s): C.D.Malecdan for Layad Circuits Electronics Engineering
Revision: 1.0 - 2017/09/27 - initial release
Layad Circuits Electronics Engineering Supplies and Services
B314 Lopez Bldg., Session Rd. cor. Assumption Rd., Baguio City, Philippines
www.layadcircuits.com
general: info@layadcircuits.com
sales: sales@layadcircuits.com
+63-916-442-8565
***************************************************************************/
#include <SoftwareSerial.h>
SoftwareSerial bt(12, A3); //these pins are from the T1 and E1 pins of the Shield.
//Connect the bluetooth module Tx and Rx pins to the Shield's T1 and E1 respectively.
//Connect the bluetooth Vcc to any + pin and ground to any - pin of the shield
// bluetooth related
const char BT_FORWARD = 'f';
const char BT_LEFT = 'l';
const char BT_RIGHT = 'r';
const char BT_BACKWARD = 'b';
const char BT_STOP = 's';
// NOTE: speed control characters: '0'(slowest) to '9' (fastest)
// The bluetooth PIN is by default, 1234
#define AIN1 2
#define BIN1 7
#define AIN2 4
#define BIN2 5
#define PWMA 3
#define PWMB 6
#define STBY 8
void speedSetting(byte val)
{
analogWrite(PWMA,val);
analogWrite(PWMB,val);
}
void forward()
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
digitalWrite(BIN1,LOW);
digitalWrite(BIN2,HIGH);
}
void backward()
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
}
void turnleft()
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);
}
void turnright()
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
digitalWrite(BIN1,LOW);
digitalWrite(BIN2,HIGH);
}
void motorstop()
{
digitalWrite(AIN1,LOW);
digitalWrite(AIN2,LOW);
digitalWrite(BIN1,LOW);
digitalWrite(BIN2,LOW);
}
void shortbreak()
{
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,HIGH);
digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,HIGH);
}
void bluetooth()
{
static unsigned long t;
static unsigned long rxtimer;
if(bt.available())
{
char c=bt.read();
Serial.print(c);
rxtimer = millis();
switch(c)
{
case BT_STOP:
motorstop();
break;
case BT_FORWARD:
forward();
break;
case BT_LEFT:
turnleft();
break;
case BT_RIGHT:
turnright();
break;
case BT_BACKWARD:
backward();
break;
case '0': speedSetting(25); break;
case '1': speedSetting(50); break;
case '2': speedSetting(75); break;
case '3': speedSetting(100); break;
case '4': speedSetting(125); break;
case '5': speedSetting(150); break;
case '6': speedSetting(175); break;
case '7': speedSetting(200); break;
case '8': speedSetting(225); break;
case '9': speedSetting(255); break;
}
}
if(millis() - rxtimer >= 1000) motorstop();
}
void setup() {
Serial.begin(9600);
bt.begin(9600);
pinMode(LED_BUILTIN,OUTPUT);
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
pinMode(STBY,OUTPUT);
digitalWrite(STBY,HIGH);//enable driver
speedSetting(100); //default
//Serial.begin(115200);
Serial.println("start");
bt.println("start");
}
void loop() {
bluetooth();
}