Array question

Hi

I am trying to get use to arrays, i am trying to get a 2 buttons to correspond with an 2 leds on a button press. but its not working and one of the leds is staying can any one

help please?

#include <EEPROM.h>


int val;
int val2;
int addr = 0;
int distbutton = 3;
int storebutton = 4;
int distrelay = 5;
int editbutton = 6;
int comprelay = 7;
int compbut = 8;
int relayid;
int buttonState = 0;
int editstate = 0;
int storestate = 0;
boolean editmode;
byte butid;
const int rotarybut = 2;


int fxbuts [2] = { 3, 8 };
int fxleds [3] = {10, 11, 13};


void setup()
{
  Serial.begin(9600);
 
 
 

 
  //for (int i = 10; i<14; i++)pinMode(i, OUTPUT); 
  for(int i = 0; i<4; i++) pinMode(fxbuts[i], INPUT_PULLUP);
for (int i = 0; i<4; i++)pinMode(fxleds[3], OUTPUT);
for(int i = 0; i<4; i++) digitalWrite(fxleds[i], LOW);
}

void loop(){
  
// read the fxbuttons  
for(int h = 0; h<4; h++){
  buttonState = digitalRead(fxbuts[h]);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is low:
  if (buttonState == LOW) {
    // turn LED on:
   for(int i = 0; i<4; i++) digitalWrite(fxleds[i], HIGH);
//digitalWrite(ledpin3,HIGH);


   
  }
  else {
    // turn LED off:
   
    for(int i = 0; i<4; i++) digitalWrite(fxleds[i], LOW);
    //digitalWrite(ledpin3,LOW);
  }
}
}
for (int i = 0; i<4; i++)pinMode(fxleds[3], OUTPUT);

A three element array doesn’t have an index 3.

fxbuts has 2 elements but you try to read 4? fxleds has three elements but you try to access 4?

On a tablet at the moment but that's what I noticed.l

yep - you are going beyond the bounds of your array...

an Array with 3 elements will have elements at

 x[0], x[1] and x[2]
for (int i = 0; i<4; i++)for (int i = 0; i<4; i++)pinMode(fxleds[3], OUTPUT);

As others have said, thus strays outside the bounds of your array but in any case I expect that you meant

pinMode(fxleds[i], OUTPUT);

Great guys !!

thank you for all the responses.

Greg

is there a way arrays that you can call the leds individually with a button ie. when button dist button calls fxleds 1 (10) in the array. combutton callls fxleds 2 (11)

#include <EEPROM.h>


int val;
int val2;
int addr = 0;
int distbutton = 3;
int ts = 9;
int storebutton = 4;
int distrelay = 5;
int editbutton = 6;
int comprelay = 7;
int compbut = 8;
int relayid;
int buttonState = 0;
int editstate = 0;
int storestate = 0;
boolean editmode;
byte butid;
const int rotarybut = 2;


int fxbuts [3] = { 3, 8, 9 };
int fxleds [3] = {10, 11, 13};


void setup()
{
  Serial.begin(9600);
 
 
 

 
  //for (int i = 10; i<13; i++)pinMode(i, OUTPUT); 
  for(int i = 0; i<3; i++) pinMode(fxbuts[i], INPUT_PULLUP);
for (int i = 0; i<3; i++)pinMode(fxleds[i], OUTPUT);
for(int i = 0; i<3; i++) digitalWrite(fxleds[i], LOW);
}

void loop(){
  
// read the fxbuttons  
for(int h = 0; h<3; h++){
  buttonState = digitalRead(fxbuts[h]);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is low:
  if (buttonState == LOW) {
    // turn LED on:
   for(int i = 0; i<3; i++) digitalWrite(fxleds[i], HIGH);
//digitalWrite(ledpin3,HIGH);


   
  }
  else {
    // turn LED off:
   
    for(int i = 0; i<3; i++) digitalWrite(fxleds[i], LOW);
    //digitalWrite(ledpin3,LOW);
  }
}
}

You could have another array for the buttons value and do buttonState[h] = digitalRead(fxbuts[h]);

So you should define this at the beginning by replacing

int buttonState = 0;

With

int buttonState[3]; // Will be initialized later

Then read just the 3 buttons states

for(int h = 0; h<3; h++){
  buttonState[h] = digitalRead(fxbuts[h]);
}

Then in the code you do another loop to check buttonState[0] to represent fxleds[0], buttonState[1] to represent fxleds[1] etc

This gives you an option to perform some actions based on the status of multiple buttons being pressed, instead of having the info about only 1 button

Alternatively if you don’t need combination of buttons, keep the code simple as it is but instead of turning on and off all LEDs with

for(int i = 0; i<3; i++) digitalWrite(fxleds[i], HIGH);

Just turn on the one matching the current button

digitalWrite(fxleds[h], HIGH); // h for the for loop you are in

Brilliant!!!!

thanks for taking the time to explain that.

Greg