Go Down

Topic: Brackets error (Read 826 times) previous topic - next topic

jonbloom

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
  }
 
}

Coding Badly

Hello.

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


Why?

fungus


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.

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

CrossRoads

#3
Nov 20, 2012, 10:10 pm Last Edit: Nov 20, 2012, 10:12 pm by CrossRoads Reason: 1
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.
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.

Arrch


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.

Coding Badly

#5
Nov 20, 2012, 10:13 pm Last Edit: Nov 20, 2012, 10:15 pm by Coding Badly Reason: 1

Oops.

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

Code: [Select]
/*
*  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
 }
 
}

PeterH

#6
Nov 20, 2012, 10:37 pm Last Edit: Nov 20, 2012, 11:38 pm by PeterH Reason: 1

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.  :0
I only provide help via the forum - please do not contact me for private consultancy.

CrossRoads

Or maybe its not recognizing the comments?

// switch
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.

AWOL

What use is a simulator that uses tools that give different results to the standard set?
"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.

jonbloom

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. 

Go Up