mBot Arduino Program


#1

Hi!
I spent a couple of days setting up a couple of mBots with an Arduino program and I’d like to share it if you like. This gives you the framework to use the following MakeBlock devices with your mBot:
Internal RGB Leds
Onboard Button
Ultrasonic Sensor
Line Follower
LED Matrix
IR
Buzzer
Temperature
7 Segment Display
Potentiometer
Sound Sensor
PIR Motion Sensor
Light Sensor
Gyro
External RGB Leds
Joystick
Limit Switch
Line Follower Array
Bluetooth
Motors 1 & 2

You’ll be able to receive Bluetooth data, either as single characters or as delimited strings.

You’ll be able to send IR data between two mBots.

I haven’t commented it out too much, but it’s pretty self-explanatory once you get the hang of it.

You can load it with the Arduino IDE.

Many thanks to all the people who contribute to these forums. I stole a lot from you all!

Have a great day!

John

#include <Wire.h>
#include “MeMCore.h”
#include <SoftwareSerial.h>
MeRGBLed rgb;
MeUltrasonicSensor ultr(PORT_3);
MeLineFollower line(PORT_2);
MeIR ir;
MeSerial se;
MeBuzzer buzzer;
Me7SegmentDisplay seg7Disp(4);
MePotentiometer potentiometer(4);
MeSoundSensor soundsensor(4);
MePIRMotionSensor pir(4);
MeLightSensor lightsensor(6); // Onboard light sensor
MeGyro gyro(1);
MeLEDMatrix ledMtx(4);
MeRGBLed rgbled(1, 1==7?2:4); // 4 LEDs
MeJoystick joystick(4);
MeTemperature temperature(1,1); // temperature - port 1, slot 1
MeLimitSwitch limit_switch(1,1); // Limit Switch Port 1 Slot 1
SoftwareSerial myBluetooth(0, 1); // RX, TX

MeDCMotor MotorL(M1);
MeDCMotor MotorR(M2);

String s;
String buffer = “”;
int moveSpeed = 200;
int minSpeed = 48;
int factor = 23;
uint8_t distance;
int LineFollowFlag=0;
unsigned char drawBuffer[16];
unsigned char *drawTemp;
const int DataPin = 10; // Data pin on Port 2 for Line Follower Array
uint8_t Sensor_Data[3]; // Line Follower Array

#define NTD1 294
#define NTD2 330
#define NTD3 350
#define NTD4 393
#define NTD5 441
#define NTD6 495
#define NTD7 556
#define NTDL1 147
#define NTDL2 165
#define NTDL3 175
#define NTDL4 196
#define NTDL5 221
#define NTDL6 248
#define NTDL7 278
#define NTDH1 589
#define NTDH2 661
#define NTDH3 700
#define NTDH4 786
#define NTDH5 882
#define NTDH6 990
#define NTDH7 112

void setup() {
Serial.begin(115200);
myBluetooth.begin(115200);
rgb.setNumber(16);
rgb.setColor(10, 0, 0);
buzzer.tone(NTD1, 300);
delay(300);
rgb.setColor(0, 10, 0);
buzzer.tone(NTD2, 300);
delay(300);
rgb.setColor(0, 0, 10);
buzzer.tone(NTD3, 300);
delay(300);
buzzer.noTone();
ir.begin();
gyro.begin();
}

void loop() {
//Serial.println(get_line_follower_array(),BIN);
//delay(100);

//seg7Disp.display(100);

/***** Use this code when you want separators and a carriage return *****/
// if (myBluetooth.available()) {
// 	char c = myBluetooth.read();
// 	if (c == '\r') {
// 		parseBuffer();
// 	} else {
// 		if (isAlphaNumeric(c) || c == '/') {
// 			buffer += c;
// 		}
// 	}
// }

/***** Use this code when you want to send single characters *****/
if (myBluetooth.available()) {
	char c = myBluetooth.read();
	if (isAlphaNumeric(c) || c == '/') {
		buffer += c;
		parseBuffer();
	} else {
		buffer = "";
	}
}
get_ir_command();
//get_line_followers();
//get_ultra();
//get_gyro();
//get_pir();
//get_sound();
get_button();
//get_potentiometer();
//get_lightsensor(); // Onboard light sensor
//show_face();
//get_joystick();
//show_leds();
//get_temperature();
//get_limit_switch();

}

