I have come to learn by experience about using arrays but instead of asking what I can do, I think it more productive to ask what I cannot do and ask for insight about why that is.
In two self-instruction exercises, I sought to use arrays to make my code look tidier and more efficient only to be error-ed by the IDE.
One situation pulses three LEDs, all at different delay times an stored in a 1x3 integer array as a global variable. Using an interrupt, I intended to overwrite the rates using the following:
void chngFlashRate(){
int timeInterval[] = {400,600,800};
}
The upload executed and ran but upon executing the interrupt, nothing happened. I verified this by printing the output of one of the elements and watching it unchanged. To check, I replaced the code so the interrupt only reassigned a single integer variable and it indeed worked.
What occurred/didn’t occur and why?
In the second use case, I wrote a program cycling LED states and initialized them, as global variables, in a 1x3 array
int ledState_0[3] = {LOW,LOW,LOW};
To write the logic for situational switching, I wrote an “if” conditional statement (and rewrote and rewrote), comparing the different variable states in the array and writing in resultant states.
if (ledState[] == {LOW, LOW, LOW}){
ledState_0[] = {LOW, HIGH, LOW};
}
I was unsure the exact syntax to use so I played around but in no case did it work. But only then did this approach with arrays work:
if (ledState_0[0] == LOW && ledState_0[1] == LOW && ledState_0[2] == LOW){ // ALTERNATE: if (pbState_1 == HIGH && pbState_0 == LOW){
ledState_0[0] = HIGH; ledState_0[1] = LOW; ledState_0[2] = LOW;
digitalWrite(ledPin[0],ledState_0[0]); digitalWrite(ledPin[1],ledState_0[1]); digitalWrite(ledPin[2],ledState_0[2]);
So clearly I have a knowledge gap in how arrays do and do not work and would appreciate a clarification of my understanding. Can this approach still be used?