Create int array with variable names

const byte LDRa = 1; //PB1
const byte LDRb = 2; //PB2
const byte LDR1 = 3; //PB3
const byte LDR2 = 4; //PB4
uint8_t pinNum[4];

void setup() {
  for (uint8_t i = 1; i < 5; i++) {
    pinMode(i, INPUT_PULLUP);
  }
  int pinNum[4] = (1, 2, 3, 4);
  int ledArray[4] = {readingLDRa, readingLDRb, readingLDR1, readingLDR2};
  for (uint8_t i = 1; i < 5; i++) {
    pinMode(ledArray[i], INPUT_PULLUP);
  }
}
void loop() {
  int readingLDRa = digitalRead(LDRa);
  int readingLDRb = digitalRead(LDRb);
  int readingLDR1 = digitalRead(LDR1);
  int readingLDR2 = digitalRead(LDR2);
}

How can the code in the loop be written into an array?

I tried

  for (uint8_t i = 1; i < 5; i++) {
    int ledArray[i] = digitalRead(pinNum[i]);
  }

..but the compiler complains about the for in the loop():
"array must be initialized with a brace-enclosed initializer"

Oops

1 Like

Braces, not parentheses

1 Like

use braces "{}" not parenthesis "()"
int pinNum[4] = {1, 2, 3, 4};

pinNum multiplee defined both globally and within setup(). the initialization within setup() doesn't affect the duplicate global array.

pinNum doesn't appear to be used anywhere

where are readingLDRa, readingLDRb, readingLDR1, readingLDR2 defined?

why are LEDs being configured as inputs instead of OUTPUT?

1 Like

consider example below using multiple buttons to control LEDs

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

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

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}
1 Like

Sorry, I had forgotten

const byte readingLDRa = 1, readingLDRb = 2, readingLDR1 = 3, readingLDR2 = 4;

It should be (corrected also now for use of braces):

const byte Relay = 0; //PB0
const byte LDRa = 1; //PB1
const byte LDRb = 2; //PB2
const byte LDR1 = 3; //PB3
const byte LDR2 = 4; //PB4
const byte readingLDRa = 1, readingLDRb = 2, readingLDR1 = 3, readingLDR2 = 4;
uint8_t pinNum[4];
void setup() {
  for (uint8_t i = 1; i < 5; i++) {
    pinMode(i, INPUT_PULLUP);
  }
  int pinNum[4] = {1, 2, 3, 4};
  int ledArray[4] = {readingLDRa, readingLDRb, readingLDR1, readingLDR2};
  for (uint8_t i = 1; i < 5; i++) {
    pinMode(ledArray[i], INPUT_PULLUP);
  }
}
void loop() {
  int readingLDRa = digitalRead(LDRa);
  int readingLDRb = digitalRead(LDRb);
  int readingLDR1 = digitalRead(LDR1);
  int readingLDR2 = digitalRead(LDR2);
}

Now arrays are initialized with braces, but I still get compiler error with

  for (uint8_t i = 1; i < 5; i++) {
    int ledArray[i] = digitalRead(pinNum[i]);
  }

Post your complete revised sketch and the full error message

1 Like

Still oops

1 Like

Hi UKHeliBob,
here the full sketch.
Error code output is below.

const byte Relay = 0; //PB0
const byte LDRa = 1; //PB1
const byte LDRb = 2; //PB2
const byte LDR1 = 3; //PB3
const byte LDR2 = 4; //PB4
const byte readingLDRa = 1, readingLDRb = 2, readingLDR1 = 3, readingLDR2 = 4;
byte stateLDRa = HIGH;  // state
byte stateLDRb = HIGH;
byte stateLDR1 = HIGH;
byte stateLDR2 = HIGH;
unsigned long debounceTimeLDRa; //H->L
unsigned long debounceTimeLDRb;
unsigned long debounceTimeLDR1;
unsigned long debounceTimeLDR2;
unsigned debounceDelay = 50; // H->L
unsigned onDelayLDR = 5000; // dealy time transistion L->H; from detect to undetect
byte relayState;
unsigned long onTimeLDRa;
unsigned long onTimeLDRb;
unsigned long onTimeLDR1;
unsigned long onTimeLDR2;
uint8_t pinNum[4];
// uint8_t ledArray[4];

