Gradually slow down until color detected

We created a cable car that goes until it sees read and then switches directions and goes until it sees blue. How would write a code to slow down as it approaches the red color…

2 Likes

Hi @Maria_Brinza! The cable car sounds awesome. I’m going to ask some questions to best be able to help you.

  • Do you already have code that drives the car in both directions?
  • Do you already have the Optical Sensor mounted on the car? If so, can you show me where with some images?
  • Is the goal to slow down as soon as it sees the red color? Or did you mean something else when you said approaching red?

Thanks! :smiley:

1 Like

We have an optical sensor pointing down to go until it sees red or blue. I could but a distance sensor on and then have a loop that multiples the distance by .1 and makes that the distance to travel…

1 Like

That is a very interesting build and a good question.

There are several ways to handle slowing down as it approaches the targeted stopping point. However, regardless of how it is solved, it will need to know that it is getting close in order for it to start slowing down.

One option is to use another color to mark the point where you want to start to slow down then set the motor to a slower speed. This would be the simplest option as you already have the color sensor and you just need to look for that color in your existing code. Then you can set the motor to use a slower speed when the new color is detected. While this would make it slower, it would not be a smooth change in speed. However, this is concept is the basis of all the other options in that it uses sensor data to adjust the motor speed.

Another variant of the color sensor stop is to use the existing red tap to be the start of the stop. For this, one the sensor detects the red mark, it will use a loop to drive a series of distance based moves, with each move at a slower speed. That could looks something like this.

A more advanced option would be to use another sensor, like the distance sensor, that can give you a range of values. If you just want it to work for slowing down as you approach the stopping point from one side, you can point it towards a flat surface that is where you want to stop. Since the reading will decrease as the sensor gets closer, we can use that to proportionally set the motor speed.

The result would be code like this. You will need to adjust the code to work for you. The values and the exact math will need to change based on your setup, what you are pointing at, and how everything moves.

You could also improve it by adding an offset to the math so that it comes to a smother stop. However you may find that there is a minimum speed for the motor to actually move.

Hopefully that helps and makes sense. If not, maybe someone can help translate my technobabble…

2 Likes

These are really interesting ideas @Jacob_Palnick! Thanks for sharing :slight_smile:

1 Like