/***** Used by the line follower array ******/
uint8_t get_line_follower_array() {
long time_out_flag = 0;
pinMode(DataPin, OUTPUT);
digitalWrite(DataPin, LOW);
delayMicroseconds(980);
digitalWrite(DataPin, HIGH);
delayMicroseconds(40);
pinMode(DataPin, INPUT_PULLUP);
delayMicroseconds(50);
time_out_flag = millis();
while((digitalRead(DataPin) == 0)&&((millis() - time_out_flag) < 6));
time_out_flag = millis();
while((digitalRead(DataPin) == 1)&&((millis() - time_out_flag) < 6));
for(uint8_t k=0; k<3; k++)
{
Sensor_Data[k] = 0x00;
for(uint8_t i=0;i<8;i++)
{
time_out_flag = millis();
while(digitalRead(DataPin) == 0&&((millis() - time_out_flag) < 6));
uint32_t HIGH_level_read_time = micros();
time_out_flag = millis();
while(digitalRead(DataPin) == 1&&((millis() - time_out_flag) < 6));
HIGH_level_read_time = micros() - HIGH_level_read_time;
if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100)
{
Sensor_Data[k] |= (0x80 >> i);
}
}
}
if (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0]))
{
return Sensor_Data[0];
}
}
void get_potentiometer() {
if (potentiometer.read() < 500) { // potentiometer in port 4
buzzer.tone(NTD2, 300);
}
}
void get_limit_switch() {
if (limit_switch.touched()) { // limit switch Port 1 Slot 1
buzzer.tone(NTD2, 300);
}
}
void get_button() {
pinMode(A7,INPUT);
if((0^(analogRead(A7)>10?0:1))){ // Onboard button
ir.sendString(“A”);
//buzzer.tone(NTD2, 300);
}
}
void get_sound() {
if (soundsensor.strength() > 500) { // soundsensor in port 4
buzzer.tone(NTD2, 300);
}
}
void get_pir() {
if (pir.isHumanDetected()) { // pir motion sensor in port 4
buzzer.tone(NTD2, 300);
}
}
void get_ultra() {
distance = ultr.distanceCm(50);
if (distance < 10) {
buzzer.tone(NTD1, 300);
} else {
buzzer.tone(NTD2, 300);
}
}
void get_gyro() {
gyro.update();
seg7Disp.display(gyro.getAngle(1));
delay(100);
}
void get_temperature() {
seg7Disp.display(temperature.temperature());
delay(100);
}
void get_lightsensor() {
seg7Disp.display(lightsensor.read());
}
void get_joystick() {
seg7Disp.display(joystick.read(1)); // x-axis
seg7Disp.display(joystick.read(2)); // y-axis
}

