hello. I'm trying to read the location of an encoder implanted in a treadmill so that i can get the speed of mice running on it.
but i found the output was incomplete. code like this:
i found the output of running distances was fewer than the actual movements. Is this related to the limit of memory size or string?
Besides, after i added another variable which also increased with time just like 'dist_Rel_Time', i found both of the two variables became shorter (even more incomplete).
The above variables are multibyte variables. You will need to disable interrupts while reading or manipulating these variables from loop() or any functions it calls.
Sorry, I don't quite understand why I need to disable the interrupt. Is this possible to lose some data when reading these variables if not disabling the interrupt?
So in this code, I need to use the noInterrupts() before
and the trialStartMillis was set in the beginning of each trial.
that is before
by the way, I still worry about the memory size of arduino. Do you think it's possible that the variable dist_Rel_Time was too large for arduino to memorize? How could I test this? If it's the memory size, is there any method to print the variables before the storage runs out? So I can get the complete data.
yeah, I know. I reset the variable in the beginning of each trial. But my goal is to get the speed during a trial. My strategy is to get the time when the encoder moved and the corresponding location. This seems to be necessary.
Do you have any suggestions to avoid too long variable for data loss during a trial?
The interrupts will continue to work even if the code is stuck in that delay(10000) statement, so that string dist_Rel_Time would still continue to grow as long as a mouse is active on that treadmill.
You are doing quite a lot of processing in the ISR EncoderCal().
This is presumably driven by pulses from the encoder connected to the treadmill.
Surely, you just need the start time of the session, the number of pulses (relative/absolute) and the session end time. Building up a long string should definitely not be done in that interrupt service routine.
How are you intending to start a session ? Push a button or wait for the first pulse from the encoder or a just a simple time slot or what ?
Is the length of a session fixed, say 10 seconds from the start, or what ?
As the mouse could run at different speed during a trial, the duration of trial and the number of pulses are not enough to calculate the speed at specific timepoint. But the sample rate was not demanding (30 Hz was enough).
I suddenly get an idea. Maybe millis() could be helpful. I mean I can get the location after a fixed duration. But another problem is that I found millis() could not work together with delay(). As I need the trial at a relative fixed duration (not too short), I should learn more about this. (Sorry, maybe this was too puzzling)
In my design, the mice would get some stimulation for a fixed duration (like 15 seconds). But actually, the stimulation system is affected by the mice's behaviors. It's a feedback system. So the durations of each trial vary from 17 s to 30 s. When all the code runs out, the session ends and another starts. Maybe I should change the design to make the session shorter.
I get it that you want performance statistics from these mice.
You can certainly do all sorts of things:
Maximum speed in a session
Average speed in a session
Length of complete pause within a session.
Duration of a session.
You can break each session into a number of sub-sessions (time windows) and display the
results on the serial monitor as each sub session end. At the end of a complete session
you can display the aggregate results for a whole session.
Use millis() for the timing or, if the mice are really fast and the encoder has a high resolution, then maybe micros().