My main problem is I was using the SoftwareSerial Library to get/send data from a Bluetooth module, all went OK, until i started controlling a servo, then noticed it didn't work as it should, I googled and found that Servo.h Conflicts with SoftwareSerial, A lot of people suggested different libraries for each the serial and servo and none worked.
A lot suggested to use the HardwareSerialLibaray which i'm guessing would work, but i dont want to block the main serial rx/tx, I have a mega so i i have a couple extra Serial pins i could use.
How can i edit the library so it uses those extra pins, or was the library intended to only be used on the 01 pins? Thanks a lot!
If I cant, I would appreciate help regarding the serial/servo problem. Thanks again!
There are 3 other serial ports that are completely separate from the USB serial that you use for upload and program output to serial monitor. There is absolutely no reason to use software serial on a Mega. The extra serial ports have their own pins. The use of those ports will not interfere with regular Serial (USB) or the Servo library.
You do not need a library or to modify anything. You use the extra ports exactly the same as Serial just different names for the ports (Serial1, Serial2, Serial3). Serial reference page.
The extra serial ports have their own pins. The use of those ports will not interfere with regular Serial (USB) or the Servo library.
Then i have an entierly different problem.
Here's whats happening,Whenever i write to the servo while bluetooth is connected(eg:section A in the code), the my phone disconnects to the connection to the bluetooth, and i have to reconnect again, The first command i send works , then the phone disconnects, heres a code:
#include <Servo.h>
#include <HardwareSerial.h>
Servo FlameServo;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600); //bluetooth
FlameServo.attach(35);
FlameServo.write(0); //servo
Serial.begin(19200);
}
char state;
int x,y;
void loop() {
while (Serial1.available()) {
state = Serial1.read();
if (state == 'A') { //servo left
delay(5);
if (Serial1.read() == '#') {
FlameServo.write(90); //command runs, but bluetooth disconnects after this,
delay(100);
}
}
if (state == 'S') { //spray water
delay(5);
if (Serial1.read() == '#')
Serial.println("Spra");
}
if (state == 'D') { //Servo Right
delay(5);
if (Serial1.read() == '#'){
Serial.println("d");
FlameServo.write(180); //command runs, but bluetooth disconnects after this,
delay(100);
}
}
delay(5);
}
}
scorpionma:
Then i have an entierly different problem.
Here's whats happening,Whenever i write to the servo while bluetooth is connected(eg:section A in the code), the my phone disconnects to the connection to the bluetooth, and i have to reconnect again, The first command i send works , then the phone disconnects, heres a code:
#include <Servo.h>
#include <HardwareSerial.h>
I think I misunderstood the problem, i was talking about teh HardwareSerial libary, but I dont think using that would help
Probably right there. Nobody else uses a hardware serial library, so why would you?
You keep talking about the phone disconnecting, but you might speculate that it is the Arduino that is doing the disconnecting - because of a shortage of power.
Nick_Pyner:
Probably right there. Nobody else uses a hardware serial library, so why would you?
Well, more like everyone uses HardwareSerial.h, which is part of the Arduino core library. We just don't bother to add an #include directive to our sketches for it because that is already done by Arduino.h, for which the Arduino IDE automatically adds an #include directive to every sketch.
Nick_Pyner:
Probably right there. Nobody else uses a hardware serial library, so why would you?
Thanks for answering! i am not using that library in anyway, i originally thought it was a library for using the first pins as serial, i was wrong , as i didn't clearly understand how they work, but i now do.
Nick_Pyner:
You keep talking about the phone disconnecting, but you might speculate that it is the Arduino that is doing the disconnecting - because of a shortage of power.
The app is almost certainly not your problem.
|I do not know which side is disconnecting,
pert:
How are you powering your servo?
I am using a DC power supply to power both the Bluetooth module (HC-05 zs040) and the Servo(SG90) giving exactly 5 Volts , and limiting amps at 1(I am far away from reaching that anyway).
I think its also worth mentioning that i used a voltage devider as people suggested, using a 4k resistor and a 2k one, but im guessing because my problem only happens when i try to use a servo that it is an arduino problem.
scorpionma:
Thanks for answering! i am not using that library in anyway, i originally thought it was a library for using the first pins as serial, i was wrong , as i didn't clearly understand how they work, but i now do.
Read reply#6, which explains better than I do.
I do not know which side is disconnecting,
I am using a DC power supply to power both the Bluetooth module (HC-05 zs040) and the Servo(SG90) giving exactly 5 Volts , and limiting amps at 1(I am far away from reaching that anyway).
I think its also worth mentioning that i used a voltage devider as people suggested, using a 4k resistor and a 2k one, but im guessing because my problem only happens when i try to use a servo that it is an arduino problem.
It is far more likely to be Arduino than Android. I hope the 5v is correctly applied to 5v, and not through the barrel jack. Assuming the voltage divider is that on Arduino Tx, it is not relevant to the problem.
Limited at 1A wil barely run what you have connected.
I'd expct the servo to use all that and more if it hits a hard limit.
Try bumping the current limit up to 2A or more
"because my problem only happens when i try to use a servo that it is an arduino problem. "
Well, that is easy to check. The arduino has no clue if a servo is connected, so just disconnect the servo, run your code, and see if the issue still exist. If the problem goes away, then you need a seperate power supply for rhe servo.
lastchancename:
Limited at 1A wil barely run what you have connected.
I'd expct the servo to use all that and more if it hits a hard limit.
Try bumping the current limit up to 2A or more
I am using a DC power Supply which shows how much current im drawing, im no where near 0.5A, im only using a bluetooth module and a small servo(sg90), I would easily know if i am above the max current.
Thanks!
zoomkat:
"because my problem only happens when i try to use a servo that it is an arduino problem. "
Well, that is easy to check. The arduino has no clue if a servo is connected, so just disconnect the servo, run your code, and see if the issue still exist. If the problem goes away, then you need a seperate power supply for rhe servo.
That's a great Idea, how have i not thought of that, i will report back with what i got.
Thanks a lot!