expected unqualified-id before ‘if'

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Autotechie
 
Posts: 2
Joined: Thu Nov 07, 2013 7:25 pm

expected unqualified-id before ‘if'

Post by Autotechie »

Hello,

I just started using the book Making Things Talk and I keep getting an error for the ‘Simple Serial’ sketch on page 29.
these are the error that keep coming back:
sketch_feb27a:29: error: expected unqualified-id before 'if'
sketch_feb27a:33: error: expected unqualified-id before 'if'
sketch_feb27a:37: error: expected declaration before '}' token

Code: Select all

/*
Simple Serial

Listens for an incoming serial byte, adds one to the byte 
and sends the result back out serially.
Also blink a LED on pin 13 every half second.
*/

int LEDPin = 13;        //you can use any I/O pin you want
int inByte = 0;        //variable to hold incoming serial data
long blinkTimer = 0;  //keeps track of how long since the LED was last turned off
int blinkInterval = 1000;  //a full second from on to off to on again

void setup() {
  pinMode(LEDPin, OUTPUT);    //set pin 13 to be an output
  Serial.begin(9600);      //configure the serial port for 9600 bps data rate
}

void loop () {
  //if there are any incoming bytes to read: if (Serial.available() > 0) {
   //then read the first available byte:
  inByte = Serial.read();
 //and add one to it, then send the result out:
 Serial.write(inByte+1);
  }
  
  //Meanwhile, keep blinking the LED.
  //after a half of a second, turn the LED on:
  if (millis() - blinkTimer >= blinkInterval / 2) {
    digitalWrite(LEDPin, HIGH);  //turn the LED pin 13 on
  }
  //after half a second, turn the LED off and reset the timer:
  if (millis() - blinkTimer >=blinkInterval) {
    digitalWrite(LEDPin, LOW);  //turn the LED pin 13 off
    blinkTimer = millis();     //reset the timer  
  }
}



Any ideas?? Thank you


--
Alexandre Balcer
Last edited by adafruit_support_bill on Sun Mar 02, 2014 7:59 am, edited 1 time in total.
Reason: Please use the 'code' button when submitting code - click 'code' and paste your code between the tags.

pdh
 
Posts: 174
Joined: Wed Dec 25, 2013 12:59 pm

Re: expected unqualified-id before ‘if'

Post by pdh »

The if statement in line 29 --- the one starting "if(millis()..." is outside of any function -- that's the problem.

You probably don't really intend this comment to encompass the "if":

//if there are any incoming bytes to read: if (Serial.available() > 0) {

Split that into two lines after the colon (':'); that will uncomment the "if (Serial.available..." line, and it will open the {-enclosed scope the way you probably intend it to be, so the loop() function encompasses more of the code.

Autotechie
 
Posts: 2
Joined: Thu Nov 07, 2013 7:25 pm

Re: expected unqualified-id before ‘if'

Post by Autotechie »

Thanks pdh, it worked! It was simple but not obvious to a newbie that is just starting out. You will probably see me here often haha!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”