Bluetooth Servo w/LED's. issues...

Thread Starter

jgreene44

Joined Dec 8, 2016
108
Hello all and thanks in advance.
If someone could help me with this I would be ecstatic. I just cant see what is wrong with my setup. As the title states. bluetooth servo... most of you are familiar with this and know how to set this up and maybe like me can make the apps thanks too MIT. Well I am trying to get servo's and control of a few output pins for simple stuff like led on/off. well, Ardudroid seems close. however if my limited knowledge serves me thats analog not pwm digital like i would need for a servo. Just pwm buttons, no slider and, would fry a servo.(pretty sure I have done this once already). I found his code too confusing to try to adapt to my needs. Found a few multi-servo apps w/code. None with additional options. I keep trying to merge a multi-servo bluetooth sketch with a simple bluetooth blink led sketch. I know its going to be something in the code. We can go over all my other setup options. readings and whatever else but, I know its the code. Im smart enough to know that. Just too stupid to fix it. I have been trying believe me. I just simply cannot find it!

heres the code:

#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo1, myservo2, myservo3, myservo4, myservo5, myservo6;

int bluetoothTx = 7;
int bluetoothRx = 8;
char junk;
String inputString="";

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{ myservo1.attach(9);
myservo2.attach(10);
myservo3.attach(11);
myservo4.attach(3);
myservo5.attach(5);
myservo6.attach(6);
Serial.begin(9600);
pinMode(13, OUTPUT);
bluetooth.begin(9600); }

void loop()
{ if(bluetooth.available()>= 2 )
{ unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);

if (realservo >= 1000 && realservo <1180)
{ int servo1 = realservo;
servo1 = map(servo1, 1000,1180,0,180);
myservo1.write(servo1);
Serial.println("servo 1 ON");
delay(10); }

if (realservo >=2000 && realservo <2180)
{ int servo2 = realservo;
servo2 = map(servo2,2000,2180,0,180);
myservo2.write(servo2);
Serial.println("servo 2 On");
delay(10); }

if (realservo >=3000 && realservo < 3180)
{ int servo3 = realservo;
servo3 = map(servo3, 3000, 3180,0,180);
myservo3.write(servo3);
Serial.println("servo 3 On");
delay(10); }

if (realservo >=4000 && realservo < 4180)
{ int servo4 = realservo;
servo4 = map(servo4, 4000, 4180,0,180);
myservo4.write(servo4);
Serial.println("servo 4 On");
delay(10); }

if (realservo >=5000 && realservo < 5180)
{ int servo5 = realservo;
servo5 = map(servo5, 5000, 5180,0,180);
myservo5.write(servo5);
Serial.println("servo 5 On");
delay(10); }

if (realservo >=6000 && realservo < 6180)
{ int servo6 = realservo;
servo6 = map(servo6, 6000, 6180,0,180);
myservo6.write(servo6);
Serial.println("servo 6 On");
delay(10); }


if(Serial.available())
{ while(Serial.available())
{ char inChar = (char)Serial.read();
inputString += inChar; }

Serial.println(inputString);
while (Serial.available() > 0)
{ junk = Serial.read() ; }
if(inputString == "1")
{digitalWrite(13, HIGH); }
else if(inputString == "0")
{digitalWrite(13, LOW); }
inputString = "";

}
}
}

sure hope someone gets it... thanks.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,237
I'm not familiar with the Bluetooth library. But how are you sending the value for the servo command.

If you send "1000" in characters, that is four bytes. You read two as a binary number. I'd insert debugging code to check the calculated value.

Secondly, I don't see any code to connect to the other device. In the examples I have found, initialization includes searching for a Bluetooth device before connecting.
 

Thread Starter

jgreene44

Joined Dec 8, 2016
108
the value is set on an app on my android but, im not sure that is what you are asking. not sure how to debug. and idk am i the only one that those questions do not make since too? ya it says bluetooth.begin(9600);? right there? surely you seen that. so am i missing something? serious question. no disrespect intended.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
the value is set on an app on my android but, im not sure that is what you are asking. not sure how to debug. and idk am i the only one that those questions do not make since too? ya it says bluetooth.begin(9600);? right there? surely you seen that. so am i missing something? serious question. no disrespect intended.

Please leave the text talk for texting. Post in full complete sentences. You will be more understandable.