void setup() {
  for (uint8_t i = 1; i < 5; i++) {
    pinMode(i, INPUT_PULLUP);
  }
  int pinNum[4] = {1, 2, 3, 4};
  int ledArray[4] = {readingLDRa, readingLDRb, readingLDR1, readingLDR2};
  for (uint8_t i = 1; i < 5; i++) {
    pinMode(ledArray[i], INPUT_PULLUP);
  }
  pinMode(Relay, OUTPUT); //setting the pin mode to Output
  digitalWrite(Relay, LOW); // relay not set, loop wired to leg a
}

void loop() {

  // read LDRa and debounce + verify if state change will be from H->L

  for (uint8_t i = 1; i < 5; i++) {
    int ledArray[i] = digitalRead(pinNum[i]);
  }
  int readingLDRa = digitalRead(LDRa);
  int readingLDRb = digitalRead(LDRb);
  int readingLDR1 = digitalRead(LDR1);
  int readingLDR2 = digitalRead(LDR2);

  if (readingLDRa == LOW) {
    debounceTimeLDRa = millis();
  }
  if ((millis() - debounceTimeLDRa) > debounceDelay || readingLDRa == LOW) {
    stateLDRa = readingLDRa; // set stateLDRa = L
    // outStateLDRa = readingLDRa; // set detection latch to L; train detected but not yet out of detection LDRa
  }

  // read LDRb and debounce + verify if state change will be from H->L
  if (readingLDRb == LOW) {
    debounceTimeLDRb = millis();
  }
  if ((millis() - debounceTimeLDRb) > debounceDelay || readingLDRb == LOW) {
    stateLDRb = readingLDRb; // ->L
    // outStateLDRb = readingLDRb; // set detection latch to L; train detected but not yet out of detection LDRb
  }

  // read LDR1 and debounce + verify if state change will be from H->L
  if (readingLDR1 == LOW) {
    debounceTimeLDR1 = millis();
  }
  if ((millis() - debounceTimeLDR1) > debounceDelay || readingLDR1 == LOW) {
    stateLDR1 = readingLDR1;
    // outStateLDR1 = readingLDR1; // set detection latch to L; train detected but not yet out of detection LDR1
  }

  // read LDR2 and debounce + verify if state change will be from H->L
  if (readingLDR2 == LOW) {
    debounceTimeLDR2 = millis();
  }
  if ((millis() - debounceTimeLDR2) > debounceDelay || readingLDR2 == LOW) {
    stateLDR2 = readingLDR2;
    // outStateLDR1 = readingLDR1; // set detection latch to L; train detected but not yet out of detection LDR1
  }

  //Delayed LDRa state change L->H, back to no detection
  if (stateLDRa == LOW || readingLDRa == HIGH) {
    // only execute when LDRa was LOW and changes state back to H
    onTimeLDRa = millis();
    if (millis() - onTimeLDRa > onDelayLDR || readingLDRa == HIGH) { // delay exectuted after L->H
      stateLDRa = readingLDRa; // reset stateLDRa H
    }
  }

  //Delayed LDRb state change L->H, back to no detection
  if (stateLDRb == LOW || readingLDRb == HIGH) {
    // only execute when LDRb changes state back to H
    onTimeLDRb = millis();

    if (millis() - onTimeLDRb > onDelayLDR || readingLDRb == HIGH) {
      stateLDRb = readingLDRb; // reset stateLDRa
    }
  }

  //Delayed LDR1 state change L->H, back to no detection
  if (stateLDR1 == LOW || readingLDR1 == HIGH) {
    // only execute when LDRb changes state back to H
    onTimeLDR1 = millis();

    if (millis() - onTimeLDR1 > onDelayLDR || readingLDR1 == HIGH) {
      stateLDR1 = readingLDR1; // reset stateLDRa
    }
  }

  //Delayed LDR2 state change L->H, back to no detection
  if (stateLDR2 == LOW || readingLDR2 == HIGH) {
    // only execute when LDRb changes state back to H
    onTimeLDR2 = millis();

    if (millis() - onTimeLDR2 > onDelayLDR || readingLDR2 == HIGH) {
      stateLDR2 = readingLDR2; // reset stateLDRa
    }
  }

  //conditions for relay output

  // 1. leg a: LDRa detect, LDR1 no detect
  if (stateLDRa == LOW && stateLDR1 == HIGH)  //train above LDRa but not inside loop yet
  {
    digitalWrite(Relay, LOW);
  }

  // 2. leg a: LDRa detect, LDR1 also detect
  if (stateLDRa == LOW && stateLDR1 == LOW)  //train above LDRa but not inside loop yet
  {
    digitalWrite(Relay, LOW);
  }

  // 3. leg a: LDRa no detect, LDR1 detect (fully inside loop)
  if (stateLDRa == HIGH && stateLDR1 == LOW)  //train above LDRa but not inside loop yet
  {
    digitalWrite(Relay, LOW);
  }

  // 4. leg b: LDRb detect, LDR2 no detect
  if (stateLDRa == LOW && stateLDR1 == HIGH)  //train above LDRa but not inside loop yet
  {
    digitalWrite(Relay, HIGH);
  }

  // 5. leg b: LDRb detect, LDR2 also detect
  if (stateLDRa == LOW && stateLDR1 == LOW)  //train above LDRa but not inside loop yet
  {
    digitalWrite(Relay, HIGH);
  }

  // 6. leg b: LDRb no detect, LDR2 detect (fully inside loop)
  if (stateLDRa == HIGH && stateLDR1 == LOW)  //train above LDRa but not inside loop yet
  {
    digitalWrite(Relay, HIGH);
  }
}

