i need help in an error: expected initializer before 'if'

motor_test.ino: In function ‘void loop()’:
motor_test:48: error: expected initializer before ‘if’
motor_test:64: error: expected ‘}’ at end of input expected initializer before ‘if’

This is a circuit about controlling one motor with Bluetooth shield in the tow direction can u help me doing it.

motor_test.ino (1.05 KB)

int motor1Pin = 10; int motor2Pin = 11; int state = 0; int flag = 0;

void setup() {

pinMode(motor1Pin, OUTPUT); digitalWrite (motor1Pin, LOW); pinMode(motor2Pin, OUTPUT); digitalWrite(motor2Pin, LOW);

Serial.begin(9600); }

void loop() { // put your main code here, to run repeatedly: if (Serial.available() > 0) { state = Serial.read(); flag = 0; }

if (state == 'OFF') { digitalWrite(motor1Pin, LOW); if (flag == 0) { Serial.println ( "MOTOR: OFF"); flag = 1; } }

else if (state == 'on') { digitalWrite(motor1Pin, HIGH); if (flag == 0) { Serial.println ("MOTOR: ON"); if (flag == 0) { flag = 1; } } } void loop()

if (Serial.available() > 0) { state = Serial.read(); flag = 0; }

if (state == 'off2') { digitalWrite(motor2Pin, LOW); if (flag == 0) { Serial.println ( "MOTOR2: OFF"); flag = 1; } }

else if (state == 'on2') { digitalWrite(motor2Pin, HIGH); if (flag == 0) { Serial.println ("MOTOR2: ON"); if (flag == 0) { flag = 1; } } }

i need to control one motor in the tow direction using a Bluetooth shield.
i will attache what i did but i have a problem in it pls help

motor_test.ino (1.05 KB)

The error message tells you which line the compiler doesn't understand. The error is usually a few lines before this. You have void loop() in there twice.

can u correct it and send it back please

int motor1Pin = 10;
int motor2Pin = 11;
int state = 0;
int flag = 0;

void setup() {

  pinMode(motor1Pin, OUTPUT);
  digitalWrite (motor1Pin, LOW);
  pinMode(motor2Pin, OUTPUT);
  digitalWrite(motor2Pin, LOW);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    state = Serial.read();
    flag = 0;
  }



  if (state == 'OFF') {
  digitalWrite(motor1Pin, LOW);
  if (flag == 0) {
    Serial.println ( "MOTOR: OFF");
    flag = 1;
  }
}

else if (state == 'on') {
  digitalWrite(motor1Pin, HIGH);
  if (flag == 0) {
    Serial.println ("MOTOR: ON");
    if (flag == 0) {
      flag = 1;
    }
  }
}
void loop() 

    if (Serial.available() > 0) {
    state = Serial.read();
    flag = 0;
  }




  if (state == 'off2') {
  digitalWrite(motor2Pin, LOW);
  if (flag == 0) {
    Serial.println ( "MOTOR2: OFF");
    flag = 1;
  }
}

else if (state == 'on2') {
  digitalWrite(motor2Pin, HIGH);
  if (flag == 0) {
    Serial.println ("MOTOR2: ON");
    if (flag == 0) {
      flag = 1;
    }
  }
}
  • you should also document your code, im still a noob to coding my self!

try to explain about your project a little more, what it currently dose & what you want it to do etc.

if (state == 'OFF') {

Well, this is nonsense, to start with. For at least two reasons.

Instead of wasting your time writing nonsense, get an elementary textbook or online tutorial on basic C/C++ language and syntax.

Have a look at the examples in serial input basics and note that Serial.read() only reads a single character.

...R

You got a whole bunch of stupid errors there. Wasn't I looking at this mess about 5 minutes ago ?

@uuemad : You need to understand how Serial communication works with the Arduino. Do a google search for "Simple Arduino Serial communication tutorial" - get a good understanding of how that part works, and I think you will be able to move forward with your project.

Your logic is ok, but as michinyon stated - your syntax is not correct

uuemad:
can u correct it and send it back please

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

int state = 0;
...
  if (state == 'OFF') {

That won't work. "state" is an int, it cannot hold 3 bytes.

thanks guys for your help and I'm sorry if i wasted your time in my project. (i start working with the arduino from two days ago so i still a beganer ) THANKS again

There are two basic parts to software code / programming. Code / language SYNTAX and code flow.

Start with File>Examples> 01 Basic >BareMinimum to get the base of your program and compile it . It won't do anything visible but it will compile AKA the code will be of correct syntax ( computer language of sort) and the processor will understand it.

The code flow is very simple also - the processor will execute / run two functions - setup and loop.

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

When you get this program to compile use another IDE example File>Examples> 01 Basic >Blink. That program follows same format as BareMinimum, so download it and compile it. When it runs it will blink LED on you processor, hence you will have an indicator your program uses correct syntax ( it compiled) and its code flow is also correct- it does something ( visible).

Now when you get that working, come back here and we will rebuild YOUR program using correct syntax and figure out the flow together.

Vaclav, in what way did that answer his question about:

motor_test:64: error: expected '}' at end of input expected initializer before 'if'

Your answer does not even mention "if" statements.