Post you code with code tags. It will be easier to read.

You never mention what chip you are using nor the compiler. Those are important details.


The best way to approach a problem like this is to break it down into small pieces then build. For example, write a program that does nothing but exchange information via bluetooth. Leave out the LEDs the servos everything else. Make sure the communication works.

When right a small program that does nothing but control your servos. No bluetooth. No LEDs.

The same for the LEDS.

Your test programs should be written by using reusable functions that you can use in you real program. Functions like initializeBluetooth, initializeLEDs, setLED, set servo etc.

Once you have all the pieces working put them all together in your real program.


Lastly, learn to use the debugger.
 

spinnaker

Joined Oct 29, 2009
7,830
? ya it says bluetooth.begin(9600);? right there? surely you seen that. so am i missing something? serious question. no disrespect intended.
djsfantasi already mention he is not familiar with the library? How would djsfantasi know Begin is the same as open? Besides as mentioned, no code tags makes your code hard to read. So yeah you are being disrespectful. Why not simply say the bluetooth.begin(9600) is opening the port instead of a snide remark? People are trying to help you on their own time. The least you can do is be grateful.
 

Thread Starter

jgreene44

Joined Dec 8, 2016
108
I guess spinnaker. I stated clearly I must be missing something. Not every question can sound less then insulting. That's why I took the extra time to state that it was not meant as such and, I'm sorry. Do you really need to know the library to understand bluetooth.begin(9600)? really? Can someone please leave the drama out and just help a man out. I really could use help despite how I may or may not come across. arduino uno/arduino ide.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
I guess spinnaker. I stated clearly I must be missing something. Not every question can sound less then insulting. That's why I took the extra time to state that it was not meant as such and, I'm sorry. Do you really need to know the library to understand bluetooth.begin(9600)? really? So now I'm thinking at least I was kind enough not to assume your both too ignorant to read. A precaution I'm now fairly certain I did not need to take! Can someone please leave the drama out and just help a man out. I really could use help despite how I may or may not come across.

You are the one injecting drama and continuing to be rude. If you want people to help you then start by not insulting them.
 

djsfantasi

Joined Apr 11, 2010
9,237
Here is an article on this site that discusses connecting to a Bluetooth device with an Arduino.

http://www.allaboutcircuits.com/projects/control-an-arduino-using-your-phone/

Note that there are several steps in establishing a connection. They include:
  • Connecting to the device
  • Checking if it's available
  • Pairing with the device
  • Creating a socket
All this, according to the article, has to be done before reading data. I read the article and even though I haven't used the library, wondered where in your code you had done that.
 

spinnaker

Joined Oct 29, 2009
7,830
I guess spinnaker. I stated clearly I must be missing something. Not every question can sound less then insulting. That's why I took the extra time to state that it was not meant as such and, I'm sorry. Do you really need to know the library to understand bluetooth.begin(9600)? really? So now I'm thinking at least I was kind enough not to assume he is too ignorant to read. A precaution I'm not certain I needed to take! Can someone please leave the drama out and just help a man out. I really could use help despite how I may or may not come across.

And I have already offered some advice of how to break your problem down into small pieces. Doubtful anyone is going to sit there and analyze all of your code (unless it is something blatantly obvious). You need to narrow your problem down to a specific area. This is basic troubleshooting technique.
 

Thread Starter

jgreene44

Joined Dec 8, 2016
108
spinnaker I have been trying everything! I tried it with both codes isolated first. then tried injecting the second into the first in every way imaginable. The servo code remains working no matter. the led code will not seem to go in. I can see the rx light up when i push the button. I know I am sending text of "1" to the arduiono. i know the code says "1" is on. really, I didn't just jump on here and ask for help I have been pulling my hair out with this... for some reason when it gets to the last part of code. the part beyond that of the servo code. There in the loop. Its just not doing what its supposed to. Could be any number of noob things I have done wrong.


Edit: This has allowed me to turn on/off the led on pin 13 with the use of the slider.
if (realservo >=6000 && realservo < 6180)
{int servo6 = 13;
digitalWrite(13, HIGH);
delay(1000); }
if (realservo >=5000 && realservo < 5180)
{int servo5 = 13;
digitalWrite(13, HIGH);
delay(1000); }
Now can I figure it out with a button... to be seen.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
Here is an article on this site that discusses connecting to a Bluetooth device with an Arduino.

