forked from Makeblock-official/mBot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mBot-default-program.ino
192 lines (155 loc) · 2.87 KB
/
mBot-default-program.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <Servo.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include "mCore.h"
MeRGBLed rgb;
MeUltrasonic ultr(PORT_3);
MeLineFollower line(PORT_2);
MeBuzzer buzzer;
MeDCMotor MotorL(M1);
MeDCMotor MotorR(M2);
MePort generalDevice;
MePort lightSensor(8);
int defaultSpeed = 200;
int minSpeed = 48;
int maxSpeed = 250;
int moveSpeed = defaultSpeed;
double lightStrength;
void setup()
{
Serial.begin(115200);
Serial.println("Game On JDriven!");
delay(1000);
}
void loop()
{
// disco
disco();
// motor control
motorDemo();
// distance sensor
distanceDemo();
// line follow sensor
lineFollowDemo();
// light sensor
lightSensorDemo();
delay(500);
}
void disco()
{
rgb.setNumber(16);
rgb.clear();
rgb.setColor(10, 0, 0);
buzzer.tone(294, 300);
delay(30);
rgb.setColor(0, 10, 0);
buzzer.tone(330, 300);
delay(30);
rgb.setColor(0, 0, 10);
buzzer.tone(350, 300);
delay(30);
rgb.clear();
buzzer.noTone();
}
void motorDemo()
{
Serial.print("Move Speed: ");
Serial.println(moveSpeed);
Serial.println("Forward!");
forward();
delay(1000);
Serial.println("Backward!");
backward();
delay(1000);
Serial.println("TurnLeft!");
turnLeft();
delay(1000);
Serial.println("TurnRight!");
turnRight();
delay(1000);
Serial.println("ChangeSpeed to minSpeed and Forward");
changeSpeed(minSpeed);
forward();
delay(1000);
Serial.println("ChangeSpeed to maxSpeed and Forward");
changeSpeed(maxSpeed);
forward();
delay(1000);
Serial.println("ChangeSpeed to maxSpeed and Forward");
changeSpeed(maxSpeed);
forward();
delay(1000);
Serial.println("Halt");
halt();
}
void forward()
{
MotorL.run(-moveSpeed);
MotorR.run(moveSpeed);
}
void backward()
{
MotorL.run(moveSpeed);
MotorR.run(-moveSpeed);
}
void turnLeft()
{
MotorL.run(-moveSpeed/10);
MotorR.run(moveSpeed);
}
void turnRight()
{
MotorL.run(-moveSpeed);
MotorR.run(moveSpeed/10);
}
void halt()
{
rgb.clear();
MotorL.run(0);
MotorR.run(0);
}
void changeSpeed(int spd)
{
buzzer.tone(441, 300);
moveSpeed = spd;
}
void distanceDemo()
{
uint8_t d = ultr.distanceCm(50);
Serial.print("Distance: ");
Serial.println(d);
}
void lineFollowDemo()
{
uint8_t val = line.readSensors();
bool leftIn = false;
bool rightIn = false;
switch (val)
{
case S1_IN_S2_IN:
leftIn = true;
rightIn = true;
break;
case S1_IN_S2_OUT:
leftIn = true;
rightIn = false;
break;
case S1_OUT_S2_IN:
leftIn = false;
rightIn = true;
break;
case S1_OUT_S2_OUT:
leftIn = false;
rightIn = false;
break;
}
Serial.print("LeftIn: ");
Serial.println(leftIn);
Serial.print("RightIn: ");
Serial.println(rightIn);
}
void lightSensorDemo() {
lightStrength = lightSensor.aRead2();
Serial.print("LightStrength: ");
Serial.println(lightStrength);
}