light leds from an array

Hello,
I have the project to build a robot for my school. It will record button pushes then move according to this record.

Before usig motors, i’m trying to do this using leds.

Thanks from posts from this community, I’ve succeed in recording button pushes in an array.

Now I’m trying to light the leds according to this array.
I have 4 buttons with 4 leds and one button tu run the recording when it’s ready.

I’m stuck in lighting the leds when I push the play_button.

I hope someone can help me…

Here’s my script :

int arr[10];
boolean previous = LOW;
unsigned long time1 = 0;
int debounce = 200;
unsigned long time2 = 0;
int index_state = 0;

int play_button = 13; //define pin # for play button

int button[4] = {2,4,6,8}; //store button pins in an array
int leds[4] = {3,5,7,9}; //store led pins in an array

void setup()
{
  for(int n=0;n<10;n++) arr[n] = 0;//initialisition
 Serial.begin(9600);
}

void loop(){
 boolean mode = digitalRead(2) || digitalRead(4) || digitalRead(6) || digitalRead (8);
 while(mode == LOW) mode = digitalRead(2) || digitalRead(4) || digitalRead(6) || digitalRead (8);//wait here till one of the pushbutton goes high;
 //Serial.println("value");
 if( mode == HIGH && previous == LOW && millis() - time1 > debounce) {
 //find out which one is high and store that value in array;
 if(digitalRead(2)) arr[index_state] = 2;
 else if(digitalRead(4)) arr[index_state] = 4;
 else if(digitalRead(6)) arr[index_state] = 6;
 else if(digitalRead(8)) arr[index_state] = 8;
 else ;
 
 //print the value and the index;
 Serial.print(arr[index_state]);
 Serial.print(" Stored in index: ");
 Serial.println(index_state);
 index_state = (index_state + 1) % 10;
 //update value of time1 for button debouncing
 time1 =  millis();
}
 

  //update the value of previous button state
  previous = digitalRead(2) || digitalRead(4) || digitalRead(6) || digitalRead(8);


  }

Thank you for your help.

 if(digitalRead(2)) arr[index_state] = 2;
 else if(digitalRead(4)) arr[index_state] = 4;
 else if(digitalRead(6)) arr[index_state] = 6;
 else if(digitalRead(8)) arr[index_state] = 8;

Instead of storing the pin number of the active button you need to store its index number in the button array. Get that by using a for loop to read each of the button pins in turn (they are in an array) and if you find an active one save the for loop variable value. It is an index to the button array but also to the leds array

Then you can iterate through arr to get the index number of the button that was active and use it as the index to the associated LED

Thank you very much for your answer.

It seems quite clear for you, but I'm not sure to understand.

Anyway I will try to!

Do your best and post any questions (and code) here

Thank you !
Can you give me an example, as I'm new to arduino and English-spoken native, I don't clearly understand your advice...

Also posted on Reddit and Stack Exchange.

Stop wasting people's time like that, if you're going to post the same question all over the internet, at least be considerate enough to provide links to the other posts.

Pieter

Start by reading the inputs in a for loop

Have this on me as a Christmas present

const byte buttonPins[4] = {2, 4, 6, 8}; //store button pins in an array

const byte NUMBER_OF_buttonPins = sizeof(buttonPins) / sizeof(buttonPins[0]);  //calculate the number of buttonPins

void setup()
{
  Serial.begin(115200);
  for (int b = 0; b < NUMBER_OF_buttonPins; b++)
  {
    pinMode(buttonPins[b], INPUT_PULLUP);
  }
}

void loop()
{
  for (int b = 0; b < NUMBER_OF_buttonPins; b++)
  {
    if (digitalRead(buttonPins[b]) == LOW) //test if button is pressed
    {
      Serial.print("button ");
      Serial.print(b);
      Serial.print(" on pin ");
      Serial.print(buttonPins[b]);
      Serial.println(" is pressed");
    }
  }
}

NOTES :
The code only reads inputs and prints a message. It does not save the data nor light the LEDs
The code assumes that the inputs are held HIGH by INPUT_PULLUP when not pressed
It would be better to detect when a button becomes pressed rather than when it is pressed
The number of buttons is calculated by the program
If a button is found to be pressed the program prints a message telling you so
Instead of printing, the value of b (the buttons array index) could be saved in another array
The values saved in the array can be read by another for loop and used to access the array of LED pins by using them as the index to the LED array

If English is not your native language then the question might be better asked in one of the other language sections. If you would like it moved then please click Report to Moderator and ask for it to be moved

Thank you !
Merry christmas to you !