Push Buttons memory - Array

Hi
Based in a question i had in ''Project Guidance section'' for what method i should use for push buttons memory ( Push Buttons memory ) all members there suggest me to follow Array method

specially one member dougp told me to follow this :

dougp:
Create an array of size 'x'. Initialize all the array elements with some number which will never naturally occur, say '99'.

Initialize an array index variable to point to the first array element.

When a button is pressed store its number (or any special number you like) in the array at the position dictated by the index variable then increment the index variable. When another button is pressed, store and increment again.

Some other code will be looking at the first array position. If it sees a '99' it does nothing. When it sees a valid number it does the process 'y' associated with that number. When process 'y' is finished the first array element is destroyed by moving all higher-indexed elements one step to the next lower element:

a[0] <-- a[1], a[1] <-- a[2], etc. Decrement the index variable.

Put an invalid value in the now-vacated last array position.

As i am newbie in all this and first time i am using array (watched some youtube tutorials ) and English is not my Native language i would appreciate any help to make it step by step

The goal is not to have a code ready... but to understand how it is made

for now i wrote this code that is far away from what the other friend suggesting

int calls[4] = {18, 28, 38, 48};
byte callbutton1 = 3;//push button in the position
byte callbutton2 = 4;//push button in the position
byte callbutton3 = 5;//push button in the position
byte callbutton4 = 6;//push button in the position

int i;

int callcar1 = 0; // variable for reading the pushbutton status of the positions
int callcar2 = 0; // variable for reading the pushbutton status of the positions
int callcar3 = 0; // variable for reading the pushbutton status of the positions
int callcar4 = 0; // variable for reading the pushbutton status of the positions

boolean callFlag1 = false;
boolean callFlag2 = false;
boolean callFlag3 = false;
boolean callFlag4 = false;

void setup() {

  Serial.begin(9600);

  pinMode(callbutton1, INPUT_PULLUP);
  pinMode(callbutton2, INPUT_PULLUP);
  pinMode(callbutton3, INPUT_PULLUP);
  pinMode(callbutton4, INPUT_PULLUP);

}

void loop() {

  // read the state of the pushbuttons value://
  callcar1 = digitalRead(callbutton1);
  callcar2 = digitalRead(callbutton2);
  callcar3 = digitalRead(callbutton3);
  callcar4 = digitalRead(callbutton4);

  if (callcar1 == LOW && callFlag1 == false) {
    calls[0] += 2;
    callFlag1 = true;
  }
  if (callcar2 == LOW && callFlag2 == false) {
    calls[1] += 2;
    callFlag2 = true;
  }
  if (callcar3 == LOW && callFlag3 == false) {
    calls[2] += 2;
    callFlag3 = true;
  }
  if (callcar4 == LOW && callFlag4 == false) {
    calls[3] += 2;
    callFlag4 = true;
  }


  for (i = 0; i < 4; i++) {
    Serial.println(calls[i]);
  }


  if ( calls[0] == 20 ) {
    test();
  }

  if ( calls[1] == 30 ) {
    test2();
  }

}


void test() {


  Serial.println("ok - 1");
  calls[0] -= 2;
  callFlag1 = false;
  delay(2000);
}

void test2() {


  Serial.println("ok - 2");
  calls[1] -= 2;
  callFlag2 = false;
  delay(2000);

}

i know that i have to avoid delays .. i place them just to be able to see the result in serial monitor

Thanks in advance

This code looks OK to me for a start.

What would you like the code to do? It looks like you want to take 1 of 4 actions if the corresponding switch is pressed.

What is not ok with the results you get?

The aim is the code can store button presses so when the first action is finished then code continue with the second action that corresponding to other button press

so when we have 4 buttons and someone press No1 button and after while the No3 the code can made action 1 that is for No1 button and then continues to action 3 that is for No3 button

i am new to arduino iam making 3x3 push button array and want to display which button was pressed and display its position on serial monitor....can someone help me out there.

Try something like this:

#define BUTTON_COUNT 9
#define DEBOUNCE 10 //millis
const byte BUTTON_PINS[BUTTON_COUNT] = { 3, 4, 5, 6, 7, 8, 9, 10, 11 };

unsigned long button_millis[BUTTON_COUNT] = {0};
byte button_state[BUTTON_COUNT];
unsigned long loop_millis;

bool button_pressed(byte index)
{
  //Debounce
  if (loop_millis - button_millis[index] < DEBOUNCE) return (button_state[index] == LOW);

  //Check state
  byte state = digitalRead(BUTTON_PINS[index]);
  if (state != button_state[index])
  {
    button_state[index] = state;
    button_millis[index] = loop_millis;
  }
  return (state == LOW);
}

void setup()
{
  Serial.begin(9600);
  for (byte i = 0; i < BUTTON_COUNT; i++)
  {
    pinMode(BUTTON_PINS[i], INPUT_PULLUP);
    button_state[i] = HIGH;
  }
}

void loop()
{
  loop_millis = millis();
  for (byte i = 0; i < BUTTON_COUNT; i++) if (button_pressed(i))
  {
    //row = i / 3
    //col = i % 3
    Serial.print(F("Button "));
    Serial.print(i);
    Serial.println(F(" pressed"));
  }
  delay(1000); //Bad boy!
}

jaskirath_singh Please make your own thread. In it post your code with an explanation of what it is supposed to do, and what you are having trouble with (for example you might expect it to do one thing but it does something else).

caslor:
i would appreciate any help to make it step by step
The goal is not to have a code ready... but to understand how it is made

An array is very easy to understand. Without an array one might declare similar entities as follows:

byte led1;
byte led2;
byte led3;

But, let's say a function needs to iterate over all of the same entities as follows

setLeds(byte x) {
  led1 = x;
  led2 = x;
  led3 = x;
}

As the group grows in size, so does the duplication of code. Now, if one were to declare similar entities in an array:byte led[3];To iterate over the group, the code becomes more and more efficient as the group gets larger.

setLeds(byte x) {
for (i = 0; i < 2; i++) led[i] = x;
}