Go Down

Topic: "expected unqualified-id before numeric constant" (Read 11365 times) previous topic - next topic

conor1148

Hey everyone, ive tried searching, but havent found anything helpful

basically i just got my arduino yesterday, and ive done some simple LED pattern stuff. i tried to do the project in the "getting started with arduino book" through MAKE that was where you press a button, and the LED lights up.

i typed up the code myself, and there are no other problems with it. i had typed out the code so that if i  pressed the button, it did a certain pattern; but i removed that and made it just turn on like the project in the book (over complicating it was what i predicted my problem to
be)

still, it gives me a message that says "error- expected unqualified-id before numeric constant" and it highlights my line that is

Code: [Select]
#define BUTTON 4

i have no clue what to do, im just new to this, go easy..

heres my code. (the extra LEDs were used in the more complicated version, but this exact code will not work)

Code: [Select]
#define LED 13
#define LED1 12
#define LED2 11
#define LED3 10
#define LED4 9
#define LED5 8
#define LED6 7
#define LED7 6
#define BUTTON 4

int val = 0



void setup()
{
 pinMode(LED, OUTPUT);
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
 pinMode(LED3, OUTPUT);
 pinMode(LED4, OUTPUT);
 pinMode(LED5, OUTPUT);
 pinMode(LED6, OUTPUT);
 pinMode(LED7, OUTPUT);
 pinMode(BUTTON, INPUT);
}

void loop(){
 val = digitalRead(BUTTON);

 if (val == HIGH) {
 digitalWrite(LED, HIGH);
 } else {
 digitalWrite(LED, LOW);
 }
}


thanks

conor1148

gotta love my idiocy, i found the problem, i missed the ";" after the "0" in the int line.

Groove

Just one thing:
Code: [Select]
 val = digitalRead(BUTTON);

if (val == HIGH) {
 digitalWrite(LED, HIGH);
 } else {
 digitalWrite(LED, LOW);
 }
}  


If "val" is not "==HIGH", it is "==LOW"
hence:
Code: [Select]
val = digitalRead(BUTTON);
digitalWrite(LED, val);

;)
Per Arduino ad Astra

Udo Klein

And if val is not needed elsewhere it can be removed completely ;)

Code: [Select]

 digitalWrite(LED, digitalRead(BUTTON));
Check out my experiments http://blog.blinkenlight.net

PaulS

Hard to de-bounce the button that way, if that's important.

conor1148

sorry guys im confused, can you dumb that down for me?

apparently this was supposed to let the button turn it on and off, but it only turns it on, and to turn it off i have to reset the board. any help?

retrolefty

Quote
but it only turns it on, and to turn it off i have to reset the board. any help?


Sounds like a wiring problem with your switch, having a "floating input" when the switch is released. Search around here for pull-ups, pull-downs, and enabling pin software pull-ups. Show us how your switch is wired to the input pin.

Lefty


PaulS

The code, as you wrote it, will turn the LED on when the button is pushed, and turn it off when the button is not pushed.

This assumes, of course, that you are using the standard momentary contact pushbutton switch, and that the switch is wired correctly.

Groove's comment was about the fact that the digitalRead function returns either HIGH or LOW, which are the same values that digitalWrite expects as input arguments. So, it is not necessary to test whether the value is HIGH or LOW, if all you are going to do is then pass the value to digitalWrite.

Udo was pointing out that it isn't even necessary to store the value from digitalRead. The function call itself can be use in place of the variable that is normally passed to digitalWrite.

My comment was that switches bounce, and debouncing is sometimes required. Without capturing the output from digitalRead, and evaluating it, you can not do the debouncing.

In your simple case, that is not necessary.

conor1148

im sorry, im completely lost, i guess im trying to move too quickly..

retrolefty

Quote
i guess im trying to move too quickly..


Possibly so. The Arduino does not come with a user pushbutton (not counting the reset switch, which can only reset the chip), so how did you wire up your switch? Did you use an example posted somewhere? Lets get the physical switch wiring correct first and then deal with any remaining software issues.

Lefty

conor1148

i got it to work now, sorry to waste space on the forum.

PaulS

You're not the 1st person to have these kind of problems, and won't be the last. The information exchanged when a newbie has problems is useful for other newbies having the same problems.

It helps those of us that have been programming for a while remember that what comes easily (now) to us is as a result of years of practice, and serves as a reminder that the answers we give need to be in easy to understand terms.

So, don't consider your posting as a waste of space on the forum.

What did you need to do to "get it working"?

conor1148

i typed in the wrong code, i typed in a code for a different project than the one i was wanting.

im having a problem with a much more complex project, that as far as i know im doing everything right except im confused about the wiring of a button.

should i post that here or start a new thread?

PaulS

Start a new thread, with appropriate title.

Go Up