Pages: [1]   Go Down
Author Topic: School project help  (Read 496 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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".
 
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For short code, it isn't necessay to use pastebin, just use the code tags here.
Code:
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:
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:
if(digitalRead (afbryder == 0))
You need to be careful where you put your brackets!
« Last Edit: September 20, 2011, 05:22:24 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if(digitalRead (afbryder) == 0)
Logged

"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.

Pages: [1]   Go Up
Jump to: