Question regarding a SIMPLE code yet very confusing

So, I need help with my code that looks really simple but it certainly confuses me because it keeps saying

272:0,
88,
28,
1:
 In function 'void loop()':
31:7: error: expected unqualified-id before numeric constant

This is my code:

int A = 2;
int B = 3;
int C = 4;
int D = 5;
int I = 10;
int J = 11;
int E = 6;
int F = 7;
int G = 8;
int H = 9;

void setup() {

  Serial.begin(9600);
  
  pinMode(A, INPUT_PULLUP);
  pinMode(B, INPUT_PULLUP);
  pinMode(C, INPUT_PULLUP);
  pinMode(D, INPUT_PULLUP);
  pinMode(I, INPUT_PULLUP);
  pinMode(J, INPUT_PULLUP);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(H, OUTPUT);

}

void loop() {
  
  int PB1 = digitalRead(A);
  
   Serial.println(PB1);
  digitalWrite(E, HIGH);
  }

My purpose as of now is to read the pushbutton's values but if you want to help me with my overall purpose of PB1 turning LED1 on, PB2 with LED2, PB3 with LED3, PB4 with LED4, PB5 with turning all on, and PB6 with turning all off, it would be greatly appreciated. Thanks! :slight_smile:

EDIT: BTW, A(PB1), B(PB2), C(PB3), D(PB4), I(PB5), and J(PB6) and E(LED1), F(LED2), G(LED3), and H(LED4)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

We need to see your code please.

Thanks.. Tom... :slight_smile:

It compiles OK for me.

Sketch uses 2,940 bytes (9%) of program storage space. Maximum is 32,256 bytes.
Global variables use 202 bytes (9%) of dynamic memory, leaving 1,846 bytes for local variables. Maximum is 2,048 bytes.

Win 7
Uno
IDE 1.5.6-r2

My purpose as of now is to read the pushbutton’s values but if you want to help me with my overall purpose of PB1 turning LED1 on, PB2 with LED2, PB3 with LED3, PB4 with LED4, PB5 with turning all on, and PB6 with turning all off, it would be greatly appreciated.

You need to read up on the if command.
Should the LEDs remain on when the associated button is released ?

UKHeliBob:
It compiles OK for me.

Sketch uses 2,940 bytes (9%) of program storage space. Maximum is 32,256 bytes.

Global variables use 202 bytes (9%) of dynamic memory, leaving 1,846 bytes for local variables. Maximum is 2,048 bytes.




Win 7 
Uno
IDE 1.5.6-r2
You need to read up on the if command.
Should the LEDs remain on when the associated button is released ?

Well, on my end it highlights

 int PB1 = digitalRead(A);

I have Windows 8.1 and whatever board I use it does not work, especially Arduino Uno. I am using Arduino 1.6.6.

And yes, the LEDs should remain on in PB1, PB2, PB3, and PB4.

Which board are you using ?

Try changing the name of the PB1 variable to xPB1 as an experiment.

I got the same results, since I changed all the PB1 variable into xPB1 like you said but its still the same code.

Which board are you using and which version of the IDE ?

Does the Blink example work ?

I suggest you change all the single character variable names to longer names. I suspect your compiler is getting confused with your 'A'

Meaningful variable names make a program much easier to develop, maintain and debug.

,,,R

UKHeliBob:
Which board are you using and which version of the IDE ?

Does the Blink example work ?

I am using circuits.io but I am also using Arduino 1.6.6 to verify the code.

Robin2:
I suggest you change all the single character variable names to longer names. I suspect your compiler is getting confused with your 'A'

Meaningful variable names make a program much easier to develop, maintain and debug.

,,,R

I changed my code to this:

int PB1 = 2;
int PB2 = 3;
int PB3 = 4;
int PB4 = 5;
int PB5 = 10;
int PB6 = 11;
int LED1 = 6;
int LED2 = 7;
int LED3 = 8;
int LED4 = 9;

