Code: Select all
static byte previous[4];
static long time[4];
Moderators: adafruit_support_bill, adafruit
Code: Select all
static byte previous[4];
static long time[4];
But as far as I can see, in the function I'm looking at, withBy default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared.
Code: Select all
if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
Code: Select all
byte check_switches()
{
static byte previous[4]; // array of previous state of button input pins
static long time[4]; // array of a point in time, reset for & on each keys pressing
byte reading; // a momentary reading of the state of a button pin
byte pressed; // a return of the PIN NUMBER for a button that has been pressed
byte index; // a zero based counter for the for-loop, used to access the (zero based) arrays
pressed = 0; // if no button pressed, return zero
for (byte index = 0; index < 4; ++index) { // loop through the button pin array, from 0 to 3
reading = digitalRead(14 + index); // read each pin in succession as loop progresses
// THIS IS WHERE THE DEBOUNCE OCCURS:
// it looks to see if the CURRENT immediate state of an button input pin is LOW,
// AND the last(?) known recorded state of that pin was HIGH,
// AND this moment in time (millis()) minus the TIME of the LAST time the key wasn't pressed????
if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
{
// switch has been pressed
time[index] = millis(); // saving the moment in time this button was pressed
pressed = index + 1; // accounting for zero-based array index
break; // exiting from for loop
}
previous[index] = reading; // not sure why this is HERE, and not ALSO just before the <break>
}
// return switch number (1 - 4)
return (pressed);
}
The arrays in question (previous and time) are declared "static". So while they are defined in the scope of the function, they are actually initialized the same as file-scope arrays. (With most compilers anyway).By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized