Go Down

Topic: School project help (Read 543 times) previous topic - next topic

HTX2v

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:

http://pastebin.com/sJnD1tpb

We're getting the error code:

Quote
alarm:10: error: expected unqualified-id before 'if'
alarm:15: error: expected unqualified-id before 'else'


Best regards

wildbill

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.

AWOL

Code: [Select]
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".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

HTX2v

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:

http://pastebin.com/b5dKeu07

AWOL

#4
Sep 20, 2011, 10:15 am Last Edit: Sep 20, 2011, 12:22 pm by AWOL Reason: 1
For short code, it isn't necessay to use pastebin, just use the code tags here.
Code: [Select]
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.
Code: [Select]
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!
Code: [Select]
if(digitalRead (afbryder == 0))
You need to be careful where you put your brackets!
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

HTX2v


Edit: Oops, missed this biggy!
Code: [Select]
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.

AWOL

Code: [Select]
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

HTX2v

As far as i know, we put "afbryder" to pin 13? Not equal to 13.

AWOL

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.
Code: [Select]
if(digitalRead (afbryder) == 0)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up