Strange behaviour - won't compile for an absurd reason...

Hello,

nothing too complicated here, just strange. I just started a new code and as I tried to compile it (to see if all my variables were declared right) I got this error message:

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:32:0,

                 from sketch\NewAnalyser.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/binary.h:31:12: error: expected unqualified-id before numeric constant

 #define B1 1

            ^

C:\Users\Juliak\Documents\Arduino\ArduinoSketches\Matrix\analyser_8X8\NewAnalyser\NewAnalyser.ino:33:7: note: in expansion of macro 'B1'

  byte B1 = Blue[0];

       ^

exit status 1
Error compiling.

With the following code:

 const byte AudioIn = 6;
 const byte Strobe = 5;
 const byte Reset = 6;
 const byte Clock = 11;
 const byte Data = 12;
 const byte Latch = 13;

 byte value[7]; // Audio signal magnitude for each frequency

 byte Red[8];
 byte Green[8];
 byte Blue[8];

 byte R1 = Red[0];
 byte R2 = Red[1];
 byte R3 = Red[2];
 byte R4 = Red[3];
 byte R5 = Red[4];
 byte R6 = Red[5];
 byte R7 = Red[6];
 byte R8 = Red[7];

 byte G1 = Green[0];
 byte G2 = Green[1];
 byte G3 = Green[2];
 byte G4 = Green[3];
 byte G5 = Green[4];
 byte G6 = Green[5];
 byte G7 = Green[6];
 byte G8 = Green[7];

 byte B1 = Blue[0];
 byte B2 = Blue[1];
 byte B3 = Blue[2];
 byte B4 = Blue[3];
 byte B5 = Blue[4];
 byte B6 = Blue[5];
 byte B7 = Blue[6];
 byte B8 = Blue[7];


void setup()
{
  pinMode(AudioIn, INPUT);
  pinMode(Strobe, OUTPUT);
  pinMode(Reset, OUTPUT);
  pinMode(Clock, OUTPUT);
  pinMode(Data, OUTPUT);
  pinMode(Latch, OUTPUT);

  delay(2000);

   
  //initialize audio chip

  digitalWrite(Reset, LOW);
  digitalWrite(Strobe, LOW);

  delay(100);

  digitalWrite(Reset, HIGH);
  delay(1);
  digitalWrite(Reset, LOW);
  delay(1000);             // MSGEQ7 ready for use
}

//////////////////////////////////////////////////////////

void loop() 
{

 
}

However, if I change "B1" for a different name, like B9, or if I comment it out, it compiles...

 byte B1 = Blue[0];    // this is the B1 I'm talking about
 byte B2 = Blue[1];
 byte B3 = Blue[2];
 byte B4 = Blue[3];

Any ideas on why is it acting like this? I wouldn't deny the possibility of an error on my side, but I can not find where could my code be incorrect.

Thanks :slight_smile:
Mike

I think if you use #define B1, then can't use B1 as a variable later. #define says it can't be changed.

CrossRoads:
I think if you use #define B1, then can’t use B1 as a variable later. #define says it can’t be changed.

Thank you for the comment, but I think the issue is caused by a different cause since I am not defining B1 anywhere in my code.

byte B00000001 = 1 right?

Or in other words,

B1 is defined as 1 in the arduino IDE/library…

Therefore, I can not use “B1” as a variable name.

For security, I changed all my variables with uppercase Bs to lowercase Bs.

Thanks again, I believe the topic is solved.
Mike

Yes, B1 is already defined in the arduino core. It's a stupid name for a variable anyway as it doesn't tell you anything. Single letter and letter-number variables are best to stay away from. Try something like blue1 which is not only more descriptive, but much less likely to be used by something in the core.

BTW: Anytime you find yourself putting sequential numbers on the end of variable names you have to ask yourself, "Should I be using an array?"

You have defined B1 as the number "1". You can not assign a value to a number. Look up l-value and r-value.as relates to C programming.

If B1 is already defined by the Arduino system, you have just re-defined it. That could cause a problem or not.

Delta_G:
Yes, B1 is already defined in the arduino core. It's a stupid name for a variable anyway as it doesn't tell you anything. Single letter and letter-number variables are best to stay away from. Try something like blue1 which is not only more descriptive, but much less likely to be used by something in the core.

BTW: Anytime you find yourself putting sequential numbers on the end of variable names you have to ask yourself, "Should I be using an array?"

1- thank you.
2- yes, I should've used an array; I did at first, but then changed it since I believe it is necessary to assign the specific array value to a variable in order to retrieve the value assigned to that specific array. Am I right?

In other words, is it necessary to write the code like this in order to get the value saved in the array?

byte someArray[5];  //example

a = someArray[0];
b = someArray[1];

etc...

thanks :slight_smile:

boolrules:
You have defined B1 as the number "1". You can not assign a value to a number. Look up l-value and r-value.as relates to C programming.

If B1 is already defined by the Arduino system, you have just re-defined it. That could cause a problem or not.

Thank you.

Mike

You could just use someArray[0] wherever you were planning to use a. There is no need to put it in a separate variable.