Ultrasonic sensor not working in online mode


#1

Hello everyone,
I am using an mbot 1.1 bluetooth with mblock 3.4.11 on mac os. The goal is to move the sprite of the panda using the ultrasonic sensor integrated in the robot, so the robot has to be used in online mode. Here is the code:
29
The problem is that the robot (I’ve tried with multiple mbots and the problem stays the same), always returns the value of 400, indipendently from the distance of the objects in front of the robot.
The sensor is plugged correctly and I’m reading from the right port, in fact, a program using the ultrasonic sensors in offline mode works perfectly.

What can I do? Is my code correct?


#2

Weird, I get the same issue with even simpler code (I just do the assignment and then watch the variable in the Stage window). Oddly, I can confirm that it works with the mBot Program for standalone because I tested it with a similar program that lights up the LEDs based on distance.

The pure Scratch program did not work with the ultrasonic sensor returning a zero.

Replacing the when [green flag] clicked block with the mBot Program block, the program worked properly when I uploaded it to to the mBot.

The Arduino source code that was generated from the second program is:

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

#include <MeMCore.h>

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));
}
double angle_rad = PI/180.0;
double angle_deg = 180.0/PI;
double distance;
MeUltrasonicSensor ultrasonic_3(3);
MeRGBLed rgbled_7(7, 7==7?2:4);

void setup(){
}

void loop(){
    distance = ultrasonic_3.distanceCm();
    if((distance) < (10)){
        rgbled_7.setColor(0,150,0,0);
        rgbled_7.show();
    }else{
        if((distance) < (20)){
            rgbled_7.setColor(0,150,150,0);
            rgbled_7.show();
        }else{
            rgbled_7.setColor(0,0,150,0);
            rgbled_7.show();
        }
    }
    _loop();
}

void _delay(float seconds){
    long endTime = millis() + seconds * 1000;
    while(millis() < endTime)_loop();
}

void _loop(){
}

I wrote a separate version that doesn’t carry all of the code-generation boilerplate code, but it was essentially the same thing logically:

#include <MeMCore.h>

MeRGBLed leds(7, 2);
MeUltrasonicSensor ul(3);
double distance = 0.0;

void setup() {
    Serial.begin(9600);    // open serial port for logging
}

void loop() {
  distance = ul.distanceCm();    // capture the distance in centimeters
  
  Serial.println("Distance: " + String(distance));   // log the distance to the serial monitor
  
  if (distance < 10) {
    leds.setColor(0, 150, 0, 0);    // less than 10 cm red leds
  } else if (distance < 20) {
    leds.setColor(0, 150, 150, 0);  // less than 20 cm, greater than 10 yellow leds
  } else {
    leds.setColor(0, 0, 150, 0);    // greater than 20 cm green leds
  }

  leds.show();   // change the color of the leds 
}

It looks like there is something broken in the Scratch code but @tech_support would have to have the developers at Makeblock investigate further.

Regards,

Chuck


#3

Hi chuck and akiracoci,
Have you upgrade firmware before running online program? I did test both of your codes, they worked in my side.
@akiracoci After upgrade firmware, still get 400? Have you tested with another port, another RJ25 cable with below codes?


#4

Hi @tech_support,

I tried all of those and I’m seeing a zero when I run the program from within mBlock. I also completely reinstalled mBlock 3.4.11 just to be sure. As I mentioned in an earlier post, the sensor seems to work fine with an Arduino sketch so my guess is somehow something has gone wonky with the Scratch code. Have the over-the-air code in Scratch been changed recently?

Best regards,

Chuck


#5

Hi Chuck,
No update or change recently.
If you did upgrade firmware before running the online program. I am not sure the exact cause to be honest.
Will you kindly post a video here?


#6

Your suggestion was right, I changed the cable and now everything works perfectly.
Thanks for the quick reply.


#7

:sunglasses:Congratulations


#8

@tech_support, not sure a video will help. I did the following:

  1. Deleted and reinstalled mBlock 3.4.11.
  2. Installed the Arduino driver.
  3. Connected the mBot with a known to be good USB cable.
  4. Reseated all of the Dupont cables after verifying they had no short circuits.
  5. Connected to the mBot via USB cable1.
  6. Upgraded the firmware successfully.
  7. Ran the following program:

  1. The Scratch environment indicated that the program was running (gold outline around blocks) but nothing occurred on the mBot.
  2. Replaced the when green flag clicked block with the mBot Program block.
  3. Attempted to run the program but had the same result as step 8.
  4. Uploaded the program to the mBot (upload to Arduino).
  5. Program executed successfully.
  6. Reconnected the mBot via USB cable.
  7. Upgraded firmware.
  8. Reset default program.
  9. Repeated step 7 but this time the program executed successfully.
  10. Repeated steps 14-15.
  11. Ran the following program:

19… LEDs only show red.
20. Replaced When green flag clicked block with mBot Program block.
21. Clicked mBot program block.
22. LEDs only show red.
23. Uploaded program to mBot.
24. LEDs light up as green.
25. When sensor detects object within 5cm the LEDs turn red as expected.

For what it’s worth I am running on the following setup:

Item Value
Computer 2018 iMac, 32GB RAM
Operating System MacOS 10.14.1 (Mojave)
Robot mBot 1.0
Software mBlock 3.4.11

#9

It seems that if I do update program or reset default program when the online program is running (golden line), it will cause ultrasonic stop working. Then reopen mBlock 3, connect COM, update firmware or reset default program, it will working. Please have a try.
This won’t happen to windows system, I am not sure if it is the compatibility issue between Mac new system. Welcome to do more test and tell us the result if you don’t mind.


It works as it should be, if I update firmware or reset default program without clicking on the red circle, the number won’t change after finishing update process.


#10

Hi @tech_support,

Just tried removing the WinChipAhead driver and reinstalling it with no new results other than I can now no longer upload the program through the mBlock/Arduino interface and get it to work. My development Arduino environment can still connect and upload programs, but I am not using the one from the mBlock installation (which is extremely old and way out of date).

One other thing I should note. Apple has apparently set up a new serial device driver that the WinChipAhead drive may be conflicting with because I was able to connect my Arduino environment to the mBot via the built-in serial driver until I installed the WinChipAhead driver. After installing the WinChipAhead driver, the standard serial driver shows the device to be busy, so there is a potential conflict that may be part of the issue. I’m not sure that I can troubleshoot things further at this time because I’ve never been able to get the mBlock 3.x code to compile. :frowning:

I guess I’ll focus the rest of my writing with regard to Makeblock products on the Arduino environment because that seems to work consistently and is likely the area where most developers will end up when they reach the limits of the Scratch/Blockly environments.

Regards,

Chuck


#11

Thanks for the feedback. I’ll record it and see if we have same feedback from other users.


#12

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.