Error output:

Arduino: 1.8.13 (Windows 10), Board:"ATtiny85/25/45 (No bootloader), Enabled, CPU (CPU frequency), ATtiny85, 8 MHz (internal), EEPROM retained, B.O.D. Disabled (saves power), Enabled"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Erik Arckens\AppData\Local\Arduino15\packages -hardware C:\Users\Erik Arckens\Google Drive\documents\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Erik Arckens\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Erik Arckens\Google Drive\documents\Arduino\libraries -fqbn=ATTinyCore:avr:attinyx5:LTO=enable,TimerClockSource=default,chip=85,clock=8internal,eesave=aenable,bod=disable,millis=enabled -ide-version=10813 -build-path C:\Users\ERIKAR~1\AppData\Local\Temp\arduino_build_448917 -warnings=default -build-cache C:\Users\ERIKAR~1\AppData\Local\Temp\arduino_cache_977379 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avrdude.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino18 -prefs=runtime.tools.avrdude-6.3.0-arduino18.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino18 -prefs=runtime.tools.micronucleus.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\ATTinyCore\tools\micronucleus\2.5-azd1b -prefs=runtime.tools.micronucleus-2.5-azd1b.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\ATTinyCore\tools\micronucleus\2.5-azd1b -verbose C:\Users\Erik Arckens\Google Drive\documents\Arduino\Digispark_modeltrain_reversing_loop_v5\Digispark_modeltrain_reversing_loop_v5.ino