void setup() {

  Serial.begin(9600);
  
  pinMode(PB1, INPUT_PULLUP);
  pinMode(PB2, INPUT_PULLUP);
  pinMode(PB3, INPUT_PULLUP);
  pinMode(PB4, INPUT_PULLUP);
  pinMode(PB5, INPUT_PULLUP);
  pinMode(PB6, INPUT_PULLUP);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

}

void loop() {
  
  int PB1Read = digitalRead(PB1);
  
   Serial.println(PB1Read);
  }

And now it says:

272:0
88,
28,
1:
1:5: error: expected unqualified-id before numeric constant
2:5: error: expected unqualified-id before numeric constant
3:5: error: expected unqualified-id before numeric constant

And the highlighted parts are:

int PB1 = 2;
int PB2 = 3;
int PB3 = 4;
int PB4 = 5;
int PB5 = 10;
int PB6 = 11;
int PB1Read = digitalRead(PB1);
}

And yes, it includes the "}" sign

Which Arduino board are you compiling for ?

PB1 … PB6 are macros defined as 1 … 6.
Use different names.

UKHeliBob:
It compiles OK for me.

IDE 1.5.6-r2

That’s a significant difference.

<avr/portpins.h> has changed in 1.5.7 (avr-libc 1.8.0).

1.5.6

#if defined(PB1)
#  define PORTB1 PB1
#endif

1.5.7

#if defined(PB1) && !defined(PORTB1)
#  define PORTB1 PB1
#elif defined(PORTB1) && !defined(PB1)
#  define PB1 PORTB1
#endif

Hi,
PB1, PB2 etc are reserved machine code variables.

Use pb1, pb2, pb3 etc..

PB1 would not complile
Win 7, UNO, IDE 1.8.0
but
pb1 will.

Tom... :slight_smile:

TomGeorge:
Hi,
PB1, PB2 etc are reserved machine code variables.

Use pb1, pb2, pb3 etc..

PB1 would not complile
Win 7, UNO, IDE 1.8.0
but
pb1 will.

Tom... :slight_smile:

See replies #4 and #5

SkullHunter:
I changed my code to this:

int PB1 = 2;

int PB2 = 3;

Well that was not very sensible (I am trying to be polite here) in view of the comment in Reply #5

How about pushButton1pin, pushButton2pin etc ?

...R

UKHeliBob:
Which Arduino board are you compiling for ?

I am compiling for an Arduino Uno in circuits.io and for a Gizduino Mini in Arduino 1.6.6.

oqibidipo:
PB1 ... PB6 are macros defined as 1 ... 6.
Use different names.

Ok, I will try.

TomGeorge:
Hi,
PB1, PB2 etc are reserved machine code variables.

Use pb1, pb2, pb3 etc..

PB1 would not complile
Win 7, UNO, IDE 1.8.0
but
pb1 will.

Tom... :slight_smile:

Please check my original code because I already tried letters and not PB1...6. But anyways I did try it and it was successful. Thanks for the help! But do you have a code for my overall problem which is the LEDs?

P.S.: Sorry for the late reply. Our internet went down for some reason.

EDIT: My only problem with the LEDs is the animation. I want it to stay blinking when I clicked one of the buttons but it just blinks when I press it. Does anyone know how to fix this problem?

Can you please restate your problem ?

Should the buttons toggle the state of the LEDs when pressed or should the LEDs only stay on while the button is pressed ?

SkullHunter:
Please check my original code because I already tried letters and not PB1...6.

You have been unfortunate in that your choice of what seemed like simple variable names, and then the alternatives that you chose, clashed with names that are already allocated for other purposes. Also, the C++ language is case sensitive and upper case variable name are more likely to have special purposes.

If you use longer meaningful names that start with a lower-case character you will be very unlikely to run into this problem.

...R

UKHeliBob:
Can you please restate your problem ?

Should the buttons toggle the state of the LEDs when pressed or should the LEDs only stay on while the button is pressed ?

I changed my code to this:

