I am stuck in conflict between softwareSerial and Servo library. I also take and try some tutorial from other websites. Some authors recommend to increase baud rate and use delay >=800ms to solve the problem. I both try 2 methods, but the servo is still shaking and some time the received data is not correct.
There are also other people to recommend me to use ServoTimer2 library, but I have tried this yet.
IF one experienced using ServoTimer2 library to solve the problem or solve the problem successfully in different way, please help me.
Sincere thanks in advance
or solve the problem successfully in different way, please help me.
I got a Mega. Problem solved.
PaulS:
I got a Mega. Problem solved.
Hey Paul Thank for your reply.
I am also using MEGA 2560 as Slaver. Hey can you show me how to solve problem. That is really helpful to my project.
I have another question
I connect TX and RX of bluetooth to PIn 10 and PIn 11 of Mega respectively. Is there of other pins to connect with bluetooth to avoid conflict because pin 10 of mega is used to interrupt .
I connect TX and RX of bluetooth to PIn 10 and PIn 11 of Mega respectively.
Why? Those are not hardware serial pins.
PaulS:
Why? Those are not hardware serial pins.
It is so embarrassingly to say that I just follow the instruction of someone, but when I changed the pins such as using Pin0 - Pin1. I do not receive data. Do I need cross connection. Anyways, can you share your wisdom to solve the conflict softwareserial and servo
#include <Servo.h>
#include <SoftwareSerial.h>
//#include <Servo.h>
#define BT_SERIAL_TX 1
#define BT_SERIAL_RX 0
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);
Servo myservo;
int val;
int array[2];
void setup()
{
Serial.begin(19200);
BluetoothSerial.begin(9600);
}
void loop()
{
myservo.detach();
int incoming = BluetoothSerial.read();
myservo.attach(7);
if (BluetoothSerial.available()>0){
array[0]= BluetoothSerial.parseInt();
myservo.write(array[0]);
Serial.println(array[0]);
delay(100);
}
}
It is so embarrassingly to say that I just follow the instruction of someone, but when I changed the pins such as using Pin0 - Pin1. I do not receive data.
You can't do hardware serial and software serial on the same set of pins at the same time.
There are three other hardware serial ports. Connect the bluetooth device to TX1 and RX1 and use Serial1 to read from/write to it. Or use TX2 and RX2 and Serial2, or TX3, RX3, and Serial3.
PaulS:
You can't do hardware serial and software serial on the same set of pins at the same time.There are three other hardware serial ports. Connect the bluetooth device to TX1 and RX1 and use Serial1 to read from/write to it. Or use TX2 and RX2 and Serial2, or TX3, RX3, and Serial3.
Hey I am a just a newbie, but Im really interesting to learn something new. As you said, we can notuse hardware serial and software serial at the same time. So in my code, I will eliminate #include(Softwareserial.h), am I? And one more thing , when I use hardware serial, is the conflict solved
I really dont want to bother your time, but I am really rushed for this project
So in my code, I will eliminate #include(Softwareserial.h), am I?
Are you what? Right? Yes.
And one more thing , when I use hardware serial, is the conflict solved
If the conflict is caused by SoftwareSerial hogging all the time in processing interrupts, yes. But, since you haven't posted any code, who knows?
but I am really rushed for this project
Waited until the last minute, eh?
PaulS:
If the conflict is caused by SoftwareSerial hogging all the time in processing interrupts, yes. But, since you
Here is my code
Sending
int potpin = A0;
int val,gt;
int val1=0;
int i;
void setup()
{
Serial.begin(9600);
}
void loop()
{
val = analogRead(potpin);
gt = map(val, 0, 1023, 0, 180);
int array[]={gt};
Serial.println(array[0]);
delay(300);
}
Receive
#include <Servo.h>
Servo myservo;
int val;
int array[2];
void setup()
{
Serial.begin(9600);
}
void loop()
{
myservo.detach();
int incoming = Serial.read();
myservo.attach(7);
if (Serial.available()>0){
array[0]= Serial.parseInt();
myservo.write(array[0]);
Serial.println(array[0]);
delay(100);
}
}
In Mega, I use hardware serial 0 (Pin 0 and Pin 1) , why the servo still vibrates ( not to much ) Please show me how to solve it
int array[]={gt};
Why do you need a one element array that contains the same data as some other variable?
In Mega, I use hardware serial 0 (Pin 0 and Pin 1) , why the servo still vibrates ( not to much ) Please show me how to solve it
Simple. Stop detaching and reattaching the servo.
The array on the receiver is pointless, too.
PaulS:
int array[]={gt};Why do you need a one element array that contains the same data as some other variable?
I know, but I just delete the length of the array , so that it is more readable to you. Thanks for your advice I will stop attach and reattach.
Dont leave the post. I will try it again and will confirm the result tomorrow
I really appreciate to your enthusiastic help!
PaulS:
Simple. Stop detaching and reattaching the servo.
Hey PaulS, your advice is really helpful to me. After your help, I realize I am so dumb that not to use hardware serial for communication.
Finally, since I used hardware serial and eliminate software serial, the servo is stop shaking. Therefore, I really appreciate your help!!
I have one question about delay between Master and Slaver. According to my observation, whether I increase or decrease time of delay in Master and Slaver, the data is received in Slaver sometime is wrong. For instance, when Master send 102, Slaver sometimes receive 2 or 100. It seems to be that Slaver could not catch up the speed of Master.
Can you guild me how to make communication between Master and Slaver more accurate.
Can you guild me how to make communication between Master and Slaver more accurate.
I think that would depend greatly on what your code looks like now.
phamb587:
Can you guild me how to make communication between Master and Slaver more accurate.
Use the 3rd example in Serial Input Basics to receive the data and make the transmitting device comply with that system.
If you can arrange always to send the same amount of data it will make the code easier to manage. For example, if I was controlling 3 servos I would always send the position data for all three even if only one of them had changed.
...R
Robin2:
Use the 3rd example in Serial Input Basics to receive the data and make the transmitting device comply with that system.If you can arrange always to send the same amount of data it will make the code easier to manage. For example, if I was controlling 3 servos I would always send the position data for all three even if only one of them had changed.
...R
Yeah I know what you mean, so in the Master code I set the condition whenever the potentiometers are changed, the data is sent. However, in the Slaver, whenever the master stops sending data, the data is received and it equals 0. This means, when the master stops sending, the data that slaver reads equals 0
Here my code in Master
int potpin = A0;
int val,gt;
int val1=0;
int i;
void setup()
{
Serial.begin(9600);
}
void loop()
{
val = analogRead(potpin);
gt = map(val, 0, 1023, 0, 180);
if(gt!=val1){ // to set condition, whenever Potentiometer changes, the data is sent
Serial.println(gt);
val1=gt;
}
}
the Slaver code
#include <Servo.h>
Servo myservo;
int val;
int array[2];
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
myservo.attach(7);
}
void loop()
{
int incoming = Serial1.read();
if (Serial1.available()>0){
val= Serial1.parseInt();
myservo.write(val);
Serial.println(val);
delay(100);
}
}
In master, do you know some sample code to maintain the previous servo position which can write to servo, when master stops sending
Im looking forward to your rely!
int incoming = Serial1.read();
if (Serial1.available()>0){
val= Serial1.parseInt();
You are reading from Serial1 regardless of whether there is anything to read. Why?
Your code suggests that you did not see any value in Serial Input Basics. As the author of the code I am curious to know why, in case I can improve something.
Sending data when a potentiometer value changes is a hit and miss affair. My approach is to read the potentiometer every (say) 100 millisecs (10 times per second) and send the value even if it is the same as the previous value.
That also makes it easier for the receiver to know that the sender has failed. If you don't get any data after (say) 1 second you can assume nothing is going to come.
...R
Robin2:
Your code suggests that you did not see any value in Serial Input Basics. As the author of the code I am curious to know why, in case I can improve something.Sending data when a potentiometer value changes is a hit and miss affair. My approach is to read the potentiometer every (say) 100 millisecs (10 times per second) and send the value even if it is the same as the previous value.
That also makes it easier for the receiver to know that the sender has failed. If you don't get any data after (say) 1 second you can assume nothing is going to come.
...R
Ok I will try to do as your advice And when i get result I will confirm to you Thanks!!!!
Robin2:
If you can arrange always to send the same amount of data it will make the code easier to manage. For example, if I was controlling 3 servos I would always send the position data for all three even if only one of them had changed....R
Hey Robin !
I would like to ask you one question
Nom I am controlling 3 servos separately with 4 potentiomers. Servo1 - Potentiometer 1; Serv2-potentiometer 2 and Servo 3 and Potentiometer 3.
My code sending
void loop()
{
val1 = analogRead(potpin1);
gt1 = map(val1, 0, 1023, 0, 180);
array[0]=gt1;;
val2 = analogRead(potpin2);
gt2 = map(val2, 0, 1023, 0, 180);
array[1]=gt2;
val3 = analogRead(potpin3);
gt3 = map(val3, 0, 1023, 0, 180);
array[2]=gt3;
array[3]=gt4;
if((gt1!=gan1)||(gt2!=gan2)||(gt3!=gan3)||(gt4!=gan4)){
//if(gt2!=gan2){
for(int i=0;i<=2;i++){ // send each position in array
Serial.println(array[i]);
delay(100);
}
gan1=gt1;
gan2=gt2;
gan3=gt3;
gan4=gt4;
// delay(100);
}
}
My receive code
void loop()
{
int incoming = Serial1.read();
if (Serial1.available()>0){ // read when data is sent
array[0]= Serial1.parseInt(); read data from array
array[1]= Serial1.parseInt();
array[2]= Serial1.parseInt();
myservo1.write(array[0]);
myservo2.write(array[1]);
myservo3.write(array[1]);
}
When I send 2 data just array[0] and array[1] to control 2 servo. They are working well. However, when I send 3 data array[0] and array[1] and array[2], the servo messed up. For instance, when I just connect one servo to PWM pin, it seems to be that there are 2 data written into servo.
How to send 3 data to control 3 servos separately without error
phamb587:
How to send 3 data to control 3 servos separately without error
Use the code in the link I gave you in Reply #13 - it works !
...R