C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Erik Arckens\AppData\Local\Arduino15\packages -hardware C:\Users\Erik Arckens\Google Drive\documents\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Erik Arckens\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Erik Arckens\Google Drive\documents\Arduino\libraries -fqbn=ATTinyCore:avr:attinyx5:LTO=enable,TimerClockSource=default,chip=85,clock=8internal,eesave=aenable,bod=disable,millis=enabled -ide-version=10813 -build-path C:\Users\ERIKAR~1\AppData\Local\Temp\arduino_build_448917 -warnings=default -build-cache C:\Users\ERIKAR~1\AppData\Local\Temp\arduino_cache_977379 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avrdude.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino18 -prefs=runtime.tools.avrdude-6.3.0-arduino18.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino18 -prefs=runtime.tools.micronucleus.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\ATTinyCore\tools\micronucleus\2.5-azd1b -prefs=runtime.tools.micronucleus-2.5-azd1b.path=C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\ATTinyCore\tools\micronucleus\2.5-azd1b -verbose C:\Users\Erik Arckens\Google Drive\documents\Arduino\Digispark_modeltrain_reversing_loop_v5\Digispark_modeltrain_reversing_loop_v5.ino

Using board 'attinyx5' from platform in folder: C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2

Using core 'tiny' from platform in folder: C:\Users\Erik Arckens\AppData\Local\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2

Detecting libraries used...

"C:\\Users\\Erik Arckens\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=attiny85 -DF_CPU=8000000L -DCLOCK_SOURCE=0 -DARDUINO=10813 -DARDUINO_AVR_ATTINYX5 -DARDUINO_ARCH_AVR -DNEOPIXELPORT=PORTB "-IC:\\Users\\Erik Arckens\\AppData\\Local\\Arduino15\\packages\\ATTinyCore\\hardware\\avr\\1.5.2\\cores\\tiny" "-IC:\\Users\\Erik Arckens\\AppData\\Local\\Arduino15\\packages\\ATTinyCore\\hardware\\avr\\1.5.2\\variants\\tinyX5" "C:\\Users\\ERIKAR~1\\AppData\\Local\\Temp\\arduino_build_448917\\sketch\\Digispark_modeltrain_reversing_loop_v5.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE

Generating function prototypes...

"C:\\Users\\Erik Arckens\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto -w -x c++ -E -CC -mmcu=attiny85 -DF_CPU=8000000L -DCLOCK_SOURCE=0 -DARDUINO=10813 -DARDUINO_AVR_ATTINYX5 -DARDUINO_ARCH_AVR -DNEOPIXELPORT=PORTB "-IC:\\Users\\Erik Arckens\\AppData\\Local\\Arduino15\\packages\\ATTinyCore\\hardware\\avr\\1.5.2\\cores\\tiny" "-IC:\\Users\\Erik Arckens\\AppData\\Local\\Arduino15\\packages\\ATTinyCore\\hardware\\avr\\1.5.2\\variants\\tinyX5" "C:\\Users\\ERIKAR~1\\AppData\\Local\\Temp\\arduino_build_448917\\sketch\\Digispark_modeltrain_reversing_loop_v5.ino.cpp" -o "C:\\Users\\ERIKAR~1\\AppData\\Local\\Temp\\arduino_build_448917\\preproc\\ctags_target_for_gcc_minus_e.cpp" -DARDUINO_LIB_DISCOVERY_PHASE

"C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\ERIKAR~1\\AppData\\Local\\Temp\\arduino_build_448917\\preproc\\ctags_target_for_gcc_minus_e.cpp"

Bezig met het compileren van de schets...

"C:\\Users\\Erik Arckens\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=attiny85 -DF_CPU=8000000L -DCLOCK_SOURCE=0 -DARDUINO=10813 -DARDUINO_AVR_ATTINYX5 -DARDUINO_ARCH_AVR -DNEOPIXELPORT=PORTB "-IC:\\Users\\Erik Arckens\\AppData\\Local\\Arduino15\\packages\\ATTinyCore\\hardware\\avr\\1.5.2\\cores\\tiny" "-IC:\\Users\\Erik Arckens\\AppData\\Local\\Arduino15\\packages\\ATTinyCore\\hardware\\avr\\1.5.2\\variants\\tinyX5" "C:\\Users\\ERIKAR~1\\AppData\\Local\\Temp\\arduino_build_448917\\sketch\\Digispark_modeltrain_reversing_loop_v5.ino.cpp" -o "C:\\Users\\ERIKAR~1\\AppData\\Local\\Temp\\arduino_build_448917\\sketch\\Digispark_modeltrain_reversing_loop_v5.ino.cpp.o"