int pushButton1 = 2;
int pushButton2 = 3;
int pushButton3 = 4;
int pushButton4 = 5;
int pushButton5 = 10;
int pushButton6 = 11;
int pushButton7 = 12;
int LED1 = 6;
int LED2 = 7;
int LED3 = 8;
int LED4 = 9;

void setup() {

  Serial.begin(9600);
  
  pinMode(pushButton1, INPUT_PULLUP);
  pinMode(pushButton2, INPUT_PULLUP);
  pinMode(pushButton3, INPUT_PULLUP);
  pinMode(pushButton4, INPUT_PULLUP);
  pinMode(pushButton5, INPUT_PULLUP);
  pinMode(pushButton6, INPUT_PULLUP);
  pinMode(pushButton7, INPUT_PULLUP);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

}

void loop() {
  
  int pushButton1Read = digitalRead(pushButton1);
  int pushButton2Read = digitalRead(pushButton2);
  int pushButton3Read = digitalRead(pushButton3);
  int pushButton4Read = digitalRead(pushButton4);
  int pushButton5Read = digitalRead(pushButton5);
  int pushButton6Read = digitalRead(pushButton6);
  int pushButton7Read = digitalRead(pushButton7);
  
  Serial.println(pushButton1Read);
  Serial.println(pushButton2Read);
  Serial.println(pushButton3Read);
  Serial.println(pushButton4Read);
  Serial.println(pushButton5Read);
  Serial.println(pushButton6Read);
  Serial.println(pushButton7Read);
  
  if(pushButton1Read == 0) {
    digitalWrite(LED1, HIGH);
    
  } else if(pushButton2Read == 0) {
    digitalWrite(LED2, HIGH);
  
  } else if(pushButton3Read == 0) {
    digitalWrite(LED3, HIGH);
    
  } else if(pushButton4Read == 0) {
    digitalWrite(LED4, HIGH);
    
  } else if(pushButton5Read == 0) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    
  } else if(pushButton6Read == 0) {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
  
  } else if(pushButton7Read == 0) {
    Animate();
 }
}
  
  void Animate() {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, LOW);
    delay(200);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
    delay(200);
  }

It work splendidly, except for a part which really troubles me:

 } else if(pushButton7Read == 0) {
    Animate();
 }
}
  
  void Animate() {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, LOW);
    delay(200);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
    delay(200);
  }

I mean, it does work, but what I needed here is that when I pressed the newly added pushButton7, it should keep blinking until i click pushButton6 or if I click pushButton7 but whats happening here is the only time that I click it will it blink for me so I need a solution for that.

Robin2:
You have been unfortunate in that your choice of what seemed like simple variable names, and then the alternatives that you chose, clashed with names that are already allocated for other purposes. Also, the C++ language is case sensitive and upper case variable name are more likely to have special purposes.

If you use longer meaningful names that start with a lower-case character you will be very unlikely to run into this problem.

...R

I'm sorry if at somewhat I offended you, but needless to say, I already fixed this and thanks for your help too. But please read my question for UKHeliBob indicated above. Thanks, and any help will be greatly appreciated! :slight_smile:

The way your code is written it updates the value of pushButton7Read every iteration of loop() and obviously that will take account of the fact that the button is no longer pushed.

To get the effect you want you need another variable - let's call it runAnimation - and that should be set to true when button 7 is pushed and set to false whenever any other button is pressed. In other words, it does not go "off" when button 7 is released. Then your code to call the animate() function would be

if (runAnimation == true) {
  animate();
}

...R

Robin2:
The way your code is written it updates the value of pushButton7Read every iteration of loop() and obviously that will take account of the fact that the button is no longer pushed.

To get the effect you want you need another variable - let's call it runAnimation - and that should be set to true when button 7 is pushed and set to false whenever any other button is pressed. In other words, it does not go "off" when button 7 is released. Then your code to call the animate() function would be

if (runAnimation == true) {

animate();
}




...R

Im sorry, but I dont know what variable to put in runAnimation. If you already mentioned it, can you please elaborate? I can't understand it thoroughly. Thanks!