Store and adress variables in an array

Hi, I'm building a 16 switch midi foot controller.
Using a Teensy.

my code so far looks like this:

int LO1 = HIGH; int LO1_1 = HIGH;
int LO2 = HIGH; int LO2_1 = HIGH;
int LO3 = HIGH; int LO3_1 = HIGH;
int LO4 = HIGH; int LO4_1 = HIGH;
int PB1 = HIGH; int PB1_1 = HIGH;
int PB2 = HIGH; int PB2_1 = HIGH;
int PB3 = HIGH; int PB3_1 = HIGH;
int PB4 = HIGH; int PB4_1 = HIGH;
int SO1 = HIGH; int SO1_1 = HIGH;
int SO2 = HIGH; int SO2_1 = HIGH;
int SO3 = HIGH; int SO3_1 = HIGH;
int SO4 = HIGH; int SO4_1 = HIGH;
int FX1 = HIGH; int FX1_1 = HIGH;
int FX2 = HIGH; int FX2_1 = HIGH;
int CLR = HIGH; int CLR_1 = HIGH;
int S16 = HIGH; int S16_1 = HIGH;

String schalter [] = {"LO1", "LO2", "LO3", "LO4", "PB1", "PB2", "PB3", "PB4", "SO1", "SO2", "SO3", "SO4", "FX1", "FX2", "CLR", "S16"};
int schalterA [] = {LO1, LO2, LO3, LO4, PB1, PB2, PB3, PB4, SO1, SO2, SO3, SO4, FX1, FX2, CLR, S16};
int schalterB [] = {LO1_1, LO2_1, LO3_1, LO4_1, PB1_1, PB2_1, PB3_1, PB4_1, SO1_1, SO2_1, SO3_1, SO4_1, FX1_1, FX2_1, CLR_1, S16_1};

bool ply = false;

void setup() {
  for(int i=5; i<21; i++){pinMode(i, INPUT_PULLUP);}
  Serial.begin(9600);
}

void loop() {

for(int i=0; i<16; i++){
    schalterA [i] = digitalRead(i+5);
     
    if (schalterA[i] == LOW && schalterB[i] == HIGH) {
      Serial.print(schalter[i]); Serial.println(" down");
      schalterB[i] = schalterA[i];
      }
      
    if (schalterA[i] == HIGH && schalterB[i] == LOW) {
      Serial.print(schalter[i]); Serial.println(" up");
      schalterB[i] = schalterA[i];
      }
    }
}

So I basically tried to declare a bunch of Variable to store my pushbutton states (connected to Pins 5 to 20).

that code works so far.

My Question is: why do I have to use

if (schalterA[i] == LOW && schalterB[i] == HIGH)

to address my variables? because

if (LO1 == LOW && LO1_1 == HIGH)

gives me no result?

thanks a lot in advance.
Thomas

String schalter [] = {"LO1", "LO2", "LO3", "LO4", "PB1", "PB2", "PB3", "PB4", "SO1", "SO2", "SO3", "SO4", "FX1", "FX2", "CLR", "S16"};
bool schalterA [16];

void setup()
{
  for (int i = 0; i < 16; i++)
  {
    pinMode(i + 5, INPUT_PULLUP);
    schalterA[i] = digitalRead(i + 5);
  }
  Serial.begin(9600);
}

void loop()
{
  for (int i = 0; i < 16; i++)
  {
    bool inputVal = digitalRead(i + 5);
    if (schalterA[i] != inputVal)
    {
      schalterA[i] = inputVal;
      Serial.print(schalter[i]);

      if (inputVal)
        Serial.println(" down");
      else
        Serial.println(" up");
    }
  }
}

If I understand your question correctly, the part of the code where you initialize a lot of ints and then create an array of values does not do what you think it does.
In fact int schalterA'[ ]' = { LO1,.. } will create an array of variables inintialized with the value HIGH.
You are not storing the adresses of variables with the code you are creating and you are not using the adresses but you are using the values to do some kind of alt-neu vergleich.
So i tried to modify your code a bit to do what i think it needs to do.

seems that all that's needed is to detect a change in the button position and report or

#undef MyHW
#ifdef MyHW
String schalter [] = {
    "LO1", "LO2", "LO3", "LO4",
};
# define N_PINS  4
# define Pin0    14

#else
String schalter [] = {
    "LO1", "LO2", "LO3", "LO4",
    "PB1", "PB2", "PB3", "PB4",
    "SO1", "SO2", "SO3", "SO4",
    "FX1", "FX2", "CLR", "S16"
};
# define N_PINS 16
# define Pin0   5
#endif


byte pinStates [N_PINS];

char s [80];

// -----------------------------------------------------------------------------
void setup () {
    for (int i = 0; i < N_PINS; i++) {
        pinMode (Pin0 + i, INPUT_PULLUP);
        pinStates [i] = digitalRead (Pin0 + i);
    }

    Serial.begin (9600);
}

// -----------------------------------------------------------------------------
void loop () {
    for (int i = 0; i < N_PINS; i++) {
        byte state = digitalRead (Pin0 + i);

        if (pinStates [i] != state)  {
            Serial.print (schalter[i]);

            if (LOW == state)
                Serial.println (" down");
            else
                Serial.println (" up");

            pinStates [i] = state;
            delay (10);         // debounce
        }
    }
}

because you are running it in a loop and [i] lets you access next variable on next iteration?

thanks everybody, I think I got my mistake and learned a bit more .
I'll be back with my update : )

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.