I am working on a project dealing with OBD2. In a nutshell, I have to light up an LED if certain parameters dealing with RPM and Speed are met.
Now, the problem is that I need 4 sets of data of RPM and speed; and compare them.
For example:
At t=0, the first RPM and speed data recorded are set as R1 & S1 respectively.
Now the next set of data received is set as R2 & S2, and same with 3 and 4.
If the parameters are met for these 4 sets, then light up the led; or if not met, the next set of data received is used instead of the first one.
In other words, the latest 4 sets of data are to be used.
Now, I don't have a bit of idea how to get with it. Any suggestion/guidance regarding this is truly appreciated.
struct {int _RPM; int _MPH;} Entries[4];
byte BufferPointer;
void addEntry(int RPM, int MPH) {
Entries[BufferPointer].RPM = _RPM;
Entries[BufferPointer].MPH = _MPH;
BufferPointer = (BufferPointer+1) % 4; // Modulo operator to give a 0..3 answer
}
// Entries[BufferPointer] is the oldest entry
// Entries[(BufferPointer+1) %4] is the second-oldest entry
// Entries[(BufferPointer+2) %4] is the third-oldest entry
// Entries[(BufferPointer+3) %4] is the newest entry
My first choice would be struct and pointers because storing new data and dropping the oldest is just an exercise in using pointers.
My second choice would be an array of structs.
My third choice would be an array of RPM values and an array of speeds.
The array approaches need a loop to move data to get rid of the oldest.
My fourth choice would be a big bunch of variables and would need a big bunch of code to get rid of the oldest. It also would not be easy to change from saving the last four readings to some other number.
This would work also. I usually avoid division - which is what the modulo operation may entail for values that are not a power of two - but division is likely to be quicker than moving a bunch of data. Good one!
vaj4088:
This would work also. I usually avoid division - which is what the modulo operation may entail for values that are not a power of two - but division is likely to be quicker than moving a bunch of data. Good one!
well also in that case since it's a simple circular buffer you can do it with no division at all since it's just moving one position by one position
you need
a boolean stating if the array has been fully populated, starts at false
an index going through the array
when the boolean is false, data captured are in 0..index
when the boolean is true, you have historical valid data in the array and you can iterate through the array from most recent to oldest with just two for loops (index-1 to 0 whilst >=0) and (max size - 1 to index)
So, where do I need to incorporate this in my code?
johnwasser:
Sounds like a "circular buffer" to me.
struct {int _RPM; int _MPH;} Entries[4];
byte BufferPointer;
void addEntry(int RPM, int MPH) {
Entries[BufferPointer].RPM = _RPM;
Entries[BufferPointer].MPH = _MPH;
BufferPointer = (BufferPointer+1) % 4; // Modulo operator to give a 0..3 answer
}
// Entries[BufferPointer] is the oldest entry
// Entries[(BufferPointer+1) %4] is the second-oldest entry
// Entries[(BufferPointer+2) %4] is the third-oldest entry
// Entries[(BufferPointer+3) %4] is the newest entry
UKHeliBob:
It seems to me that these 2 statements are contradictory
I'm sorry if I messed it up, but I need the last 4 data set received from the OBD for the job.
torquefreak:
For this reason, though I was able to understand the replies, I'm unable to put this in code.
Only in the short period of time that elapsed since the replies... programming takes time. It would be better for you to study the replies than to sail off with a cut and paste job from another program.