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.
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
}
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.
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"
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.
@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
}
}
jonbloom:
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