Help Needed with Starter Kit motors program


Hello, I am trying to make a simple avoidance program however I am getting some unusual behavior.
I am using the Starter Robot Kit with Bluetooth.
I am able to successfully connect to Bluetooth.
I have made a simple progam that steers the robot with the arrow keys and it works just fine. In this case I use the arrow keys to set the motor speeds. This works perfectly. Space bar sets motor speeds to 0, and arrow keys do the appropriate thing.
HOWEVER, when I put the motor commands inside of if blocks/loops etc. The robot makes a humming noise and jerks a little bit but does not move. In this case I am using the ‘green flag’ to start the program with a forever loop around two if conditions. The little sprite has the correct text based on sensor reading, so when its too close it says “Turn” and when its clear it says “Go Straight” however the motors are not turning and there is the humming noise. I have tried using the sensor block directly instead of variables. Seems the variable is a pointer to the input stream as the ‘dist’ variable is continuously updating in the sprite window. Any suggestions or help would be appreciated.

See attached image:
MB-AvoidTurnRight.sb2 (74.3 KB)


First put the line which reads the distance into the loop. Right now it will never be updated.

Then repeat reading the sensor if it is zero. Wait 0.1 seconds between each try. Reason is that quick reads lead to faulty values.


SOLVED: The problem was with the ‘flag’ event, there is some kind of defect in the mblock program code that is generated I think. The solutions above did not solve it, however when I tied the code to the space bar, the motors started operating correctly. That said I did need to change the way I was using the variable, it is NOT a pointer to the stream, it is only set once so it does have to be updated. I went for directly reading the sensor instead of using a variable because its less code blocks, however I am sure setting the variable in the loop as suggested would work. The confusing part was when using the ‘flag’ event the variable appeared to be updating in real time. The wait 0.1 seconds was not needed.
See attached:
MB-AvoidTurnRight.sb2 (74.3 KB)