I have been sketching a moped computer with my son. What I try to achieve is simple meter that displays speed, records distances and info about RPM. Later also info about different liquids like engine temp and oil and gas. I also will put limits to RPM and speed. I happen to have NodeMCU for the job, and wanted to learn micro python while doing it. I have basic python skills, nothing fancy.
So I am successfully running and detecting spark and hall sensor. Now I want to record times of those, and filter out the noise. At this point I'd like to do minimal stuff in interrupt while rising edge of pin happens. I want to increase a counter and compare time between this and previous interrupt, to filter out noise (debounce).
For this I was reading manuals, and understood I can use global vars of integers, bytearray or array. It kinda works with just increasing the index with global variable for it. But what is the size of time_ms() variable, if I'd use array to store those? How should it be done?
I could use array of few time values for counting average speed of interrupts, and I'd like to drop any value which is e.g. < 5ms from the previous interrupt as noise. Now I didn't figure out how to initialize such array for for storing times. What's the sizeof(time_ms)?
I tried with globals like this:
Code: Select all
micropython.alloc_emergency_exception_buf(100)
ARRAYSIZE = const(20)
data = array.array('H', 0 for x in range(ARRAYSIZE))
spark_index = 0
prev_spark_ms = 0
this_spark_ms = 0
def callback_spark(p):
global spark_index, prev_spark_ms, this_spark_ms
this_spark_ms = time.ticks_ms()
# filter away any bounces under 12k rpm (< 5ms)
if (time.ticks_diff(this_spark_ms, prev_spark_ms) > 4):
spark_index += 1
prev_spark_ms = this_spark_ms
1. Is it OK to use time.tics_ms() in interrupt?
2. Is size of tick_ms enough to fit into integer?
3. If I'd use array like the data above, what should be the size for time_ms() entry there?
With this I currently get some memory allocation errors, so clearly something wrong there. I have a main loop which initializes the values and assigns the callback to pin. The main loop checks spark index has grown and does the calcs for RPM.
Happy to get guided a bit. Micro python seems fun, easy to explain this stuff to son with no coding experience. Thanks in advance!