Turning my List into arrays

Hello everyone …I have the following in my code and it was suggested that I use arrays instead …

int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int sensorValue = 0;
int outputValue = 0;
int sensorValue1 = 0;
int outputValue1 = 0;
int sensorValue2 = 0;
int outputValue2 = 0;
int sensorValue3 = 0;
int outputValue3 = 0;
int sensorValue4 = 0;
int outputValue4 = 0;
int sensorValue5 = 0;
int outputValue5 = 0;
int sensorValue6 = 0;
int outputValue6 = 0;
int sensorValue7 = 0;
int outputValue7 = 0;
int sensorValue8 = 0;
int outputValue8 = 0;
int sensorValue9 = 0;
int outputValue9 = 0;
int sensorValue10 = 0;
int outputValue10 = 0;

Any helpful suggestions ?? Thanks in advance

Something like this:

int buttonState[7];
int sensorValue[11];
int outputValue[11];
3 Likes

Wow that shortens it …thanks I’ll give it a try

I’ve got another tip for this

const int AlarmPinIn = D1;//LowPressure
const int AlarmPinIn1 = D2;//High Pressure
const int AlarmPinIn2 = D3;//Flame Fail
const int AlarmPinIn3 = D4;//CO2 Alarm

To keep things flexible and neat

const int AlarmPinIn[] = 
{ D1 //LowPressure
, D2 //High Pressure
, D3 //Flame Fail
, D4 //CO2 Alarm
}
const int countAlarmPinsIn = sizeof(AlarmPinIn) / sizeof(AlarmPinIn[0]);

void setup() {
  for(int i = 0; i < countAlarmPinsIn; i++) 
    pinMode(AlarmPinIn[i], INPUT_PULLUP);
}

This way you can rearrange hardware pins and add pins with virtually no need to alter the rest of the code but adding a new line of , D5 (or any other pin name) in the array.

If you keep using loops and arrays that way you can shorten your code and cut on code maintenance considerably.

3 Likes

Thanks for the help