[Resolved]Setting up an array's values... I don't understand my current results

Part One:
I am creating a program that is meant to read four button inputs and record them in an array so I can call upon it later. Right now my program is assigning values to the array but they don't seem to make any sense. Any insight on the current results and how to fix them would be appreciated. Please note I am fairly new to programming.

void greenLongKeyPress() {
  if (greenRunning == false){
    greenRunning = true;
  }
    Serial.println(greenRunning);
    int arrayTotal = 0;
    int storage[100];

if (greenRunning == true){
  for (int t = 0; t < 100;){
   
    if (digitalRead(blueInPin) == LOW && millis() - time >= debounce){
      Serial.println(arrayTotal);
      Serial.println(storage[arrayTotal]);
      storage[arrayTotal] = 1;
      arrayTotal++;
      t++;
      time = millis();
    }else if (digitalRead(redInPin) == LOW && millis() - time >= debounce){
      Serial.println(arrayTotal);
      Serial.println(storage[arrayTotal]);
      storage[arrayTotal] = 2;
      arrayTotal++;
      t++;
      time = millis();
    }else if (digitalRead(yellowInPin) == LOW && millis() - time >= debounce){
      Serial.println(arrayTotal);
      Serial.println(storage[arrayTotal]);
      storage[arrayTotal] = 3;
      arrayTotal++;

      t++;
      time = millis();
    }else if (digitalRead(greenInPin) == LOW && millis() - time >= debounce){
      Serial.println(arrayTotal);
      Serial.println(storage[arrayTotal]);
      storage[arrayTotal] = 4;
      arrayTotal++;
      t++;
      time = millis();
      }
    }
  }
}

Here is the serial print results.

green key press
green key release
1
0
-5169
1
-1562
2
-4658
3
25599
4
-3395
5
-1884
6
-2531
7
-7236
8
-13891
9
-17555
10
-8333
11
-27438
12
2219
13
32247
14
23391
15
29551
16
-1030
17
8101
18
-1354
19
-1057
20
-905
21
32715
22
-1055
23
22002
24
-591
25
-639
26
-577
27
15943
28
26239
29
-6921
30
-4449
31
-11570
32
-6601
33
15255
34
-21889
35
-20801
36
-16917
37
32670
38
-9246
39
-1028
40
-22685
41
15164
42
-16521
43
11894
44
-21643
45
-20486
46
32343
47
-2689
48
-25277
49
-24849
50
24052
51
-587
52
-4521
53
-9231
54
-1303
55
-20947
56
-16985
57
-8871
58
-14265
59
30211
60
-16128
61
19714
62
-18371
63
12296
64
256
65
0
66
9013
67
30211
68
0
69
2048
70
239
71
1
72
2215
73
2254
74
494
75
1
76
769
77
17
78
768
79
-239
80
1022
81
374
82
803
83
2313
84
265
85
13
86
2
87
8960
88
803
89
118
90
907
91
118
92
0
93
0
94
37
95
0
96
8450
97
29
98
16129
99
16358
green key press
green key release

When you declare a local variable like storage, there is no initialization of the memory so who knows what value is in the array. You print this unknown value and then assign a known value to that index.

If you make your 'storage' array a global (declare it outside the function), C++ guarantees it will be initialized to zero.

Also, please post your entire sketch. A snippet can not be compiled by anyone but you...

OK. The issue I had with the global array is that it set each array value to zero and I couldn't change it. Sorry about the code. I ran into an issue with the character limit. I attempted to attach the file if its useful.

Revised_Button_Switch_Program.ino (10.1 KB)

The issue I had with the global array is that it set each array value to zero and I couldn't change it.

Why not? You should be able to change any value in a global array, anywhere in your code. Unless you marked the global array const.

In the code that you attached the storage[] array is local to the greenLongKeyPress() function and will be initialised with random values each time the function is called.

Is that what you intend to happen ?

UKHeliBob:
In the code that you attached the storage[] array is local to the greenLongKeyPress() function and will be initialised with random values each time the function is called.

Is that what you intend to happen ?

Not truly random values, just whats in stack memory at the time of the call, in other words there
is no initialization of local values unless you do it yourself - so do it yourself, or use a global array
if you want state to persist when the function returns.

I realize now the array needs to be called globally. The issue I don't understand is that when I call it globally and try to assign values to the array, they are all set to zero and I don't know how to fix it. My intention is to set the array with values 1-4. I feel like the code I have should set this properly but I feel like I'm missing something important.

green key press
green key release
1
0
0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
0
11
0
12
0
13
0
14
0
15
0
16
0
17
0
18
0
19
0
20
0

I attached a modified file with the global array if anyone finds it useful.

Revised_Button_Switch_Program.ino (10.1 KB)

Why make people download 10.1KB of unrelated, cluttered code? Post a simple MCVE that demonstrates your problem.

