mBot Ultrasonic Sensor Code not working


#1

Hi, I have an assessment due in a couple of days and when I try and make the mBot move forward while stopping before hitting something with the ultrasonic sensor, it works.

But when I add more functions, it does not. Can you please help me?

Thank you so much.

[https://ibb.co/LR559W6] (code that works)

[https://ibb.co/hCzsmDr] (code that does not work)


How to code a three mode robot?
#2

I suggest that you make a code like this:

When mbot starts
Forever
If ultrasonic sensor range cm < 30 then
Turn right at power 50%
Else
Move forward at power 75%

Your mbot code doesn’t work because mbot cannot multitask.

About Multitasking

In the context of computers, multitasking refers to the ability of an operating system or a computer system to execute multiple tasks or processes concurrently. Here are some key points about multitasking in computers:

Multitasking allows a computer to run multiple applications or processes simultaneously, giving the illusion of concurrent execution.
Modern operating systems, such as Windows, macOS, and Linux, have built-in multitasking capabilities.
There are two main types of multitasking: preemptive multitasking and cooperative multitasking.
Preemptive multitasking is the more common approach, where the operating system allocates time slices to each task and switches between them based on priorities or time-sharing algorithms.
Cooperative multitasking relies on the cooperation of individual programs to yield control to other tasks when they are idle. This approach is less common in modern operating systems.
Multitasking enables users to switch between different applications or tasks seamlessly, allowing them to work on multiple things at the same time.
It improves overall system efficiency, as idle time can be utilized by other tasks, maximizing the utilization of system resources.
Multitasking can be seen in action when you have multiple programs running simultaneously, like browsing the internet, listening to music, and working on a document, all happening concurrently.

It’s important to note that the efficiency of multitasking depends on factors such as the system’s resources (CPU, memory), task prioritization, and the capability of the operating system to manage and allocate resources effectively.

Because mbot cannot multitask, it has trouble when it runs the 0.6 second delays and can’t run the other part of the if. If you want me to make you a different code, I will be happy to!


#3

Hey, I’m still a bit confused. Could you make me a different code?

Also in my code, I plan for the mBot to move around a certain track that I built, hence the quick turns left and right. You’re code, I think suggests that it only turns right once it detects something, but that would not work for my project.


#4

OK, when I find time, I will make you
a code!


#5

Thank you so much!

Just letting you know that I need it by tomorrow afternoon. But if you don’t have time, that’s fine.


#6

OK, thanks!


#7

Try this:


#8

Sorry you can only see some of the code.

You can download it here:
https://share.eu.internxt.com/sh/file/c83a38447a150c87d6a9/79a95219aaef614e55ec138860c04e6f5ab85820608017206a9c89d41f6f89da


#9

Hi, thank you so much! I am going to test it this afternoon.

Just a quick question, you made the functions for going forward and turning left, but how would I make one for turning right?

(I made a mistake with the code I sent haha)


#10

Nevermind, I’ve figured it out!


#11

Great! If that code doesn’t work, here is a simple code that I made:


#12

Let me know if it works or if there are any bugs! I’ll be here to help.

P.S.
The second code is better.

But don’t forget to change the custom block to right, sorry I forgot.


#13

Great! Thanks so much for helping.


#14

No problem! Here is the file for download:

https://secure.eu.internxt.com/sh/file/bbd0e4cf33ab4f67caf8/8b97f35988ab0558b8ef0251f38c2c6c9ddead688dc9bddc7f734a53b1426e3b


#15

Hey, sorry but I just checked and the code does not seem to work.

Is it because the code is for mBot2 and I have mBot?


#16

My code is for mbot. What went wrong?


#17

@BishalK I’m so sorry I found the problem. Add a block

Set time_trig to 0

To the end of the define drive forward… And Define drive right… Codes.

Thanks for telling me! I’m so sorry!!
:disappointed_relieved:


#18

That’s fine!

Why do I get this error while trying to upload to the mbot?

Command failed: avr-toolchain\bin\avr-g++ -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10605 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I"C:\Users\Public\Programs\mblock\resources\app\mlink-v1\external\arduino\avr-library\variants\standard" -I"avr-library/cores/arduino" -I"arduino-libraries/makeblock/src" -I"arduino-libraries/makeblock/src/utility/avr" -I"avr-library/libraries/Wire/src/utility" -I"avr-library/libraries/Wire/src/" -I"avr-library/libraries/EEPROM/src/" -I"avr-library/libraries/SPI/src/" -I"avr-library/libraries/SoftwareSerial/src/" -I"arduino-libraries/arduino/WiFi/src/" -I"arduino-libraries/arduino/SD/src/" -I"arduino-libraries/arduino/Bridge/src/" -I"arduino-libraries/arduino/Temboo/src/" -I"arduino-libraries/arduino/Servo/src/" -I"arduino-libraries/arduino/Ethernet/src/" -I"arduino-libraries/arduino/TFT/src/" -I"arduino-libraries/arduino/SpacebrewYun/src/" -I"arduino-libraries/arduino/LiquidCrystal/src/" -I"arduino-libraries/arduino/GSM/src/" -I"arduino-libraries/arduino/Adafruit_Circuit_Playground/" -I"arduino-libraries/arduino/Adafruit_Circuit_Playground/utility/" -I"arduino-libraries/arduino/Esplora/src/" -I"arduino-libraries/arduino/Firmata/" -I"arduino-libraries/arduino/Firmata/utility/" -I"arduino-libraries/arduino/Keyboard/src/" -I"arduino-libraries/arduino/Mouse/src/" -I"arduino-libraries/arduino/Robot_Control/src/" -I"arduino-libraries/arduino/Robot_Motor/src/" -I"arduino-libraries/arduino/RobotIRremote/src/" -I"arduino-libraries/arduino/Stepper/src/" “C:\Users\bisha\mblock-avr\temp\build\code.cpp” -o “C:\Users\bisha\mblock-avr\temp\build\code.o” C:\Users\bisha\mblock-avr\temp\build\code.cpp: In function ‘void Drive_right_at_speed_N_For_N_Seconds_until_B(double, double, boolean)’: C:\Users\bisha\mblock-avr\temp\build\code.cpp:56:13: error: ‘speed’ was not declared in this scope move(4, speed / 100.0 * 255); ^~~~~ C:\Users\bisha\mblock-avr\temp\build\code.cpp:56:13: note: suggested alternative: ‘Speed’ move(4, speed / 100.0 * 255); ^~~~~ Speed C:\Users\bisha\mblock-avr\temp\build\code.cpp: In function ‘void Drive_left_at_speed_N_For_N_Seconds_until_B(double, double, boolean)’: C:\Users\bisha\mblock-avr\temp\build\code.cpp:77:13: error: ‘speed’ was not declared in this scope move(4, speed / 100.0 * 255); ^~~~~ C:\Users\bisha\mblock-avr\temp\build\code.cpp:77:13: note: suggested alternative: ‘Speed’ move(4, speed / 100.0 * 255); ^~~~~ Speed

Failed to compile file code.cpp

arduino build failed.


#19

Did you use the code I made


#20

I got an mbot so this code is tested:

https://share.ue.internxt.com/sh/file/0a8a4fcf8fdf5f4ea4b0/33bad5278fc6d827dffb20ecafdee1063db6a34fe79f97c35c587b42405d59ae

It worked for me.

@BishalK

Although you might change the distance up to 15 cm or so.