http://www.allaboutcircuits.com/projects/control-an-arduino-using-your-phone/

Note that there are several steps in establishing a connection. They include:
  • Connecting to the device
  • Checking if it's available
  • Pairing with the device
  • Creating a socket
All this, according to the article, has to be done before reading data. I read the article and even though I haven't used the library, wondered where in your code you had done that.

Ah the HC-05. Finally get some information. I have only used the HC-05 with the Pic but if the mcu is a slave it is paired by the host software. The only thing the slave mcu needs to do is to open a serial port. I am not familiar with Arduino but on the Pic no special library is needed for the HC-05 if it is acting as a slave. The only thing that is needed is the library to open, write to and read from the serial port. I don't see why it would be different under the Arduino. As a slave the HC-05 is very straight forward and easy to use.
 

spinnaker

Joined Oct 29, 2009
7,830
spinnaker I have been trying everything! I tried it with both codes isolated first. then tried injecting the second into the first in every way imaginable. The servo code remains working no matter. the led code will not seem to go in. I can see the rx light up when i push the button. I know I am sending text of "1" to the arduiono. i know the code says "1" is on. really, I didn't just jump on here and ask for help I have been pulling my hair out with this... for some reason when it gets to the last part of code. the part beyond that of the servo code. There in the loop. Its just not doing what its supposed to. Could be any number of noob things I have done wrong.

Is the Arduino acting as a master or a slave?
 

Thread Starter

jgreene44

Joined Dec 8, 2016
108
cell phone > hc-05 ---> arduino uno and ya we don't do it with a laptop the advantages of the MIT app maker is too huge. even people doing it with a pc install an android emulator.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
spinnaker I have been trying everything! I tried it with both codes isolated first. then tried injecting the second into the first in every way imaginable. The servo code remains working no matter. the led code will not seem to go in. I can see the rx light up when i push the button. I know I am sending text of "1" to the arduiono. i know the code says "1" is on. really, I didn't just jump on here and ask for help I have been pulling my hair out with this... for some reason when it gets to the last part of code. the part beyond that of the servo code. There in the loop. Its just not doing what its supposed to. Could be any number of noob things I have done wrong.


Edit: This has allowed me to turn on/off the led on pin 13 with the use of the slider.
if (realservo >=6000 && realservo < 6180)
{int servo6 = 13;
digitalWrite(13, HIGH);
delay(1000); }
if (realservo >=5000 && realservo < 5180)
{int servo5 = 13;
digitalWrite(13, HIGH);
delay(1000); }
Now can I figure it out with a button... to be seen.
So it sounds like you know the servo code is working.

Are you certain you are communicating via Bluetooth? If so how did you determine that?
 

spinnaker

Joined Oct 29, 2009
7,830
Well the hc-05 is the slave. The master would be any android. thanks again.
Doubtful you need the Bluetooth library then. The HC-05 should act just like any other serial port to your mcu. Same as if you would have connected a Max232 and connected straight to your PCs serial port. Again I am not familiar with Arduino but I would bet all you need to do is open the serial peripheral. It is that simple on the pic.
 

Thread Starter

jgreene44

Joined Dec 8, 2016
108
Yes I know my basic thinking is fair. However a syntax error is real likely or something like that. You should get an arduino. You'll love it.
 

spinnaker

Joined Oct 29, 2009
7,830

Thread Starter

jgreene44

Joined Dec 8, 2016
108
Not sure how to make that use full in finding an error in my code. I know the arduino ide finds nothing. Do you think it is something other then code? Im open to suggestions thats for sure. Oh and I know yours is still in the package so I should tell you the serial monitor is part of the arduino ide. It seems to have any functionality I need this far.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
You need to first test if your bluetooth connection is indeed working. Sending data from your tablet to the Arduino then ecohing it to the terminal will help you to determine if the bluetooh connection is working. Strategically placed messages in your code to the terminal will also help you track down the source of the error. The debugger is also a useful tool. The purpose of both of these tools is to help you track down the issue. So yes they are VERY useful when you know how to use them.

You have the code and the hardware right in front of you. Far easier for you to narrow your issue down to a few lines where others might be able to concentrate and try to help you figure out what is wrong.
 
Top