Go Down

Topic: variable declaration problem (Read 369 times) previous topic - next topic

alexandros301

Hi,
I'm trying to send Arduino a list from Pure Data, in order to have several LEDs on (or off) from a multiplexer. I know this isn't the section for multiplexing, but my problem for now lies in the declaration of a variable, so I thought of posting here. My code is this (the actual problem occurs in the loop but I'm including the setup of the code as well for definition and variable declaration clarification; you can as well skip this)

Code: [Select]

#define CONTROL0 2
#define CONTROL1 3
#define CONTROL2 4
#define CONTROL3 5

const int muxLed = A0;
int i = 0;

void setup() {
  Serial.begin(9600);
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
  pinMode(muxLed, OUTPUT);
}


Here is where the problem occurs
Code: [Select]
void loop() {
  if (Serial.available() > 0) {
    i = Serial.read();
    if (i>16) {
      int j = i - 16; /* I don't really know how to distinguish data that I send
     from my computer, therefore I set different number ranges and correct with
    an offset in the arduino code */
      int myLeds[j];
    }
    if (i>=0 && i<16) {
      for (int k = 0; k < j; k++) { // here is where the problem occurs, even though I've already declared j earlier
        myLeds[k] == i;
        digitalWrite(CONTROL0, (myLeds[k]&15)>>3); /* here I want to read the array elements one by one, maybe another for loop is needed.. */
        digitalWrite(CONTROL1, (myLeds[k]&7)>>2);
        digitalWrite(CONTROL2, (myLeds[k]&3)>>1);
        digitalWrite(CONTROL3, (myLeds[k]&1));
        digitalWrite(muxLed, HIGH);
      } else {
        digitalWrite(muxLed, LOW);
      }
    }
  }
}


I get the following error

Code: [Select]
'j' was not declared in this scope

What I want to do is first send the length of my list to set it as an array length, and then set the elements of the array. Finally I want to read the array's elements one by one in a for loop.
Any help appreciated.

HazardsMind

Take the "int" out of this "int j = i - 16; " and make it a global variable at the top of the code.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PaulS

Code: [Select]
    if (i>16) {
      int j = i - 16;  // Useless comment deleted
      int myLeds[j];
    }

j and myLeds are local to the block. They go out of scope as soon as the block ends. Most likely NOT what you want to have happen.

Dynamically sized arrays created this way are really NOT a good idea.

alexandros301

HazardsMind, if I declare j globally, it will be changing the array size with each loop, right?
PaulS, how would you suggest to created a dynamically sized array?
Plus, how can I route incoming data of the same type (numbers) to different parts of the code (e.g. first number is the size of the array, following numbers are set as the array's elements)?

HazardsMind

Well, you can put this at the top "int myLeds[ ];" and make it have a set size say 20. So now it will be "int myLeds[20];"
Quote
it will be changing the array size with each loop, right?

Why would you want the array size to change with each loop? if 20 is not enough, make it 50 or more.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

UKHeliBob

Quote
how would you suggest to created a dynamically sized array?

Don't.  Declare the array globally at the maximum size that you need.

Quote
how can I route incoming data of the same type (numbers) to different parts of the code (e.g. first number is the size of the array, following numbers are set as the array's elements)?

This problem goes away if you follow the first suggestion.

You may want/need to initialise the previously declared array to known, dummy values before populating it with new values, but that is trivial to do.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up