/*********** The LED Matrix **/
void show_face() {
unsigned char drawBuffer[16];
ledMtx.setColorIndex(1); // 1 - Normal, 0 - Negative
ledMtx.setBrightness(4); // 0 - 8
ledMtx.showNum(56,3);
delay(1000);
ledMtx.drawStr(1,1+7,“Hi”);
delay(1000);
ledMtx.showClock(12, 34, PointOn); // clock with separator
delay(1000);
ledMtx.showClock(12, 34, PointOff); // clock no separator
delay(1000);
drawTemp = new unsigned char[16]{0,0,0,0,48,72,68,34,68,72,48,0,0,0,0,0};
memcpy(drawBuffer,drawTemp,16);
free(drawTemp);
ledMtx.drawBitmap(0,0,16,drawBuffer);
delay(1000);
ledMtx.clearScreen();
delay(1000);
}
void show_leds() {
rgbled.setColor(2,20,60,150); // 0-all 1-1 etc, red, green, blue
rgbled.show();
}
/
Break apart the data. Used / as a separator *******/
void parseBuffer() {
String strTemp;
//delay(10);
String values[6];
buffer = “/” + buffer + “/”;
int count = 0;
int startIndex = 0;
int endIndex = 0;
int len = buffer.length();
if (len < 1) {
return;
}
while (true) {
startIndex = buffer.indexOf("/", endIndex);
endIndex = buffer.indexOf("/", startIndex + 1);
strTemp = buffer.substring(startIndex + 1, endIndex);
values[count] = strTemp;
count++;
if (endIndex == len - 1) break;
}
char charFirst = values[0].charAt(0);
switch (charFirst) {
case ‘A’:
buzzer.tone(NTD1, 50);
break;
case ‘B’:
buzzer.tone(NTD2, 50);
break;
case ‘E’:
buzzer.tone(NTD3, 50);
break;
case ‘F’:
buzzer.tone(NTD4, 50);
break;
case ‘U’:
buzzer.tone(NTD5, 50);
break;
case ‘L’:
buzzer.tone(NTD6, 50);
break;
case ‘R’:
buzzer.tone(NTD7, 50);
break;
case ‘D’:
buzzer.tone(NTDH1, 50);
break;
case ‘S’:
buzzer.tone(NTDH2, 50);
break;
//myBluetooth.println(values[1]);
//myBluetooth.println(values[2]);
//myBluetooth.println(values[3]);
//Serial.println(strTemp);
default:
buffer = “”;
}
buffer = “”;
}

void get_line_followers() {
uint8_t val;
val = line.readSensors();
switch (val) {
case S1_IN_S2_IN:
buzzer.tone(NTD1, 300);
break;
case S1_IN_S2_OUT:
buzzer.tone(NTD2, 300);
break;
case S1_OUT_S2_IN:
buzzer.tone(NTD3, 300);
break;
case S1_OUT_S2_OUT:
buzzer.tone(NTD4, 300);
break;
}
}

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 Stop() {
MotorL.run(0);
MotorR.run(0);
}

void get_ir_command() {
if(se.equalString(ir.getString(),“A”)){
Forward();
delay(1000);
Stop();
buzzer.tone(NTD1, 50);
}
static long time = millis();
if (ir.decode())
{
uint32_t value = ir.value;
time = millis();
switch (value >> 16 & 0xff) {
case IR_BUTTON_A:
buzzer.tone(NTD1, 300);
rgb.setColor(10, 10, 10);
break;
case IR_BUTTON_B:
buzzer.tone(NTD2, 300);
break;
case IR_BUTTON_C:
buzzer.tone(NTD3, 300);
break;
case IR_BUTTON_D:
buzzer.tone(NTD4, 300);
break;
case IR_BUTTON_E:
buzzer.tone(NTD3, 300);
break;
case IR_BUTTON_F:
buzzer.tone(NTD3, 300);
break;
case IR_BUTTON_PLUS:
Forward();
buzzer.tone(NTD4, 300);
break;
case IR_BUTTON_MINUS:
Backward();
buzzer.tone(NTD4, 300);
break;
case IR_BUTTON_NEXT:
TurnLeft();
buzzer.tone(NTD4, 300);
break;
case IR_BUTTON_PREVIOUS:
TurnRight();
buzzer.tone(NTD4, 300);
break;
case IR_BUTTON_9:
buzzer.tone(NTDH2, 300);
break;
case IR_BUTTON_8:
buzzer.tone(NTDH1, 300);
break;
case IR_BUTTON_7:
buzzer.tone(NTD7, 300);
break;
case IR_BUTTON_6:
buzzer.tone(NTD6, 300);
break;
case IR_BUTTON_5:
buzzer.tone(NTD5, 300);
break;
case IR_BUTTON_4:
buzzer.tone(NTD4, 300);
break;
case IR_BUTTON_3:
buzzer.tone(NTD3, 300);
break;
case IR_BUTTON_2:
buzzer.tone(NTD2, 300);
break;
case IR_BUTTON_1:
buzzer.tone(NTD1, 300);
break;
case IR_BUTTON_0:
buzzer.tone(NTD1, 300);
break;
case IR_BUTTON_SETTING:
Stop();
buzzer.tone(NTD1, 300);
break;
}
}
else if (millis() - time > 120) {
time = millis();
}
}


#2