Pages: [1]   Go Down
Author Topic: Brackets error  (Read 618 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!
I'm trying to run this really simple sketch through "Simulator for Arduino" ver V0.97B and it kicks out an error related to the brackets of my "if" statement.
 "Sketch Error - no opening bracket for switch"

My code has both opening and closing brackets containing the "to do" commands.  Not sure if this is a simulator glitch or syntax error on my part.  (Waiting for an Uno board delivery, thus simulator use.)

Any insights would be appreciated. 
Jon

It seems everyone at home thinks the dog had been fed, or he gets double fed.  So why not use a microcontroller, right?
/*
 *  Feed the Dog Timer
 *  When LED is green, dog has been fed; When red, dog is hungry.
 *  Momentary switch depression (green - fed) starts timer (long delay)
 *   upon timeout, (red - hungry)
 *  First microcontroller project by Jon Bloom, 11/2012
 */

int switchPin = 2;              // switch is connected to pin 2
int grnLED = 6;                 // green LED connected to pin 6
int redLED = 7;                 // red LED connected to pin 7


void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(grnLED, OUTPUT);      // Set fed LED pin as output
  pinMode(redLED, OUTPUT);      // Set hungry LED pin as output
}


void loop(){
 
  digitalWrite(grnLED,LOW);     // turns off fed lamp
  digitalWrite(redLED,HIGH);    // turns on hungry lamp
  if (switchPin==HIGH){
    digitalWrite(redLED,LOW);  // turns off hungry lamp
    digitalWrite(grnLED,HIGH); // turns on fed lamp
    delay(28800000);               // sets fed lamp on time duration, 8 hours
  }
 
}
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12861
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello.

I'm trying to run this really simple sketch through "Simulator for Arduino" ...

Why?
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 146
Posts: 5469
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello.

I'm trying to run this really simple sketch through "Simulator for Arduino" ...

Why?

Because he hasn't got his board yet. He says so in his post.

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26970
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use CTRL-T (autoformat), will help you spot mismatched qtys of { }, mayebe ( ) and [ ] even.
If nothing else, clickt to right of ], ), }, will highlight the opening (, {, [ so you can see what the code is doing.

Altho I don't see any problems like that here, nothing the IDE compiler would complain about.
« Last Edit: November 20, 2012, 04:12:23 pm by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!
I'm trying to run this really simple sketch through "Simulator for Arduino" ver V0.97B and it kicks out an error related to the brackets of my "if" statement.
 "Sketch Error - no opening bracket for switch"

It sounds like the simulator sees "switch" and assumes it means it's a switch statement. Try changing the name of the variable to something without switch.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12861
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Oops.

@jonbloom, independent of the simulator problem, you need to call digitalRead....

Code:
/*
 *  Feed the Dog Timer
 *  When LED is green, dog has been fed; When red, dog is hungry.
 *  Momentary switch depression (green - fed) starts timer (long delay)
 *   upon timeout, (red - hungry)
 *  First microcontroller project by Jon Bloom, 11/2012
 */

int switchPin = 2;              // switch is connected to pin 2
int grnLED = 6;                 // green LED connected to pin 6
int redLED = 7;                 // red LED connected to pin 7


void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(grnLED, OUTPUT);      // Set fed LED pin as output
  pinMode(redLED, OUTPUT);      // Set hungry LED pin as output
}


void loop(){
  
  digitalWrite(grnLED,LOW);     // turns off fed lamp
  digitalWrite(redLED,HIGH);    // turns on hungry lamp
  if ( digitalRead( switchPin ) == HIGH ) {
    digitalWrite(redLED,LOW);  // turns off hungry lamp
    digitalWrite(grnLED,HIGH); // turns on fed lamp
    delay(28800000);               // sets fed lamp on time duration, 8 hours
  }
  
}
« Last Edit: November 20, 2012, 04:15:37 pm by Coding Badly » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it kicks out an error related to the brackets of my "if" statement.
 "Sketch Error - no opening bracket for switch"

I don't see anything wrong with the pairing of { } and ( ) and no errors relating to switches or cases. I'd expect that to compile without error. However, I'm not a compiler - why don't you simply compile it in the Arduino IDE to find out whether it's actually valid?

If it compiles I don't think it would do what you intend since you are comparing the pin number switchPin instead of digitalRead(switchPin). I suppose it's barely possible that the simulator has somehow picked up on this but given a misleading error message.

Edit to add: the misleadingly-named Coding Badly beat me to it.  smiley-mad
« Last Edit: November 20, 2012, 05:38:53 pm by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26970
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Or maybe its not recognizing the comments?

// switch
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

What use is a simulator that uses tools that give different results to the standard set?
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: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Coding Badly.  That was SO obvious when pointed out.

FYI to all on simulator, it works with the digitalRead fix.  Simulator is pretty kewl IMHO. 
Logged

Pages: [1]   Go Up
Jump to: