Offline
Newbie
Karma: 0
Posts: 5
|
 |
« on: December 13, 2012, 09:43:11 am » |
Me and my friend don't know how to fix this error, or why it's accouring. Our code looks like this:
const int buttonPin = 2; const int hl = 3; int buttonState = 0; void setup() { pinMode(hl, OUTPUT);
pinMode(buttonPin, INPUT); } void loop(){
buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { // turn LED on: digitalWrite(hl, LOW); } else { // turn LED off: digitalWrite(hl, HIGH); } } { digitalWrite(hl, HIGH) delay(1000); digitalWrite(hl, low) delay(1000) }
When we try to verify it, we get the error: Button:57: error: expected unqualified-id before '{' token
Please tell us how to fix the problem!!
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
God Member
Karma: 19
Posts: 616
Scientia potentia est.
|
 |
« Reply #1 on: December 13, 2012, 09:49:03 am » |
Hello and welcome  You have few problems on the last lines: - First you forgot some ';' - Then if you check the count of '{' and '}' (and if you indented your code correctly) then you will see the last lines are outside of void loop(). Here is your code, exactly the same but correctly indented: const int buttonPin = 2; const int hl = 3; int buttonState = 0;
void setup() { pinMode(hl, OUTPUT); pinMode(buttonPin, INPUT); }
void loop() { buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { // turn LED on: digitalWrite(hl, LOW); } else { // turn LED off: digitalWrite(hl, HIGH); } }
{ digitalWrite(hl, HIGH) delay(1000); digitalWrite(hl, low) delay(1000) }
See the problem(s) ??
|
|
|
|
« Last Edit: December 13, 2012, 09:52:43 am by guix »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #2 on: January 05, 2013, 01:09:59 pm » |
Hi, thanks for the help, we tried to look at it again, but the error is still not fixed, maybe I don't really get what the error is. It comes with the same error when we try to put the '{' other places.
Please help.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #3 on: January 05, 2013, 01:14:49 pm » |
Me and my friend don't know how to fix this error, or why it's accouring (we have very little experience with programming). Our code looks like this:
const int buttonPin = 2; const int hl = 3; int buttonState = 0; void setup() { pinMode(hl, OUTPUT);
pinMode(buttonPin, INPUT); } void loop(){
buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { // turn LED on: digitalWrite(hl, LOW); } else { // turn LED off: digitalWrite(hl, HIGH); } } { digitalWrite(hl, HIGH); delay(1000); digitalWrite(hl, LOW); delay(1000); }
When we try to verify it, we get the error: Button:57: error: expected unqualified-id before '{' token
Please tell us how to fix the problem!!
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Online
Brattain Member
Karma: 277
Posts: 25493
Solder is electric glue
|
 |
« Reply #4 on: January 05, 2013, 01:18:01 pm » |
You must have an equal number of braces both { and }.
The braces must enclose blocks of code. The braces in that code are all over the place.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Online
Brattain Member
Karma: 277
Posts: 25493
Solder is electric glue
|
 |
« Reply #5 on: January 05, 2013, 01:19:32 pm » |
I imagine that this is what you want, but I am not sure. const int buttonPin = 2; const int hl = 3; int buttonState = 0; void setup() { pinMode(hl, OUTPUT);
pinMode(buttonPin, INPUT); } void loop(){
buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { // turn LED on: digitalWrite(hl, LOW); } else { // turn LED off: digitalWrite(hl, HIGH); }
digitalWrite(hl, HIGH); delay(1000); digitalWrite(hl, LOW); delay(1000); }
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #6 on: January 05, 2013, 01:19:45 pm » |
First, if you post please use the # button to tag code correctly Give this a try, I removed a { and added an extra delay(200) , .. (code not tested) const int buttonPin = 2; const int hl = 3; int buttonState = 0;
void setup() { pinMode(hl, OUTPUT); pinMode(buttonPin, INPUT); }
void loop() { buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(hl, LOW); } else { digitalWrite(hl, HIGH); } delay(200); // extra
digitalWrite(hl, HIGH); delay(1000);
digitalWrite(hl, LOW); delay(1000); }
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 48
Posts: 1408
May all of your blinks be without delay
|
 |
« Reply #7 on: January 05, 2013, 01:20:17 pm » |
Problem 1 - all the statements should end with a semi-colon like this digitalWrite(hl, HIGH);
Are there any lines in your sketch that don't ? Problem 2 - when, if ever, is this code going to be executed ? { digitalWrite(hl, HIGH) delay(1000); digitalWrite(hl, low) delay(1000) }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #8 on: January 05, 2013, 01:35:18 pm » |
First, if you post please use the # button to tag code correctly Give this a try, I removed a { and added an extra delay(200) , .. (code not tested) const int buttonPin = 2; const int hl = 3; int buttonState = 0;
void setup() { pinMode(hl, OUTPUT); pinMode(buttonPin, INPUT); }
void loop() { buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(hl, LOW); } else { digitalWrite(hl, HIGH); } delay(200); // extra
digitalWrite(hl, HIGH); delay(1000);
digitalWrite(hl, LOW); delay(1000); }
Hi, it worked, thanks, but why did you make an extra delay, and why did it work???
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: January 06, 2013, 02:40:03 am » |
Please don't cross post. Threads merged.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #10 on: January 06, 2013, 09:10:34 am » |
Hi, it worked, thanks, but why did you make an extra delay, and why did it work??? That is your homework:) Follow the code by hand, read it carefully line by line. Be the Arduino ! and do what the code says and you will understand.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #11 on: January 06, 2013, 03:01:47 pm » |
Follow the code by hand, read it carefully line by line. Be the Arduino ! and do what the code says and you will understand.
Well, ok, thank you! 
|
|
|
|
|
Logged
|
|
|
|
|
|