Sorry. New to the forum and I thought people wanted the full code given every other single post I have seen. The following code should be easier to use as it cuts out MOST of the programming for 3 of my buttons. As I said, the issue I am having is when I attempt to assign a value to my array, it remains equal to zero, not a value between one and four like I'm trying to get it to do.

int blueInPin = 2;
int redInPin = 3;
int greenInPin = 4;
int yellowInPin = 5;

int blueOutPin = 10;
int redOutPin = 11;
int greenOutPin = 12;
int yellowOutPin = 13;

bool greenRunning = false;

int storage[100];

int blueState = HIGH;

int grrenReading;

long delayValue = 500;
long time = 0;
long debounce = 150;

unsigned long grrenKeyPrevMillis = 0;

const unsigned long keySampleIntervalMs = 25;
byte grrenLongKeyPressCountMax = 30;    // 30 * 25 = 750 ms

byte grrenLongKeyPressCount = 0;

byte grrenPrevKeyState = HIGH;

const byte grrenKeyPin = 2;

void greenShortKeyPress() {
    Serial.println("grren short");
    Serial.println(time);
    Serial.println(millis());
if (greenRunning == false){
   if (greenState == HIGH && millis() - time > debounce){
      grrenState = LOW;
          Serial.println("grren low trigger");
     time = millis();
  }else{
      grrenState = HIGH;
          Serial.println("grren high trigger");
    time = millis();
    }
  }
}

void greenLongKeyPress() {
  if (greenRunning == false){
    greenRunning = true;
  }
    Serial.println(greenRunning);
    int arrayTotal = 0;

if (greenRunning == true){
  for (int t = 0; t < 100;){
   
    if (digitalRead(grrenInPin) == LOW && millis() - time >= debounce){
      Serial.println(arrayTotal);
      Serial.println(storage[arrayTotal]);
      storage[arrayTotal] = 1;
      arrayTotal++;
      t++;
      time = millis();
    }else if (digitalRead(redInPin) == LOW && millis() - time >= debounce){
      Serial.println(arrayTotal);
      Serial.println(storage[arrayTotal]);
      storage[arrayTotal] = 2;
      arrayTotal++;
      t++;
      time = millis();
    }else if (digitalRead(yellowInPin) == LOW && millis() - time >= debounce){
      Serial.println(arrayTotal);
      Serial.println(storage[arrayTotal]);
      storage[arrayTotal] = 3;
      arrayTotal++;
      t++;
      time = millis();
    }else if (digitalRead(greenInPin) == LOW && millis() - time >= debounce){
      Serial.println(arrayTotal);
      Serial.println(storage[arrayTotal]);
      storage[arrayTotal] = 4;
      arrayTotal++;
      t++;
      time = millis();
      }
    }
  }
}

void greenKeyPress() {
    Serial.println("green key press");
    greenLongKeyPressCount = 0;
}

