system
September 15, 2011, 12:10pm
1
Hey everyone, we're making an alarm for a school project. We have a problem that we can't solve ourselves.
Here is the code:
We're getting the error code:
alarm:10: error: expected unqualified-id before 'if'
alarm:15: error: expected unqualified-id before 'else'
Best regards
All excutable code (apart from variable initialization) needs to be inside a function. Your if statements aren't. To get it to compile you will need to move them inside the loop function. To get it to work, you will, among other things, need to use digitalRead to get the state of your input.
system
September 15, 2011, 12:25pm
3
if(afbryder == 1) {
You've give the constant "afbryder" the value 13.
It is never, ever going to be equal to 1.
As Wildbill said, you need to read the pin.
If you don't want to run "loop", then put all your code into "setup".
system
September 20, 2011, 8:05am
4
Alright, we tried fixing it, but it still won't work. It compiles fine, but the speaker doesn't turn on.
We tried several different ways, and now we would appreciate some help from outside. Here's what we have right now:
system
September 20, 2011, 8:15am
5
For short code, it isn't necessay to use pastebin, just use the code tags here.
void loop () {
if(digitalRead (afbryder == 0)) {
//does nothing
} //ends if
else if (digitalRead (afbryder == 1)) {
digitalWrite(SPKR, HIGH); // Turns on speaker
delay(1000); // Turns on sound for one sec.
digitalWrite(SPKR, LOW); // Turns off speaker
delay(100); // Waits 100 mili seconds before turning speaker back on
}
} //ends loop
"digitalRead" can only return zero or one, if it returns one, then it isn't necessary to test for it returning zero, a simple else will suffice.
Is the speaker really just that?
They don't really like DC.
void loop () {
if(digitalRead (afbryder == 1)) {
for (int i = 0; i < 30000; ++i) {
digitalWrite(SPKR, HIGH); // Turns on speaker
delayMicroseconds(500);
digitalWrite(SPKR, LOW); // Turns off speaker
delayMicroseconds(500);
}
}
} //ends loop
Edit: Oops, missed this biggy!
if(digitalRead (afbryder == 0))
You need to be careful where you put your brackets!
system
September 22, 2011, 8:52am
6
AWOL:
Edit: Oops, missed this biggy!
if(digitalRead (afbryder == 0))
You need to be careful where you put your brackets!
What do you mean by that? Is there something wrong with it?
When you disconnect the wire from "afbryder" the speaker should turn on. That's what we're trying to make, but we can't get it to work properly.
Thanks for the help so far.
system
September 22, 2011, 8:56am
7
if(digitalRead (afbryder == 0))
Yes, there's something wrong.
You've given "afbryder" the value 13, so it is never going to be equal to zero, so your digital read will always be from pin zero.
system
September 22, 2011, 8:58am
8
As far as i know, we put "afbryder" to pin 13? Not equal to 13.
system
September 22, 2011, 9:11am
9
No, look at what you have written - it is the value read from the pin that you want to compare against zero, not the pin number itself.
Your parentheses are in the wrong place.if(digitalRead (afbryder) == 0)