Code error - 'x' was not declared in this scope

I'm upgrading one of my projects that I did last year - A Christmas Light Spectrum Analyzer which used an MSGEQ7 chip. On that prototype, I had one potentiometer to set the threshold for all (7) bands. This year, I want to upgrade it to (7) potentiometers where each individual pot sets the threshold for its dedicated band.

I understand that there might be better methods than this, but I'm just tinkering with the idea and want to prove it out because I wasn't satisfied with one pot sets all.

I edited my old sketch and got it working, which was great. But being the amateur that I am, the code was very long and sloppy. So I felt the need to simplify the code and learn some new techniques... but I'm getting couple of errors.

Below is what I've written so far...

#include "LedControl.h"

#define msg7RESET 13
#define msg7Strobe 9
#define msg7DCout A0

#define maxDataIn 10
#define maxLoad 11
#define maxCLK 12

#define led1 2
#define led2 3
#define led3 4
#define led4 5
#define led5 6
#define led6 7
#define led7 8

int TRIM_POT_A[7] = {A1,A2,A3,A4,A5,A6,A7};
#define LED_LOW HIGH  //was LOW
#define LED_HIGH LOW  //was HIGH
#define CYCLE_DELAY 50 // set to 100 for mechanical relay, set to 50 for solid state relay


LedControl lc=LedControl(maxDataIn,maxCLK,maxLoad,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime=100;
byte ledPins[7]= {led1,led2,led3,led4,led5,led6,led7};
byte level[8]=   {128,192,224,240,248,252,254,255};
byte setlevel[8]={128,64,32,16,8,4,2,1};
unsigned int valueMSG[7];
unsigned int potValue_A[7] = {0};
byte potSet_A[7] = {0};

void setup() {
  Serial.begin(115200);
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  pinMode(msg7RESET, OUTPUT);
  pinMode(msg7Strobe, OUTPUT);
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  
  digitalWrite(msg7Strobe, HIGH);
  digitalWrite(msg7RESET, LOW);
  digitalWrite(led1, LED_LOW);
  digitalWrite(led2, LED_LOW);
  digitalWrite(led3, LED_LOW);
  digitalWrite(led4, LED_LOW);
  digitalWrite(led5, LED_LOW);
  digitalWrite(led6, LED_LOW);
  digitalWrite(led7, LED_LOW);


  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,1);
  /* and clear the display */
  lc.clearDisplay(0); 
}

void loop()
{ 
  digitalWrite(msg7Strobe, HIGH);  
  digitalWrite(msg7RESET, HIGH);
  //delayMicroseconds(0.1); //Reset Pluse Width 100nS Min
  for (int x = 0 ; x < 7 ; x++)
  potValue_A[x] = analogRead(TRIM_POT_A[x]);
  potSet_A[x] = map(potValue_A[x], 0, 1024, 0, 8);  

  lc.setRow(0,[x]+1,setlevel[potSet_A[x]);  

  digitalWrite(msg7RESET, LOW);
  delayMicroseconds(72); // Reset to Stobe Delay 72uS min
  

    {
      digitalWrite(msg7Strobe, LOW);      
      delayMicroseconds(36); //Output settling time 36us Min

      int spectrumRead = analogRead(msg7DCout);
      
      valueMSG[x] = map(spectrumRead, 0, 1024, 0, 8);

      lc.setRow(0,x,level[valueMSG[x]]);
      digitalWrite(msg7Strobe, HIGH);
      delayMicroseconds(72); //Strobe to Strobe 72uS Min 
      
      //Set Lights 
      if(valueMSG[x] >= potSet_A[x])
      {
        digitalWrite(ledPins[x], LED_HIGH);
      }
      else
      {
        digitalWrite(ledPins[x], LED_LOW);
      }

      Serial.print("x = ");
      Serial.println(x);
      Serial.print("spectrumRead = ");
      Serial.println(spectrumRead);
      Serial.print("Potentiometer Value = ");
      Serial.println(potValue_A[x]);
      Serial.print("valueMSG[x] = ");
      Serial.println(valueMSG[x]);
      Serial.print("Potentiometer Setting = ");
      Serial.println(potSet_A[x]]); 

      Serial.print('\n');
    }
    delay(CYCLE_DELAY); //Allow them to be on or off for a bit of time 
}

And these are the errors that I'm getting...

C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino: In function 'void loop()':
C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino:78:12: error: 'x' was not declared in this scope
   potSet_A[x] = map(potValue_A[x], 0, 1024, 0, 8);
            ^
