Thanks for the feedback.
I will add some more debugging code to the program to see whether I can see what is causing the problem and get back to you.
Thanks for the feedback.
I will add some more debugging code to the program to see whether I can see what is causing the problem and get back to you.
Here is a new version for you to try.
I have changed the program so that it only sets the motor direction once for each command input rather than repeating the motor control commands until another one is entered which may have been causing a problem. I have also added a message to each of the motor control functions so that we can see which one is being called but I don't currently have the hardware available to test the code with an actual motor or motor driver. As the test code at the start of the program works OK in controlling the motor then the motor control functions would seem to be OK.
Please try the new version, let me know how it behaves and post the output if there are problems.
As a matter of interest what do you have the line ending set to in the Serial monitor ? The best option would be "No line ending"
/*
* Control DC motor with Smartphone via bluetooth
*/
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 5; // pin 1 on L293D IC
int state;
int flag = 0; //makes sure that the serial only prints once the state
unsigned long currentTime; //current time used throughout the program
unsigned long startTime; //start time of period used throughout the program
unsigned long period = 5000; //change this to alter the run period
boolean timing = false; //flag to indicate that timing is in progress
void setup()
{
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(115200);
//temporary code to test the motor control functions
Serial.println("Testing motor functions. Please wait");
motorRight();
delay(5000);
motorStop();
delay(2000);
motorLeft();
delay(5000);
motorStop();
delay(2000);
Serial.println("Finished testing motor functions");
}
void loop()
{
currentTime = millis(); //current time
//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 (state == '0')
{
motorStop(); //stop the motor
if (flag == 0)
{
Serial.println("Motor: off");
flag = 1;
}
}
// if the state is '1' the motor will turn right
else if (state == '1')
{
motorRight(); //turn motor to the right
if (flag == 0)
{
Serial.println("Motor: right");
flag = 1;
startTime = millis(); //start time of 1 second period **************
timing = true;
Serial.println("timing");
}
}
// if the state is '2' the motor will turn left
else if (state == '2')
{
motorLeft(); //turn the motor to the left
if (flag == 0)
{
Serial.println("Motor: left");
flag = 1;
startTime = millis(); //start time of timing period **************
timing = true;
Serial.println("timing");
}
}
}
if (timing) //check whether period has ended
{
if (currentTime - startTime >= period) //period has ended
{
timing = false;
motorStop();
}
}
}
void motorStop()
{
Serial.println("in motorStop()");
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low turn off the motor
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
void motorRight()
{
Serial.println("in motorRight()");
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
}
void motorLeft()
{
Serial.println("in motorLeft()");
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
Hi and thanks for all your support
Please find the report about all my tests of your latest code modification
1- Either the IDE is opened or not, the motor will turns once to right and left as soon as the power connects to the arduino board
2- The motor turns to right and left as soon as the IDE Serial Monitor is opened
3- As you can see, one of the main previous problems that was continuous turning without matching the timing is gone and that is OK with thanks but the problem of not turning right by sending command “1” and rare times left by sending command “2 is still there and to turn the motor right, no way except sending command “2” to turn the motor left and then send “1” command to turn the motor right.
4- The red highlights are also for the times that although command “2” is sent and the Serial monitor shows the motor turned left but the motor did not turn.
I hereby send you attached the Serial monitor log and I highlighted the fails by green color.
Thanks for the details
The motor running when the sketch is started or the Serial monitor is opened is normal. I added that to make sure that the motor control code was working. Opening the Serial monitor resets the Arduino so the code runs again.
I think (hope) that I have tracked down the problem and the reason why it worked for me and not for you.
I had put the code that starts timing inside your test as to whether the message had been printed which used the flag variable. In principle what you had done was correct, but the flag variable was reset to zero whenever Serial input was received. I believe that you have the line ending in the Serial monitor set to something other than "No line ending" so when you send say a '1' other characters are sent which confuses the situation. I was testing with no line ending so did not get the same problems.
The code below has been changed so that starting timing does not depend on whether or not a message has been printed and I have changed the flag variable to a boolean and given it a better name. I have also changed the state variable to a char as it is the proper data type to use for text and I have added a number of debugging statements so that the program flow is easier to determine when debugging.
Please try the program with various lin ending settings in teh Serial monitor and let me know how you get on
/*
* Control DC motor with Smartphone via bluetooth
*/
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 5; // pin 1 on L293D IC
char state;
boolean messagePrinted = false; //makes sure that the serial only prints once the state
unsigned long currentTime; //current time used throughout the program
unsigned long startTime; //start time of period used throughout the program
unsigned long period = 5000; //change this to alter the run period
boolean timing = false; //flag to indicate that timing is in progress
void setup()
{
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(115200);
//temporary code to test the motor control functions
Serial.println("Testing motor functions. Please wait");
motorRight("testing");
delay(5000);
motorStop("testing");
delay(2000);
motorLeft("testing");
delay(5000);
motorStop("testing");
delay(2000);
Serial.println("Finished testing motor functions");
}
void loop()
{
currentTime = millis(); //current time
//if some date is sent, reads it and saves in state
if (Serial.available() > 0)
{
state = Serial.read();
messagePrinted = false;;
// if the state is '0' the DC motor will turn off
if (state == '0')
{
showState();
motorStop("state 0"); //stop the motor
if (!messagePrinted)
{
Serial.println("Motor: off");
messagePrinted = true;
}
}
// if the state is '1' the motor will turn right
else if (state == '1')
{
showState();
motorRight("state 1"); //turn motor to the right
startTime = currentTime; //start time of 1 second period **************
timing = true;
Serial.println("timing");
if (!messagePrinted)
{
Serial.println("Motor: right");
messagePrinted = true;
}
}
// if the state is '2' the motor will turn left
else if (state == '2')
{
showState();
motorLeft("state 2"); //turn the motor to the left
startTime = currentTime; //start time of timing period **************
timing = true;
Serial.println("timing");
if (!messagePrinted)
{
Serial.println("Motor: left");
messagePrinted = true;
}
}
}
if (timing) //check whether period has ended
{
if (currentTime - startTime >= period) //period has ended
{
timing = false;
motorStop("timing ended");
}
}
}
void motorStop(char * source)
{
Serial.println("in motorStop()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low turn off the motor
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
void motorRight(char * source)
{
Serial.println("in motorRight()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
}
void motorLeft(char * source)
{
Serial.println("in motorLeft()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
void showState()
{
Serial.print("State : ");
Serial.println(state);
}
Bravoooo and thank you very very much.
I was not in town and just got your message tonight and uploaded your new sketch immediately and tested the system. This is working great until now without even one failure. Really professional assistance and follow up.
I continue testing the system and keep you update.
Regards
Good news.
The problem turned out to be simple, as many of them are.
When you are ready you can take out all of the debugging Serial.prints()s or comment the lines in case you need them later.
Hi my friend and really thanks for all your efforts.
I tested your code for several times during past two days and just can say again thank you very very very much.
YOUR code worked great even without one single failure and this was amazing since you saw how much time you patiently and kindly spent on it to correct all. Again Thanks
Now may you let me know what the final code without self-checking the system is that the system does not turn right ad left automatically when the power is connected or the serial monitor opened?
Regards
Now may you let me know what the final code without self-checking the system is that the system does not turn right ad left automatically when the power is connected or the serial monitor opened?
Take out these line
//temporary code to test the motor control functions
Serial.println("Testing motor functions. Please wait");
motorRight("testing");
delay(5000);
motorStop("testing");
delay(2000);
motorLeft("testing");
delay(5000);
motorStop("testing");
delay(2000);
Serial.println("Finished testing motor functions");
I had hoped that it would be obvious what these lines did from the comment and the messages printed.
Hi again and thanks for your instruction
The following error message is shown whole compiling after taking out the below code
//temporary code to test the motor control functions
Serial.println("Testing motor functions. Please wait");
motorRight("testing");
delay(5000);
motorStop("testing");
delay(2000);
motorLeft("testing");
delay(5000);
motorStop("testing");
delay(2000);
Serial.println("Finished testing motor functions");
Arduino: 1.6.12 (Windows XP), Board: "Arduino/Genuino Uno"
collect2.exe: error: ld returned 5 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Regards
Please post the whole program as it is now
Hi,below is the whole program
Regards
/*
void setup()
{
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
//temporary code to test the motor control functions
Serial.println("Testing motor functions. Please wait");
motorRight("testing");
delay(200);
motorStop("testing");
delay(2000);
motorLeft("testing");
delay(200);
motorStop("testing");
delay(2000);
Serial.println("Finished testing motor functions");
}
void loop()
{
currentTime = millis(); //current time
//if some date is sent, reads it and saves in state
if (Serial.available() > 0)
{
state = Serial.read();
messagePrinted = false;;
// if the state is '0' the DC motor will turn off
if (state == '0')
{
showState();
motorStop("state 0"); //stop the motor
if (!messagePrinted)
{
Serial.println("Motor: off");
messagePrinted = true;
}
}
// if the state is '1' the motor will turn right
else if (state == '1')
{
showState();
motorRight("state 1"); //turn motor to the right
startTime = currentTime; //start time of 1 second period **************
timing = true;
Serial.println("timing");
if (!messagePrinted)
{
Serial.println("Motor: right");
messagePrinted = true;
}
}
// if the state is '2' the motor will turn left
else if (state == '2')
{
showState();
motorLeft("state 2"); //turn the motor to the left
startTime = currentTime; //start time of timing period **************
timing = true;
Serial.println("timing");
if (!messagePrinted)
{
Serial.println("Motor: left");
messagePrinted = true;
}
}
}
if (timing) //check whether period has ended
{
if (currentTime - startTime >= period) //period has ended
{
timing = false;
motorStop("timing ended");
}
}
}
void motorStop(char * source)
{
Serial.println("in motorStop()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low turn off the motor
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
void motorRight(char * source)
{
Serial.println("in motorRight()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
}
void motorLeft(char * source)
{
Serial.println("in motorLeft()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
void showState()
{
Serial.print("State : ");
Serial.println(state);
}
Hi,below is the whole program
That is the original code with the test code in place.
Please post the code that you modified to remove the test code.
Hi again and thanks for your time
below is the modified code after removing the part of testing
/*
void setup()
{
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop()
{
currentTime = millis(); //current time
//if some date is sent, reads it and saves in state
if (Serial.available() > 0)
{
state = Serial.read();
messagePrinted = false;;
// if the state is '0' the DC motor will turn off
if (state == '0')
{
showState();
motorStop("state 0"); //stop the motor
if (!messagePrinted)
{
Serial.println("Motor: off");
messagePrinted = true;
}
}
// if the state is '1' the motor will turn right
else if (state == '1')
{
showState();
motorRight("state 1"); //turn motor to the right
startTime = currentTime; //start time of 1 second period **************
timing = true;
Serial.println("timing");
if (!messagePrinted)
{
Serial.println("Motor: right");
messagePrinted = true;
}
}
// if the state is '2' the motor will turn left
else if (state == '2')
{
showState();
motorLeft("state 2"); //turn the motor to the left
startTime = currentTime; //start time of timing period **************
timing = true;
Serial.println("timing");
if (!messagePrinted)
{
Serial.println("Motor: left");
messagePrinted = true;
}
}
}
if (timing) //check whether period has ended
{
if (currentTime - startTime >= period) //period has ended
{
timing = false;
motorStop("timing ended");
}
}
}
void motorStop(char * source)
{
Serial.println("in motorStop()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low turn off the motor
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
void motorRight(char * source)
{
Serial.println("in motorRight()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
}
void motorLeft(char * source)
{
Serial.println("in motorLeft()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
void showState()
{
Serial.print("State : ");
Serial.println(state);
}
Also below is the error message
Arduino: 1.6.12 (Windows XP), Board: "Arduino/Genuino Uno"
collect2.exe: error: ld returned 5 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Regards
Compiles OK for me.
Win 10
IDE 1.8.5
Arduino Nano or Uno
Can you compile and upload other programs ?
Yes, I can compile exactly program without removing the testing part.
Regards
Well, I just copied the program from your post, copied it to the IDE and it compiled without any problem.
It would help in copying the program if you posted it in code tags like this
/*
* Control DC motor with Smartphone via bluetooth
*/
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 5; // pin 1 on L293D IC
char state;
boolean messagePrinted = false; //makes sure that the serial only prints once the state
unsigned long currentTime; //current time used throughout the program
unsigned long startTime; //start time of period used throughout the program
unsigned long period = 200; //change this to alter the run period
boolean timing = false; //flag to indicate that timing is in progress
void setup()
{
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop()
{
currentTime = millis(); //current time
//if some date is sent, reads it and saves in state
if (Serial.available() > 0)
{
state = Serial.read();
messagePrinted = false;;
// if the state is '0' the DC motor will turn off
if (state == '0')
{
showState();
motorStop("state 0"); //stop the motor
if (!messagePrinted)
{
Serial.println("Motor: off");
messagePrinted = true;
}
}
// if the state is '1' the motor will turn right
else if (state == '1')
{
showState();
motorRight("state 1"); //turn motor to the right
startTime = currentTime; //start time of 1 second period **************
timing = true;
Serial.println("timing");
if (!messagePrinted)
{
Serial.println("Motor: right");
messagePrinted = true;
}
}
// if the state is '2' the motor will turn left
else if (state == '2')
{
showState();
motorLeft("state 2"); //turn the motor to the left
startTime = currentTime; //start time of timing period **************
timing = true;
Serial.println("timing");
if (!messagePrinted)
{
Serial.println("Motor: left");
messagePrinted = true;
}
}
}
if (timing) //check whether period has ended
{
if (currentTime - startTime >= period) //period has ended
{
timing = false;
motorStop("timing ended");
}
}
}
void motorStop(char * source)
{
Serial.println("in motorStop()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low turn off the motor
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
void motorRight(char * source)
{
Serial.println("in motorRight()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
}
void motorLeft(char * source)
{
Serial.println("in motorLeft()");
Serial.print("Called from : ");
Serial.println(source);
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
}
void showState()
{
Serial.print("State : ");
Serial.println(state);
}
What happens if you just comment out the test section rather than deleting it ?
What OS, Arduino and IDE version are you using ?
Have you got anything connected to pins 0 and 1 ?
Hi and thanks for your follow up
1- I don't know how to post the code in the code tag. please teach me if necessary
2- how should I comment out the test section rather than deleting it?
3- My OS is windows XP professional second edition and the version of IDE that I use is 1.6.12
4- I disconnect the pin 0 and 1 any time I compile or upload the code
5- I tested to see what the problem is compiling the code after deleting the testing part is, so I deleted line by line and I saw when I delete the first delay, the compiling is failed. I hereby send you the last code I have that not only compiled and upload but stopped testing, so you can see what the problem was.
5-1) void setup()
{
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
delay(200);
}
NOTE: THE COMPILING FAILS IF I DELETE "delay(200);"
6- The final question is, printing the log for each function is too long, for example, the IDE serial monitor shows below lines each time the function is done.
State : 1
in motorRight()
Called from : state 1
timing
Motor: right
in motorStop()
Called from : timing ended
So, may you teach me how I can short it, for example for state 1, the serial print just print "Right"
Regards
1- I don't know how to post the code in the code tag. please teach me if necessary
2- how should I comment out the test section rather than deleting it?
3- My OS is windows XP professional second edition and the version of IDE that I use is 1.6.12
1 - read read this before posting a programming question. There is also a link to it at the top of the forum page
2 - put // in front of each line
3 - Google for arduino xp error. It is a known problem
Thanks and seems you tired. sorry for all troubles I force on you
May you let me know how I can set the code to print or not print what I am looking for in IDE serial monitor?
I tried to use // in front of all lines which I thought that should be related the printing but no success.
Regards
The problem compiling on Windows XP with some versions of the IDE is a known one. Did you try Googling for it ?
If you want to stop some, maybe all, of the messages being printed then delete those lines of code or put // in front of them, but until you fix the problem with using Windows XP I cannot help any further.