Can you create a variable to capture time or distance traveled

How can you use Scratch programming to create a variable that captures the time or distance travelled so you can travel back that same time or distance…

1 Like

Hi! That’s a really interesting question and I think there are a couple ways to solve it. Obviously coding and programming has so many different possible answers, so there may be more.

First you can do using just variables, the timer, and drivetrain blocks. So here, I used a BaseBot (2-motor drivetrain) template and had the robot first drive forward for 800mm. After the robot is done driving forward, the variable is set to be the number of seconds on the timer. So that would set ‘drivetime’ variable to the number of seconds the program has been running. Then I reset the timer so it goes back to zero.

To drive back for the same amount of time, I have the robot driving forward until the timer (that was just reset to zero) is greater than my ‘drivetime’ variable. I used greater than rather than equals to because of general accuracy for the project. The robot will report the values of timers or sensors quickly, but it may not report when the timer is instantaneously at the same time. So by doing greater than, I can ensure it has hit the same value (even if it might be just so slightly above).

I actually tested this project first in VEXcode VR to validate my logic made sense before building the VEXcode IQ version. This could be a cool way for you and your students to test out code like this too! Here is a video of the robot executing the code in VR. You can see the monitor console there so we can see the timer being reset, and I used the pen of the robot to make the forward and backwards driving more easily visible.

Hopefully this helps!

4 Likes

This is such a cool idea for a project. And ever wondered how fast the VEXcode VR robot goes? That’s a nice way to play with variables and maths functions. You can then change the drive velocity% and see what different values equate to in terms of speed in m/s:

Then expand on this code so that the output to the monitor tells you what the velocity% is and what that equates to in m/s. Then for fun, maybe add a bit more maths and see what the speed is in mph! Might need to change the print precision value for that one :rofl:

4 Likes

1 Like

Hi @Maria_Brinza! Thanks for sharing this project! Do you have a question about it at all? Or just sharing? :smiley:

1 Like