expected initializer

Arduino: 1.6.12 (Mac OS X), Board: "Arduino/Genuino Uno"

sketch_nov13a:15: error: expected initializer before 'if'
if (buttonState == HIGH) {
^
exit status 1
expected initializer before 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Clearly, the error is in the code that you didn't post. Please post it, (between [code]code tags[/code]).

Incidentally, all error messages should also be placed between code tags, not posted inline.

Actual_Dragon:

sketch_nov13a:15: error: expected initializer before 'if'

if (buttonState == HIGH) {
  ^
exit status 1
expected initializer before 'if'

Well, we need to see what you have before your "if".

Please use code tags.

Read this before posting a programming question

How to use this forum

I guess a missing semi-colon :wink:

1 Like

#include <Servo.h>

Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
const int buttonPin = 2; // the number of the pushbutton pin

void setup() {
servoLeft.attach(A5); // Set left servo to analog pin 5
servoRight.attach(A4);// Set right servo to analog pin 4
pinMode(buttonPin, INPUT);
}
void loop()
if (buttonState == HIGH) {
void forward() {
servoLeft.write(0);
servoRight.write(180);
else(
}

void loop()
  if (buttonState == HIGH)

Missing {

Missing code tags

  if (buttonState == HIGH) {
  void forward() {

What's that "void" doing there?

void loop(){
if (buttonState == HIGH)
forward() {
servoLeft.write(0);
servoRight.write(180)

where do i put it?

C/C++ (or at least the Arduino dialect) doesn't allow nested function definitions, so I'd say "outside of your current function".

1 Like

Actual_Dragon:
void loop(){
if (buttonState == HIGH)
forward() {
servoLeft.write(0);
servoRight.write(180)

where do i put it?

@Actual_Dragon - start using code tags before I start locking your thread.

Please use code tags.

Read this before posting a programming question

How to use this forum

AWOL:
C/C++ (or at least the Arduino dialect) doesn't allow nested function definitions

Actually GCC does support nested functions, but only in C, not C++.

oqibidipo:
Actually GCC does support nested functions, but only in C, not C++.

Actually, I wrote "C/C++ (or at least the Arduino dialect) doesn't allow nested function definitions".
I know very well that nested functions can be allowed.

Arduino: 1.6.12 (Mac OS X), Board: "Arduino/Genuino Uno"

/Users/svteacher/Documents/Arduino/sketch_nov13a/sketch_nov13a.ino: In function 'void loop()':
sketch_nov13a:22: error: no match for call to '(Servo) (int)'
servoLeft(180);
^
sketch_nov13a:23: error: no match for call to '(Servo) (int)'
servoRight(0);
^
sketch_nov13a:25: error: no match for call to '(Servo) (int)'
servoLeft(90);
^
sketch_nov13a:26: error: no match for call to '(Servo) (int)'
servoRight(90);
^
exit status 1
no match for call to '(Servo) (int)'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

im getting this after i type this
[void loop(){
val==digitalRead(buttonPin); //read input value
if (val=HIGH) { //check if input is high
servoLeft(180);
servoRight(0);
delay(6000);
servoLeft(90);
servoRight(90);
}]

Are you missing some ".write"s?

You're certainly missing code tags.
Still.

Edit: ironically, it seems you're also missing the code that caused the error.

Actual_Dragon:
void loop(){
if (buttonState == HIGH)
forward() {
servoLeft.write(0);
servoRight.write(180)

where do i put it?

You put it before and after everything that's code. Put
** **[code]** **
before void loop(){ and put
** **[/code]** **
after servoRight.write(180).