mBot Counting lines and doing a command after counting lines


#1

Hello ladies and gentleman of the mBlock forums. I am need of help with a project I am working on. I am trying to code a program that makes the mbot follow a line and after it passes a gap in the line (1" gap) it counts it. I need the mbot to make a left turn when it passes one gap, make it right when it passes 2 lines and a Uturn when it passes 3 lines. I don’t need you guys doing it for me but I just need somebody to explain how to do this. My class explains it but I just don’t understand it very much and I’m in need of help. Thank you guys so much


#2

Should the bot still follow a line when it does the turns? Or should it turn and then pick up the next line it finds?


#3

Hey Andreas! Thanks for the reply. The mbot needs to be following the lines after it turns


#4

I guess this is an assignment, so just a few hints here.

What you need to do is a state machine. The robot is in a state such as

  1. Following a line, on track
  2. Following a line, left side off track
  3. Inside first gap

List all possible states. Then you need to determine what the possible actions are in each state, and what conditions are there to change to a different state.

So for 2. above, the action would be to turn right and run forward. Conditions to change to a different state would be

  • if line follower sensor detects that both lines are on track, switch to state 1
  • if line follower sensor detects that both lines are off track. switch to state 3

For implementation, make a function for each state.
Unfortunately Scratch does not support CASE statements, so your main program will be lots of nested IF…THEN…ELSE commands.
REPEAT FOREVER
IF state = 1 THEN
state1
ELSE
IF state = 2 THEN
state2
ELSE
IF state = 3 THEN

ENDREPEAT

The state needs to be kept in a variable.

The rest is up to you to figure out :slight_smile:


#5

Hey Andreas! Thanks for the reply but i’m still having a bit of a trouble. Do you want me to define 3 blocks for each state? Can you elaborate a little more cause I am completely new to this and I feel such doof lol. Again thank you for the reply and I appreciate it alot. I would post the track here but i kind of don,t know how to post pictures here.


#6

Track for class.zip (407.0 KB)

I uploaded the track and what it is suppose to do. It is in a zip folder due to the fact that i cannot upload PDF files. Please help and elaborate because I have no clue on how to code at all.


#7

Oh wow, that is a pretty complex assignment for a beginner.

I assume the idea is that the robot should begin below the “Left turn”, drive around the course, turn around in the 3-point-turn and then go back up to the parking and stop there?

I also assume that the robot needs to complete exactly this course, not an arbitrary course, correct? That would make coding much simpler.


#8

Yeah it needs to finish this course. The hard part is the counting the lines part to make the turns. that’s what I just need help with


#9

Start with the line follower part. If the robot leaves the line on the left, run forward left 100 and right at -100 so the robot turns on the spot. Do that until the sensor is on the line again. The n ru forward again.
Right is just the opposite:-)

Define a subroutine for the line follower, you will need that often. I case you detect that the sensor is on the space (gap), leave the subroutine.

Define another subroutine for the gap that runs forward until the sensor is no longer onwhite space.

Now you just need to put a sequence of sunroutine calls together that fits your track
line follower
gap
line follower
gap

Then do the left turn
line follower
gap


#10

Hello, Does anyone know the solution to the problem: http://learn.makeblock.com/en/robot-kits/mbot/line-counting/ ‘With this code, the value of the variable recorded will not be correct. Can you figure out why? And how to correct this?’ Thanks.


#11

Hi johan,
You may try this codes:
image
variable “shuliang” is the number of the times that mBot passed black line.
segment display will show the number.


#12

Thanks for the answer!
The problem stays: as long as the <linefower on port 2> stay above the black line the counter stay counting. When you have a wide line, the counter counts more then 1 count. How to stop counting the counter once above the black line. He has just to tel +1 when he comes above a (wide) black line.
And what’s the reason of your ‘flag’. Our mbot has no segment display.

line%20counter


#13

Flag will let the counter not increase 1 if the black line is too wide until the line follower reaches white line and to black line again.
That logic and programming, you may take more time to consider and learn.
Good luck.


#14

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