Go Down

Topic: expected unqualified-id before if (Read 1 time) previous topic - next topic

Nater

I am writing a program for a simple line following robot and keep getting the same error message.  I am new to arduino and dont understand the error messages.  Any help would be aprecciated.
Heres the code.
Code: [Select]
// Using Arduino uno

// Pin definitions - attaches a variable to a pin.
const int RightMotor = 8; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int LeftMotor = 7; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
const int Sensor = A0; // This pin is used to read the value of the Right Sensor.


// Variable definitions
int Sensorv; //ldr sensor

// the setup() method runs once when the program is run. When the
// Arduino is reset, the setup() will be executed once again.

void setup() {

pinMode(LeftMotor, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(RightMotor, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(Sensor, OUTPUT);
Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600
Serial.println(" \nBeginning Light Seeking Behavior"); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop() {
Sensorv = 1023 - analogRead(Sensor); // This reads the value of the sensor, then saves it to the corresponding integer.
// This section of the sketch is used to print the values of the
// sensors through Serial to the computer. Useful for determining
// if the sensors are working and if the code is also functioning properly.

Serial.print("Left Sensor = "); // Prints the text inside the quotes.
Serial.print(Sensor); // Prints the value of the Left Sensor.
Serial.print("\t"); // Prints a tab (space).
}


// This section of the sketch is what actually interperets the data and then runs the motors accordingly.

if (Sensorv < 6 ) { //turn right
digitalWrite(RightMotor, LOW); // This is used to turn right. Notice the
digitalWrite(LeftMotor, HIGH; // opposite motor runs to turn right.
Serial.println("right"); //print to moniter
}

if (Sensorv > 8 ) { //turn left
digitalWrite(RightMotor, HIGH); // This is used to turn left. Notice the
digitalWrite(LeftMotor, LOW);   // opposite motor runs to turn left.
Serial.println("left"); //print to monitor
}
Serial.print("\n");
}

retrolefty

Simple typing errors, see blue text below. It now complies without errors, but I don't say it does what you wish yet, that's for you to test.


Quote
// Using Arduino uno

// Pin definitions - attaches a variable to a pin.
const int RightMotor = 8; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int LeftMotor = 7; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
const int Sensor = A0; // This pin is used to read the value of the Right Sensor.


// Variable definitions
int Sensorv; //ldr sensor

// the setup() method runs once when the program is run. When the
// Arduino is reset, the setup() will be executed once again.

void setup() {

pinMode(LeftMotor, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(RightMotor, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(Sensor, OUTPUT);
Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600
Serial.println(" \nBeginning Light Seeking Behavior"); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop() {
Sensorv = 1023 - analogRead(Sensor); // This reads the value of the sensor, then saves it to the corresponding integer.
// This section of the sketch is used to print the values of the
// sensors through Serial to the computer. Useful for determining
// if the sensors are working and if the code is also functioning properly.

Serial.print("Left Sensor = "); // Prints the text inside the quotes.
Serial.print(Sensor); // Prints the value of the Left Sensor.
Serial.print("\t"); // Prints a tab (space).
                // // remove this }

// This section of the sketch is what actually interperets the data and then runs the motors accordingly.

if (Sensorv < 6 ) { //turn right
digitalWrite(RightMotor, LOW); // This is used to turn right. Notice the
digitalWrite(LeftMotor, HIGH); // missing )  opposite motor runs to turn right.[/color]
Serial.println("right"); //print to moniter
}

if (Sensorv > 8 ) { //turn left
digitalWrite(RightMotor, HIGH); // This is used to turn left. Notice the
digitalWrite(LeftMotor, LOW);   // opposite motor runs to turn left.
Serial.println("left"); //print to monitor
}
Serial.print("\n");
}

Nater


Go Up