C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino: In lambda function:
C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino:80:18: error: expected '{' before '+' token
   lc.setRow(0,[x]+1,setlevel[potSet_A[x]);
                  ^
C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino: In function 'void loop()':
C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino:80:18: error: no match for 'operator+' (operand types are 'loop()::<lambda()>' and 'int')
   lc.setRow(0,[x]+1,setlevel[potSet_A[x]);
C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino:80:41: error: expected ']' before ')' token
   lc.setRow(0,[x]+1,setlevel[potSet_A[x]);
                                         ^

exit status 1

Compilation error: 'x' was not declared in this scope

I'm a bit confused, because I thought I already declared 'x' in the scope.
The second error, I'm attempting to recall 'x' and add "1" but I'm I'm getting hung up here too.

What did I do wrong? Can you set my code straight?

The code that you want to be executed as part of the for loop must be enclosed in curly brackets. Without curly brackets, only 1 line after the for is part of the for (the first ; (semi-colon) ends the for). All the rest are not part of the for and so x (the for indexer) is out of scope (does not exist) for them.

1 Like

Use braces.

1 Like

Thanks guys. That worked.

Last piece of the puzzle... I can't seem to resolve this issue. I have my readings from valueMSG[x] posting to an 8x8 LED Matrix (max7219). I want to post my potentiometer values on each row coinciding with the valueMSG[x] . I attempted this on line 87.

lc.setRow(0,[x],setlevel[potSet_A[x]]);

but get the following errors:

C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino: In lambda function:
C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino:87:22: error: expected '{' before ',' token
       lc.setRow(0,[x],setlevel[potSet_A[x]]);
                      ^
C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino: In function 'void loop()':
C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino:87:44: error: no matching function for call to 'LedControl::setRow(int, loop()::<lambda()>, byte&)'
       lc.setRow(0,[x],setlevel[potSet_A[x]]);
                                            ^
In file included from C:\Users\jalbe\OneDrive\Documents\Arduino\X-Mas Lights\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13\Update Musical_Lights_Controller_V13.ino:1:0:
C:\Users\jalbe\OneDrive\Documents\Arduino\libraries\LedControl\src/LedControl.h:150:14: note: candidate: void LedControl::setRow(int, int, byte)
         void setRow(int addr, int row, byte value);
              ^~~~~~
C:\Users\jalbe\OneDrive\Documents\Arduino\libraries\LedControl\src/LedControl.h:150:14: note:   no known conversion for argument 2 from 'loop()::<lambda()>' to 'int'

exit status 1

Compilation error: expected '{' before ',' token

If I set lc.setRow(0,[x],setlevel[potSet_A[x]]); to lc.setRow(0,7,setlevel[potSet_A[x]]); it works, but this isn't the result I want.

Any idea how to resolve this?

Latest Code Below...

#include "LedControl.h"

#define msg7RESET 13
#define msg7Strobe 9
#define msg7DCout A7

#define maxDataIn 10
#define maxLoad 11
#define maxCLK 12

#define led1 2
#define led2 3
#define led3 4
#define led4 5
#define led5 6
#define led6 7
#define led7 8

int TRIM_POT_A[7] = {A0,A1,A2,A3,A4,A5,A6};
#define LED_LOW HIGH  //was LOW
#define LED_HIGH LOW  //was HIGH
#define CYCLE_DELAY 50 // set to 100 for mechanical relay, set to 50 for solid state relay


LedControl lc=LedControl(maxDataIn,maxCLK,maxLoad,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime=100;
byte ledPins[7]= {led1,led2,led3,led4,led5,led6,led7};
byte level[8]=   {128,192,224,240,248,252,254,255};
byte setlevel[8]={128,64,32,16,8,4,2,1};
unsigned int valueMSG[7];
unsigned int potValue_A[7] = {0};
byte potSet_A[7] = {0};

void setup() {
  Serial.begin(115200);
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  pinMode(msg7RESET, OUTPUT);
  pinMode(msg7Strobe, OUTPUT);
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  
  digitalWrite(msg7Strobe, HIGH);
  digitalWrite(msg7RESET, LOW);
  digitalWrite(led1, LED_LOW);
  digitalWrite(led2, LED_LOW);
  digitalWrite(led3, LED_LOW);
  digitalWrite(led4, LED_LOW);
  digitalWrite(led5, LED_LOW);
  digitalWrite(led6, LED_LOW);
  digitalWrite(led7, LED_LOW);


  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,1);
  /* and clear the display */
  lc.clearDisplay(0); 
}

void loop()
{ 
  digitalWrite(msg7Strobe, HIGH);  
  digitalWrite(msg7RESET, HIGH);
  //delayMicroseconds(0.1); //Reset Pluse Width 100nS Min


  digitalWrite(msg7RESET, LOW);
  delayMicroseconds(72); // Reset to Stobe Delay 72uS min
  
    for (int x = 0 ; x < 7 ; x++)
    {
      
      potValue_A[x] = analogRead(TRIM_POT_A[x]);
      potSet_A[x] = map(potValue_A[x], 0, 1024, 0, 8);  

      lc.setRow(0,[x],setlevel[potSet_A[x]]);
      digitalWrite(msg7Strobe, LOW);
      delayMicroseconds(36); //Output settling time 36us Min

      int spectrumRead = analogRead(msg7DCout);
      
      valueMSG[x] = map(spectrumRead, 0, 1024, 0, 8);

      lc.setRow(0,x,level[valueMSG[x]]);
      digitalWrite(msg7Strobe, HIGH);
      delayMicroseconds(72); //Strobe to Strobe 72uS Min 
      
      //Set Lights 
      if(valueMSG[x] >= potSet_A[x])
      {
        digitalWrite(ledPins[x], LED_HIGH);
      }
      else
      {
        digitalWrite(ledPins[x], LED_LOW);
      }

      Serial.print("x = ");
      Serial.println(x);
      Serial.print("spectrumRead = ");
      Serial.println(spectrumRead);
      Serial.print("Potentiometer Value = ");
      Serial.println(potValue_A[x]);
      Serial.print("valueMSG[x] = ");
      Serial.println(valueMSG[x]);
      Serial.print("Potentiometer Setting = ");
      Serial.println(potSet_A[x]); 

      Serial.print('\n');
    }
    delay(CYCLE_DELAY); //Allow them to be on or off for a bit of time 
    
}

Why is x in []?

[x] is not the for loop indexer, x is the for loop indexer.

lc.setRow(0, x, setlevel[potSet_A[x]]); // removed the [ ]

It compiles, now, for me.

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