Offline
Newbie
Karma: 0
Posts: 3
|
 |
« on: November 20, 2012, 03:35:49 pm » |
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
Online
Shannon Member
Karma: 118
Posts: 10155
|
 |
« Reply #1 on: November 20, 2012, 03:50:12 pm » |
Hello. I'm trying to run this really simple sketch through "Simulator for Arduino" ... Why?
|
|
|
|
|
Logged
|
|
|
|
|
Valencia, Spain
Online
Edison Member
Karma: 65
Posts: 2254
|
 |
« Reply #2 on: November 20, 2012, 04:01:52 pm » |
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
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 245
Posts: 16519
Available for Design & Build services
|
 |
« Reply #3 on: November 20, 2012, 04:10:05 pm » |
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
|
|
|
|
|
California
Online
Edison Member
Karma: 40
Posts: 1860
|
 |
« Reply #4 on: November 20, 2012, 04:12:23 pm » |
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
Online
Shannon Member
Karma: 118
Posts: 10155
|
 |
« Reply #5 on: November 20, 2012, 04:13:30 pm » |
Oops. @jonbloom, independent of the simulator problem, you need to call digitalRead.... /* * 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
Tesla Member
Karma: 89
Posts: 6377
-
|
 |
« Reply #6 on: November 20, 2012, 04:37:00 pm » |
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. 
|
|
|
|
« Last Edit: November 20, 2012, 05:38:53 pm by PeterH »
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 245
Posts: 16519
Available for Design & Build services
|
 |
« Reply #7 on: November 20, 2012, 04:40:20 pm » |
Or maybe its not recognizing the comments?
// switch
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19059
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: November 20, 2012, 04:51:33 pm » |
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.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #9 on: November 21, 2012, 11:15:00 am » |
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
|
|
|
|
|
|