I'm trying to record the times of each input to an interrupt, and regularly process the results, resetting the data. I am using two arrays, one of which is used to record the data, the other becomes a copy of the first before processing the data.
The arrays are supposed to hold the millis() at the time of the input.
Unfortunately, when I analyse the data, the arrays appear to be empty.
Where did I go wrong?
volatile unsigned long buttonPushes[133];
unsigned long calcPushes[133];
const byte interruptPin = 2;
volatile int buttonPushCount = 0 ;
int calcCount = 0;
void setup() {
Serial.begin(9600);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), buttonPush, FALLING);
buttonPushes[buttonPushCount] = millis();
}
void loop() {
delay(10000); // period between analysis
calcCount = 0;
while (calcCount < buttonPushCount) {
calcPushes[calcCount] = buttonPushes[calcCount];
calcCount++;
}
buttonPushCount = 0;
if (calcCount == 0) {buttonPushes[0] = millis();}
else buttonPushes[buttonPushCount] = calcPushes[calcCount];
Serial.print("calcCount = ");
Serial.println(calcCount);
Serial.print("Total time between start and last push: ");
Serial.println((calcPushes[calcCount] - calcPushes[0]));
Serial.print("Time of last button push: ");
Serial.println(calcPushes[calcCount]);
}
void buttonPush() {
buttonPushCount++;
buttonPushes[buttonPushCount] = millis();
}
That's just there so that if there had been no activity in the previous time period, the start time is set to the current time, and if there had been activity in the previous period, the time of the last input is passed to the start of the new period. The value passed in the "else" option is the time of the last input.
Thank you for that. I've changed these:
volatile unsigned long buttonPushes[133] = {};
unsigned long calcPushes[133] = {};
and I'm now getting some numbers. I assume that's what you meant, so I'll set up some calculations with the results to see if they make any sense.
I'm not seeing where or how you make sure not to gather more than 133 samples.
What is making the interrupts? What is the significance of the dimension 133? Is it possible you are getting more than 133 interrupts in your sample window?
Just now that is the only way I can interpret @DrDiettrich's several warnings about the undefined variable.
Your fix in #5 changes nothing; global variables are initially zero, so changing out
I don't know a word for it. I would not use undefined as it sparks in at least my imagination the idea that you are pointing out reference to an array member outside the dimensions of the array, as it " that provokes undefined behaviour".
So in casual conversation I might call it invalid , trash , junk or leftover form last time.
That is to say it is there, but holds a value that is without meaning and might be messing with the analysis of the truly gathered samples.
But maybe it is good you caused me to look at what you were pointing out and wonder what you could possibly mean!
OK, I wondered if chatGPT might help here, fun! It took a few "course corrections" to get it to understand us, but eventually it typed this, long winded it is.
I see. In that case, you can describe this issue as "Off-by-one Error" or "Off-by-one Bug." This term is commonly used in programming to refer to situations where code incorrectly accesses data located one position before or after the intended location in an array or data structure.
The "Off-by-one Error" typically occurs due to an incorrect loop termination condition or an incorrect index calculation, which leads to accessing the wrong data or a default value (like zero) instead of the intended data.
If the problem is specifically related to picking up a sample from a previous run that gathered more samples, you might also consider using the term "Stale Data" or "Residual Data." This can happen when the code does not properly reset or update certain variables or arrays before starting a new run, leading to unintended data being used in calculations.
I asked it to be a bit less verbose,
Got it! Here's a more concise version:
Issue: Off-by-one Error or Off-by-one Bug
Description: Code goes one step too far in the array, leading to zero values or picking up samples from previous runs.
Optional: Stale Data or Residual Data, if the problem involves using data from previous runs.
HTH and your English is better than my <any language>, maybe even better than my English.
133 is in excess of the absolute maximum number of operations in a 15 minute period, the delay I will eventually use. For this excercise I'm just using a button over a ten second period, so I'll have a hard time pressing the button that many times.
After I made the definition change, the script started to work, so if this wouldn't have caused it, I don't know what did.