How to use the value of a variable when a timer event is set, not when it executes

So I have a function. Since I’m typing this on my iPad I’ll simplify it by a lot since it doesn’t really matter

void randomfunction() {


Now when I run Brain.Timer.Event(specified time, randomfunction) I want it to use the value of x when it runs that Timer command, NOT when it actually gets used. I have a constantly changing variable, which is why. If I use the value of x when it actually executes, it’ll only be the last value of the 2d array (which is what x is)

Or just any way to make it so something the right value gets executed. I’m trying to have separate index’s for each function to access the right part of the 2d array, but doing indexShadow+=1; does nothing. It’s very strange.

This thread talks about the time clock that is the time the program has been running.

You want to use that info for your X

Yes, the recording part is fine. I don’t need that part. It’s unrelated to the time. The issue is x is an integer that I have saved in a 2d array, and an index moves across that array. I need the value of the array when Brain.Timer.Event is called to be used for the function, not when the function is run, as it’ll just skip to the last ones. I tried using a different index but for some reason it isn’t running indexDrive+=1.

Not following what you want to do. So rather than trying to fix your solution, post what you are trying to do. (Oddly we know it in the IT world as the XY problem ). From the description we should be able to help you out.

ok. i cant post my code as i am at home without the laptop. but i can post it tomorrow morning. i would try to type it out here but there is A LOT of context needed. Save and load text file onto/from SD card - #33 by jpearman this code is very similar to what im doing now, except i changed it from executing it in the interpreter to using the Brain.Timer.Event() and i put all the execution as a callback, and i saved the time values of driver input every time the brain refreshed into the arrray.

you can’t do that (if I understand you correctly).

But why ? timer events are not that useful in normal robot programming, Yes you can schedule something to happen in the future, but they only have 10mS resolution and I did not expose any way to pass arguments (you can do that in Python but not C++).


I’m not after the code. What I’m looking for is 3 or 4 paragraphs on what you are trying to do. Like if I was a junior programmer working for you. I want know what you want to do, not the how. Did you look at the link that I posted? I’m after requirements and functionality not the how.


the reaosn why is the refresh rate is what was holding back my code. when it runs the code to make it move the motors, it runs it faster or slower depdning on the refresh rate. meaning the auton is EXTREMELY inconsistent. if i have it constant, there will be a different result every time because the brain moves at different speeds every run ig. the timer code was meant to be extremely specific, and make it exactly the same time as it was in driver control based off the brains clock.

sorry, i actually didnt see it. was kind of in the middle of math tutoring.

so basically im making what people in the forums call a “reader auton”. it reads driver control, executes the movement of the motors just like it did in driver, so that way you can write autons by just driving. the issue is it is extremely annoying to code. ive been on the forums twice already trying to do saving and loading, that eventually got fixed. the major caveat right now though is how the brain is inconsistent with its refresh rate, and i cant make it so it performs the “move motor” functions at the EXACT time the driver did it. it does it to early or too soon.

the way my code works is a 2d array saves the inputs of the controller, and the time of the brain (using Brain.Timer.value()) then its supposed to execute the “move motor” commands at the times specified in the 2d array.

it worked before, just with that speed problem, and ive managed to get 100 points maximum with it (of course, that was one time, and running it again with the exact percise same setup led to 40 points).

im gonna check out your link now

edit: well that was a quick read and applies very well to me in this situation rn

Ok, so you are looking for a way to write all of the driver moves to the SD card and then play them back. You are presently doing 10ms slices, but because of timing some slices are longer and miss an activity.

A few years ago there was a description about an IQ auto translate program. It used the buttons to make the movements. Every time a button was pressed it recorded the action. The button press did a fixed amount each time so the movements would be jerky on input.

For example press forward and it moved the robot forward 1". So 12 pushes would move the robot forward 12". Same thing for rotations. Turns were 15 degrees at a time.

There was a set of posts and code posted by @turbodog, here is a link to video of the robot being programmed. You might want to look at it as an alternative.

You might want to think about that as an alternative. Shadowing something on time intervals is dicey on both the record and playback stages. Using other sensors help in the process.

You might want to consider writing the time stamp out with the data, that way you can try to sync up the time that it happened to the playback times. But you are on a slippery slope, you are really trying to do realtime system in an event driven environment.



If he/she will use the ‘programming toolkit’ the resulting moves will be EXTREMELY close on the very first run, requiring only minor tweaking.

And it’s a fast process overall.

idk. ive spent so much time on this that it feels wimpy to just give up on the idea. its been only a week, but that weeks been anywhere from 5 - 10 hours a day. im gonna try getting the Unix timestamp, but honestly, you may be right. Plus im in the highschool level so i dont think i can use it. HOWEVER i did consider making it with a gps sensor combined with ultrasonic so i can do some weird thingy that makes it accurate. sadly we dont have any sensors besides ultrasonic and color. idk. ill try it tomorrow, let you guys know

it jsut sucks thinking that no one believed i could do it on my team, and i really wanted to get that credit saying “yeah i did it”. there has to be some way to make it semi accurate i think. i just need it to run well 1 time during a tournament really.

Need to remember that it’s the time the program has been running, not a timestamp like the unix timestamp.

You have the added issues of joysticks are analog, so the difference between 80 and 90 is very slight. Thats why the button method works, it has discrete values, its always the same.

A few other options:

  1. put the write in the main code loop, so you are not dependent on the event times. You are really trying to create a log, the main loop would be that place.

  2. If you are only starting one event at a time:
    A) in the last empty row put the timestamp you started the trigger
    B) in a global variable put the row number you just filled.
    C) When the trigger runs, it populates the empty row (it knows the index from the global) and that way has the timestamp,

Or put the timestamp time in a global and have the triggered routine use that.

And I have the rule of 6, it has to run 6 times in a row without error to be declared to be done.

Lastly you have walked away learning the lesson of “Holes”. The first rule of holes is to stop digging. After your first 5 hours, ask around, and remember the XY problem.

Post back what you end up doing.


2 is the idea, not sure how I’d do 1 with 5 different motor groups.

I understand the determination… but the analog stick problem is NOT going away, so even if you succeed… you still sort of fail.


Try slowing things down.
The VEXnet radio will only send new controller values every 25mS.
background processing in the brain runs every 10mS unless you have blocking code like writing to the SD card (which I don’t think you have IIRC) always save to RAM and write/read from SD card at non-critical times. so there may be 5mS jitter, but I would be surprised if that matters too much. But try slowing the capture/playback loop down to 20Hz (ie. 50mS), that may help.


Also, I don’t understand what you are trying to do with this, but if you want the variable value before the timer is set, you could always make a copy of the variable

1 Like

How did you make out with the recommendations?

Well, trying to make it as accurate as possible isn’t going so well. But it does seem like putting the motors on brake, and making the ms 100 does help a lot. I’m trying to do refresh rate now, the inconsistent one, and do time based later. I think I can make it work using Unix time it’s just horrible. I’m genuienly exhausted and don’t think I can do a fully perfect shadow auton at the moment. I knew almost no c++ two weeks ago, and then I learn a ton and made all of this.