Counting Color Algorithm Activity Question

Good afternoon,
I have altered the Counting Color Algorithm activity to count the red lines as it drives. When it gets to the end of the line, it turns around, drives and keeps counting until more than 10 lines are counted. I also coded it so that if green lines are touched, the red line number goes down. When the variable for the red lines is more than 10, the robot needs to turn right (this is just so I know when the program ends).

The problem is that the robot does not recognize the wall with front distance sensor. Is that playground setup so that the front distance sensor should be able to detect the wall?
I have included the vrpython file if it helps.
MSherry_Counting_Algorithms.vrpython (1.9 KB)
Thanks so much!
:slight_smile: Michelle

1 Like

I took a look at your code. it looks like the issue is that you have a 5 second delay in your code when the sensor detects red (line 42). While this might not seem like it would cause a problem, this prevents the logic from running during that delay. So if when the sensor first sees the red line, it would take less than 5 seconds to detect/hit the wall, you will end up hitting the wall before the code looks to see if the wall is close.

There are several ways to handle this.

  • replace the delay with code to wait for the sensor to no longer see the red line before resuming the logic
  • add a variable so that you only update the values if the sensor value has changed. In other words, only check the color if the last color detected does not match the current color
  • you can use threads to process the code in parallel. This would let you have one thread constantly checking the colors and controlling the count. the other thread would always be checking to see if the robot need to turn around.

If you are less familiar with programming I would recommend the first option. The code for that solution could look something like this. (There are several ways to make the code wait until it no longer sees the red, so the exact code could be different)

if down_eye.detect(RED):
    lineRed = lineRed + 3
    while down_eye.detect(RED):
        wait(5, MSEC)

This will make sure that it does not continue to check for lines while it still sees the red line. You will also probably want to have something like this for the green lines as well to make sure that it only detects it once.

4 Likes

Thank you so much Jacob. I thought maybe there wasn’t a wall but your explanation made sense. I made the correction and it worked. I really appreciate the clarity and thoroughness of your response. Have a great day-
Michelle

2 Likes