Track Number of Turns and Changing Color


#1

Hello! I am new to mBot and coding and I am having problems making my code transfer over to my robot. What I am trying to do is move forward at speed 100 until it gets within 7 cm of an object and turn right. Once it turns right, the lights must turn green and sound D3 as whole notes distinctly 5 times.

The problem I am having is keeping track of the total number of turns using the variable “TurnCount” and once the TurnCount reaches 3, the lights have to turn red and play D4, 4 times. Also the robot must start on the start line and stop on the finish line.

Please help! Below is what I have so far. Everything works except the turn count.


#2

Your problem is that you put “set TurnCount to 0” in the loop. You should put this block after “when mbot starts up”. Also you should put the “if TurnCount = 3 then” block in the “forevere” block.


#3

This worked, however, now the robot will not stop at the “end line” and instead keeps moving through it. Did I put the code in the wrong area/place?


#4

How do you detect the end line, with the variable Linevar ? I never use the mbot so I don’t know how work the line follower sensor.


#5

What color is your end line? If you can tell me, I can give you a code!


#6

That would be awesome! The line is black.


#7

I was given that code and it originally worked before I added the turn count. There are certain numbers for the sensors and what they detect. When the line variable is 3, both of the sensors have to see black to stop.


#8

Can you add a print that show the value of Linevar and try your program to see the behaviour of the robot.


#9

I apologize, what do you mean by add a print? I do not know anything else about the linevar that just what I was given in order to make the mbot start and stop on the black line. If it does not detect it, the robot should continue to move forward. That is why I am thinking it is just a placement issue.


#10

On mbot you don’t have a block that allow you to show a text ?


#11

This is what my screen looks like. Are you referring to Arduino?


#12

In “Looks” can you use “LED panel port1 shows text hello” and replace hello by Linevar and put this block after “set Linevar to line follower sensor port1 value”.


#13

Should I change the port value to 2 since I do not have anything in 1?


#14

Well did you have a LED panel ? If yes change the port to the one its connected.


#15

It is showing the correct colors and tone but it is not stopping after hitting the line. I was just thinking there was an error in my placement of the code “if turn count = 3”.


#16

I think the problem is that mbot doesn’t support multi-threading, so when you ask to repeat something (specially play a tone) the mbot can’t check at the same time the line follower sensor and can miss the end line.


#17

Okay and I do not have an LED panel. I just have the LED on top. Nothing is connected except for the line sensor and the ultrasonic sensor.


#18

Can you try your code without the repeat block, and see if the mbot stops at the end line.


#19

I could but how do you then get the tone to play 1 beat for 4 times distinctly?


#20

If you really want play a tone you should reduce every waiting time, btw 1 beat = 1 second.