// generated by mBlock5 for mBot
// codes make you happy
#include <MeMCore.h>
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
float Speed = 0;
float Distance = 0;
MeUltrasonicSensor ultrasonic_3(3);
MeDCMotor motor_9(9);
MeDCMotor motor_10(10);
void move(int direction, int speed) {
int leftSpeed = 0;
int rightSpeed = 0;
if(direction == 1) {
leftSpeed = speed;
rightSpeed = speed;
} else if(direction == 2) {
leftSpeed = -speed;
rightSpeed = -speed;
} else if(direction == 3) {
leftSpeed = -speed;
rightSpeed = speed;
} else if(direction == 4) {
leftSpeed = speed;
rightSpeed = -speed;
}
motor_9.run((9) == M1 ? -(leftSpeed) : (leftSpeed));
motor_10.run((10) == M1 ? -(rightSpeed) : (rightSpeed));
}
void _delay(float seconds) {
long endTime = millis() + seconds * 1000;
while(millis() < endTime) _loop();
}
void setup() {
/* Arduino code of ultrasonic sensor mBot */
Speed = 50;
while(1) {
Distance = ultrasonic_3.distanceCm();
if(Distance > 25){
if(Distance > 15){
move(1, Speed / 100.0 * 255);
_delay(0.4);
}else{
move(2, Speed / 100.0 * 255);
if(random(1, 100 +1) > 50){
move(4, Speed / 100.0 * 255);
}else{
move(3, Speed / 100.0 * 255);
}
}
}
_loop();
}
}
void _loop() {
}
void loop() {
_loop();
}