C:\Users\Erik Arckens\Google Drive\documents\Arduino\Digispark_modeltrain_reversing_loop_v5\Digispark_modeltrain_reversing_loop_v5.ino: In function 'void loop()':

Digispark_modeltrain_reversing_loop_v5:70:34: error: array must be initialized with a brace-enclosed initializer

     int ledArray[i] = digitalRead(pinNum[i]);

                       ~~~~~~~~~~~^~~~~~~~~~~

exit status 1

array must be initialized with a brace-enclosed initializer

@anon73444976 is really trying to help you. :slight_smile:

error: array must be initialized with a brace-enclosed initializer

     int ledArray[i] = digitalRead(pinNum[i]);

Wonder why the compiler tells you that ledArray[i] is being initialized?

1 Like

Yes amazed, I know, AWOL is really giving hints by am I too dumb to see, really, I am like a hare staring into lights right now, I am very sorry

    int ledArray[i] = digitalRead(pinNum[i]);

I spy a bracket where there should be no bracket

1 Like

??

    int ledArray(i) = digitalRead(pinNum[i]);

??

..I am lost..??

..I am lost..??

Yes. Remove int before ledArray in the for loop and change back to brackets. And you also have re-declaration of pinNum and ledArray is not in the scope of the loop and also over-indexed in both for loops. Nothing else for now. :slight_smile:

1 Like

OMG!!! Right, red to the cheeks.. :slight_smile:

.."overindexed": right, I got that too, thanks a million for all of you!!

Your setup routine is very confusing:

void setup() {
  for (uint8_t i = 1; i < 5; i++) {
    pinMode(i, INPUT_PULLUP);
  }
  int pinNum[4] = {1, 2, 3, 4};
  int ledArray[4] = {readingLDRa, readingLDRb, readingLDR1, readingLDR2};
  for (uint8_t i = 1; i < 5; i++) {
    pinMode(ledArray[i], INPUT_PULLUP);
  }
  pinMode(Relay, OUTPUT); //setting the pin mode to Output
  digitalWrite(Relay, LOW); // relay not set, loop wired to leg a
}

The first for loop sets pins 1 though 4 to INPUT_PULLUP mode. Then you define an array, pinNum, for those same pin numbers, but never use that array. Next you define another array, ledArray, set to the values of four global variables, that happen to have the values 1 though 4, and subsequently try to use this array to again set the pins to INPUT_PULLUP. but you are referencing array elements 1 through 4, when the array only consists of elements 0 through 3.

I think what you are trying to accomplish is something like this:

const byte Relay = 0; //PB0

const byte LDR[] = {1, 2, 3, 4};

void setup() {
  for (uint8_t i = 0; i < sizeof(LDR)/sizeof(LDR[0]); i++) { 
    // sizeof(LDR)/sizeof(LDR[0]) gives the number of elements in the LDR array
    pinMode(LDR[i], INPUT_PULLUP);
  }
  pinMode(Relay, OUTPUT); //setting the pin mode to Output
  digitalWrite(Relay, LOW); // relay not set, loop wired to leg a
}
1 Like

You are right, initialization and setup are a mess.
However: pinNum is used as a pointer in the first for in the loop().
And I would like to keep the names of the global variables in the array ledArray[ ]
But I have some work to do to clean up this pigsty and introduce arrays properly.
Thanks for your suggestions!

pinNum is created as a local variable within setup. It ceases to exist at the end of setup and cannot be used in loop.

Hello brice3010
And take some time and study the IPO model first.
Have a nice day and enjoy coding in C++.