void greenKeyRelease() {
    Serial.println("green key release");
    
    if (greenLongKeyPressCount >= greenLongKeyPressCountMax) {
        greenLongKeyPress();
    }else{
      if (greenRunning == false){
        greenShortKeyPress();
      }
    }
void setup(){
  pinMode(grrenInPin,INPUT_PULLUP);
  pinMode(redInPin,INPUT_PULLUP);
  pinMode(greenInPin,INPUT_PULLUP);
  pinMode(yellowInPin,INPUT_PULLUP);

  pinMode(blueOutPin,OUTPUT);
  pinMode(redOutPin,OUTPUT);
  pinMode(greenOutPin,OUTPUT);
  pinMode(yellowOutPin,OUTPUT);

  pinMode(allPin,OUTPUT);

  Serial.begin(115200);
  pinMode(blueKeyPin, INPUT_PULLUP);
  pinMode(redKeyPin, INPUT_PULLUP);
  pinMode(greenKeyPin, INPUT_PULLUP);
  pinMode(yellowKeyPin, INPUT_PULLUP);
}
void loop(){
  blueReading = digitalRead(blueInPin);
  redReading = digitalRead(redInPin);
  greenReading = digitalRead(greenInPin);
  yellowReading = digitalRead(yellowInPin);

    if (millis() - greenKeyPrevMillis >= keySampleIntervalMs) {
        grrenKeyPrevMillis = millis();
        
        byte grrenCurrKeyState = digitalRead(grrenKeyPin);
        
        if ((grrenPrevKeyState == HIGH) && (grrenCurrKeyState == LOW)) {
            grrenKeyPress();
        }
        else if ((grrenPrevKeyState == LOW) && (grrenCurrKeyState == HIGH)) {
            grrenKeyRelease();
        }
        else if (grrenCurrKeyState == LOW) {
            grrenLongKeyPressCount++;
        }
        
        grrenPrevKeyState = grrenCurrKeyState;
    }

Sorry if this still isn't what people are looking for in the code I provided.

Ennacirruh:
New to the forum and I thought people wanted the full code given every other single post I have seen.

No, as I mentioned, the easiest way for people to help you is to provide an MCVE. That's the SIMPLEST, COMPLETE code that compiles and demonstrates the problem. Anything else is just clutter.

The code you just posted will not compile, so it is not complete. And, it only contains one array plus a lot of extra variables and code not dealing with the array. All of that is clutter.

The really important thing about making an MCVE is that in cutting down your code to the bare minimum you may actually find the problem yourself.

I am not sure how I am supposed to cut down the code any further, It is everything that goes into calling the function. Sorry but I don't know what your looking for. The page that tells me how to do put the minimal code isn't helping me much...
I just wan't to know why storage[array total] = 3; sets the value for any position in my array to zero. This is my primary question. At this point if you need the programming, please read the previous posts as I'm not sure what I need to provide at this point. Basically, how do I set up an array because the reference and other array related posts I have read don't really show me what I might be doing wrong. I've put a great deal of my time into trying to solve this and have found nothing that fixes the program.

I just wan't to know why storage[array total] = 3; sets the value for any position in my array to zero.

A minimal program to test that would be:

int storage[100];

int arrayTotal = 0;

void setup()
{
   storage[arrayTotal] = 3;

   Serial.print("storage[");
   Serial.print(arrayTotal);
   Serial.print("] = ");
   Serial.println(storage[arrayTotal]);
}

void loop()
{
}

If that prints, as it should, "storage[0] = 3", then any claims you are making about incorrect assignments to arrays are because you are doing something wrong, somewhere.

Your code is incredibly complicated, because you have no comments, you have short and long presses happening, you have switches wired who knows how, and you have NOT shared any serial output.

It would be helpful to know what your program is supposed to do.

I am going to answer this in two parts.
The greenLongKeyPress function is meant to read button inputs from four buttons. The color based names represents the four different colored buttons. Based on which button is pushed, the function will assign a value the array. For example, on the fist button push, you push the blue button. Than storage[0] = 1; Than you push red leaving storage[1]=2; You push blue again leaving storage [2] = 1; Blue is 1. Red is 2. Green is 3. Yellow is 4.

The rest of the program is meant to detect if you only push the button or hold it down, hence green long vs. a greenShortKeyPress function I have. The short functions simply toggle a corresponding LED. This works. The if statements in the void loop detect if you press or hold and release the button. This works. The blueLong inverts which LEDs are on and off. This works. The red long is just remembers the LEds current states, turns off the LEDs, runs a LED sequence (I wrote this before I knew what arrays were), than sets the LEDs to how they were before you held the red button. This works. The yellow long does nothing at the moment. The button down just runs a serial print to notify you the button was pushed. The release function triggers when you release the button you press or hold and pulls from the if statements in to loop to see if the button was pressed or held. It then triggers either the short button press function or the long button press function accordingly. This works. The only thing in my program that doesn't work is my arrays in the green long function not properly being assigned the intended values. I don't know what I am doing wrong here based on the arduino reference and other array related posts I have seen.

I have shared two serial prints in earlier post replies, the one with random values is from when I define the array in the green long function. The second one showing all of the assigned zeros is from where I define the array as a global array.

The greenLongKeyPress function is meant to read button inputs from four buttons.

The first thing that I note is that this section of code

void greenLongKeyPress()
{
  if (greenRunning == false)
  {
    greenRunning = true;
  }
  Serial.println(greenRunning);

will always set set greenRunning to true and will always print 1 which tells you nothing.

For that reason this test

  if (greenRunning == true)
  {

is a waste of time

I also note that the blue input pin is not read in the for loop in the greenLongKeyPress() but green input pin is read twice. Is that deliberate ?

The geenRunning is not a test. It is meant to disable the buttons ability to toggle the LEDs while the greenLongKeyPress is running. And no, the repeat is not deliberate. I would like to note that the greenRunning toggle block does stop the LEDs while not blocking the codes ability to read there was a short press. I just don't get why when say arrayTotal = 0, storage[arrayTotal] =1 sets storage[arrayTotal] to 0. Same if I try to set it to other values like 2, 3, or 4. This is what I'm trying to fix.

I just don't get why when say arrayTotal = 0, storage[arrayTotal] =1 sets storage[arrayTotal] to 0

Prove that is what is happening by printing the value before and after you set it to 1 but print a text label before the value in each case so it is obvious where the program is in the code.

Print the index (i.e. arrayTotal) at the same time.

Thank You. This helped a lot. Sorry about he inconvenience. I am going to take it the function I used to have in my yellowLongKeyPress wasn't calling the values correctly and my serial print misplacement confused my as to where the problem was. One again sorry for the inconvenience. Have a good day.