mBot Arduino code that works for the ultrasonic sensor


#1

I encountered problems trying to get readouts from the ultrasonic sensor in the mBot. After much tinkering and forum reading (with help from a post by @benjamin.m,) I was able to come up with this code that allows you to get sensor readouts using the COM window while connected via USB cable.

NOTE: Original code from @benjamin.m is in this link.

Modified code, below:

#include <Arduino.h> 
#include <Wire.h>
#include <SoftwareSerial.h>
#include "MeMCore.h"  // note the change in header for mBot

// Please read the comments. They include info to help customize the code for your particular set up.
// This code was borrowed and then revised from this one by @benjamin.m
// You can paste this directly to Arduino. Make sure your mBot is linked to the Arduino IDE using Tools > Port
// To see the ultrasonic readings, open Arduino IDE: Tools > Serial Monitor

MeBuzzer buzzer;  // Initialize buzzer
MeUltrasonicSensor ultrasonic(3); // Initialize ultrasonic sensor. Change the number inside the parentheses to the port where you plugged your sensor

void setup()
{
  Serial.begin(9600); //Begins communication with the computer at a baud rate of 9600
}

void loop()
{
  Serial.print("Distance : "); //Prints the string "Distance : " over the serial most likely the usb. Can be seen using serial monitor in arduino tools setting

  Serial.print(ultrasonic.distanceCm()); //Prints the value received from the Ultrasonic Sensor in Centimeters. Can be changed to inches with .distanceIn()
  // mBot note: .distanceIn() does not seem to work on my mBot. 

  Serial.println(" cm");//Prints the string "cm" followed by a new line

  if(ultrasonic.distanceCm()<20) { //if statement to check if data received is less than the value 20, in this case 20 centimeters
    //If value is true the following code executes if false, code will skip this section
    buzzer.tone(262,500); //turns buzzer on
    delay(500); //waits 500 milliseconds or half a second with a minimum value of 100 or 1/10 of a second
    buzzer.tone(0); //turns buzzer off
  }
delay(100); //wait 100 milliseconds - Increase this for less frequent readings.
}

#2

Hi, I dont know how old this post is, but I would like to know if you can show this code
in mblock form instead of arduino code, for arduino, Im trying to assemble multible block
codes.
Thanks, if your still around, Jayson


#3

Hi Jayson,

I posted it just this July 2016. I’m traveling right now but when I get back I’ll try using mBlock. Meanwhile, please read this thread: http://forum.makeblock.cc/t/programming-the-ultrasonic-sensor/3315. There’s a screenshot there of how to do it via mBlock. You just need to insert a “Say” block to report the results.

I started off by trying to use mBlock, but it seems to introduce some delay that makes the “Say” block to always report 0 distance. This is why I switched to Arduino mode, where I got better results.

Good luck!

has not worked for me and that is why


#4