Error keep appearing on my program. I dont know how to solve it.

Hi,

I am new to Arduino and have the following setup via watching tutorials from net.
I am using an Arduino Nano with L298N to control 2 dc motor and Bluetooth HC05.
I have written the following program but it shows error. Please advise me where I go wrong.
Thanking you in advance.
From
RLJF

//Right side motor:
int L298N_IN1 = 3;
int L298N_IN2 = 4;
int enable1Pin = 6;
//Left side motor:
int L298N_IN3 = 8;
int L298N_IN4 = 9;
int enable2Pin = 11;
int state;
int flag = 0; // makes sure that the serial only prints once the state:

void setup() {
// sets pin as outputs:
//initialize serial communication at 9600 bits per second:
pinMode (L298N_IN1, OUTPUT);
pinMode (L298N_IN2, OUTPUT);
pinMode (enable1Pin, OUTPUT);
pinMode (L298N_IN3, OUTPUT);
pinMode (L298N_IN4, OUTPUT);
pinMode (enable2Pin, OUTPUT);
//sets enable1Pin and enable2Pin in high so that motor can run on:
digitalWrite (enable1Pin, HIGH);
digitalWrite (enable2Pin, HIGH);
Serial.begin (9600);
}
void loop() {
//if some date is sent, reads it and saves in state
if (Serial.available () > 0) {
state = Serial.read ();
flag = 0;
}
// if the state is '0' the DC motor will turn off:
if (flag == '0') {
digitalWrite (L298N_IN1, LOW);
digitalWrite (L298N_IN2, LOW);
digitalWrite (L298N_IN3, LOW);
digitalWrite (L298N_IN4, LOW);
if (flag == 0) {
Serial.println ("Motor Off");
flag = 1;
}
// if thw state is '1' the DC motor will move forward:
else if (state == '1') {
digitalWrite (L298N_IN1, HIGH);
digitalWrite (L298N_IN2, LOW);
digitalWrite (L298N_IN3, LOW);
digitalWrite (L298N_IN4, HIGH);
if (flag == 0) {
Serial.println ("Motor Forward");
flag = 1;
}
// if the state is '2' the DC motor will turn left:
else if (state == '2') {
digitalWrite (L298N_IN1, HIGH);
digitalWrite (L298N_IN2, LOW);
digitalWrite (L298N_IN3, LOW);
digitalWrite (L298N_IN4, LOW);
if (flag == 0) {
Serial.println ("Motor Left");
flag = 1;
}
// if the state is '3' the DC motor will turn right:
else if (state == '3') {
digitalWrite (L298N_IN1, LOW);
digitalWrite (L298N_IN2, LOW);
digitalWrite (L298N_IN3, LOW);
digitalWrite (L298N_IN4, HIGH);
if (flag == 0) {
Serial.println ("Motor Right");
flag = 1;
}
// if the state is '4' the DC motor will reverse:
else if (state == '4') {
digitalWrite (L298N_IN1, LOW);
digitalWrite (L298N_IN2, HIGH);
digitalWrite (L298N_IN3, HIGH);
digitalWrite (L298N_IN4, LOW);
if (flag == 0) {
Serial.println ("Motor Reverse");
flag = 1;
}
//For debugging purpose;{
//Serial.println(state);
}
}
//Right side motor:
int L298N_IN1 = 3;
int L298N_IN2 = 4;
int enable1Pin = 6;
//Left side motor:
int L298N_IN3 = 8;
int L298N_IN4 = 9;
int enable2Pin = 11;
int state;
int flag = 0; // makes sure that the serial only prints once the state:

void setup() {
// sets pin as outputs:
//initialize serial communication at 9600 bits per second:
pinMode (L298N_IN1, OUTPUT);
pinMode (L298N_IN2, OUTPUT);
pinMode (enable1Pin, OUTPUT);
pinMode (L298N_IN3, OUTPUT);
pinMode (L298N_IN4, OUTPUT);
pinMode (enable2Pin, OUTPUT);
//sets enable1Pin and enable2Pin in high so that motor can run on:
digitalWrite (enable1Pin, HIGH);
digitalWrite (enable2Pin, HIGH);
Serial.begin (9600);
}
void loop() {
//if some date is sent, reads it and saves in state
if (Serial.available () > 0) {
state = Serial.read ();
flag = 0;
}
// if the state is '0' the DC motor will turn off:
if (flag == '0') {
digitalWrite (L298N_IN1, LOW);
digitalWrite (L298N_IN2, LOW);
digitalWrite (L298N_IN3, LOW);
digitalWrite (L298N_IN4, LOW);
if (flag == 0) {
Serial.println ("Motor Off");
flag = 1;
}
// if thw state is '1' the DC motor will move forward:
else if (state == '1') {
digitalWrite (L298N_IN1, HIGH);
digitalWrite (L298N_IN2, LOW);
digitalWrite (L298N_IN3, LOW);
digitalWrite (L298N_IN4, HIGH);
if (flag == 0) {
Serial.println ("Motor Forward");
flag = 1;
}
// if the state is '2' the DC motor will turn left:
else if (state == '2') {
digitalWrite (L298N_IN1, HIGH);
digitalWrite (L298N_IN2, LOW);
digitalWrite (L298N_IN3, LOW);
digitalWrite (L298N_IN4, LOW);
if (flag == 0) {
Serial.println ("Motor Left");
flag = 1;
}
// if the state is '3' the DC motor will turn right:
else if (state == '3') {
digitalWrite (L298N_IN1, LOW);
digitalWrite (L298N_IN2, LOW);
digitalWrite (L298N_IN3, LOW);
digitalWrite (L298N_IN4, HIGH);
if (flag == 0) {
Serial.println ("Motor Right");
flag = 1;
}
// if the state is '4' the DC motor will reverse:
else if (state == '4') {
digitalWrite (L298N_IN1, LOW);
digitalWrite (L298N_IN2, HIGH);
digitalWrite (L298N_IN3, HIGH);
digitalWrite (L298N_IN4, LOW);
if (flag == 0) {
Serial.println ("Motor Reverse");
flag = 1;
}
//For debugging purpose;{
//Serial.println(state);
}
}
expected '}' at end of input

expected '}' at end of input

Massive clue there.

Please remember to use code tags when posting code.

Perhaps two ‘setup()’ functions and two ‘loop()’ functions isn’t such a good idea. :wink:

Also, if ‘flag’ is ‘0’, it can’t also be 0:-

 if (flag == '0')    // *** Did you mean:-  if(state=='0')  ?
    {
        digitalWrite (L298N_IN1, LOW);
        digitalWrite (L298N_IN2, LOW);
        digitalWrite (L298N_IN3, LOW);
        digitalWrite (L298N_IN4, LOW);
        if (flag == 0)
        {
            Serial.println ("Motor Off");
            flag = 1;
        }

Some more closing curly brackets might be handy, too.

The only problem with that code is all the extra stuff you have in it. Please take a few moments to refactor that code. Remove the redundant code. Make sure all your brackets are matched up.

When you compile the code, the compiler is decent about telling you where the error is. It will normally give you the line and even column number where the error is.

Always remember that one syntactic error in your code can lead to many errors. Always work the errors from the top down. as you fix them, you can verify the application again and continue the job of getting the code to compile.

Once you get the code to compile you can start debugging it.

The autoformat tool in the IDE (Tools, Atuoformat or Crtl-t) will point out mismatched curly brackets ({}) and/or parenthesis. Autoformat also properly indents code, making the code easier to read and decipher.