I am using mBlock to generate an Arduino project. It consists in using Motor shield L293D and HC-SR04 Ultrasonic Sensor PCM libraries for an Arduino UNO car of 4 motors.
The problem states:
Move the Arduino car forward and when faced with an obstacle less than 30 cm away, stop for 5 seconds and go back.
My question is: Is the _delay(3) necessary for the purpose of the exercise?
I think we want to move backward as long as we are near the object (< 30 cm); then go forward to, again, enter the proximity. So the delay of 3 seconds is not necessary.
I forgot to mention it at the beginning, I can't access the Arduino car these days, so I'm programming blind. I wanted to record a video tutorial explaining the programming to have the class ready, and that delay makes me hesitate whether to include it or not. That's why I'm asking for help, because at the moment I don't have a way to test it working.
Thank you for pointing it out. For sure it is not optimized (it is generated by mBlock), but we are focusing on another aspect.
As stated before, at the moment I am unable to test. If we have to dissipate the motor (for each one of four motors), do we need to add a 'Stop motor' with a delay after the backward movement? Sorry, I am not specialized in electronics.
I hope it wasn't some sort of AI, in general AI is a bit rubbish at generating code, and is apt you just make things up. So If you can't read and understand what it has made up then you are up the creak without a paddle.
A basic property of the way forces at a distance moves. So its Physics.
But this topic is not about optimizing code. It is intended for an introductory course of high school students, and my question is about a small part of the code.
If the delay command was inserted by the program that translated the mBlock diagram to Arduino code, ask the developers of that software whether the delay has a purpose.
In general when reversing motor direction, the code should first brake or coast, and wait for the motor to stop completely, before starting out in the reverse direction. Otherwise the motor driver can be overloaded and damaged.
The Arduino code was generated because of I block-coded the solution. I attached you the source code, but the purpose of the question has nothing to do with source code, only the block-code solution. Thank you for pointing it out.
Evidently, whoever wrote the program to translate the block diagram to Arduino code likes to insert arbitrary delays.
However, I don't see anything in the generated Arduino code that performs a command corresponding to "wait 5 seconds", for example.
Any means of ensuring that motor rotation is zero, before the direction of rotation is changed, will avoid creating above mentioned problem for the motor driver.
The kids don't code with Arduino, they use blocks. That's why it's irrelevant to consider the source code. If I had to program in Arduino, I would never use what mBlock produces because it is a mess, I would write it by hand, but that is not what is relevant to this topic.
Thank you for explaining this, it makes it a lot more clear what is going on.
However, I must say that the Arduino code generated is absoloutly appalling. Both in structure and implementation.
I have used many sorts of block code implementation, Scratch, Lego, DF robotics, and a few other that I can't recall, but I have never used one that shows you the translation into C code, although they all must do it. I wonder if they are all so bad?
No I think not. It will only delay the detection of the new distance measurement and you don't want to do that.
On the face of it, then it looks acquitted, and the delays you have put into it are needed.
In the Arduino code (real life) you need a delay between pulsing the send in the ultrasonic sensor and waiting for the echo to return, the time it takes to return a signal back to the receiver part of the ultra sonic sensor. But you need a delay first to make sure that the ultrasonic signal has died down so you don't receive your own transmission and so record a distance of zero.