Need Help with my H...
 
Notifications
Clear all

Need Help with my HC 05 Bluetooth Pair (Master and slave)  

  RSS

Gulshan
(@gulshan)
Estimable Member
Joined: 1 year ago
Posts: 127
2020-10-24 2:53 pm  

Hello, Recently I have been trying to make a 4 door mechanism which consists of 4 servo motors that are operated by potentiometer using arduino.

but Now I want to make it wireless using my HC 05 master/slave bluetooth pair. I wanted to keep the connection binded and secured to only one master module so i didn't used phone bluetooth for it but i dont know how to control servos using two HC 05 bluetooth modules between two arduino by potentiometer. Can anyone help me with it.

is it just like the normal code ?


Quote
Gulshan
(@gulshan)
Estimable Member
Joined: 1 year ago
Posts: 127
2020-10-28 4:46 pm  

Can anyone help me out 


ReplyQuote
Spyder
(@spyder)
Prominent Member
Joined: 1 year ago
Posts: 849
2020-10-28 10:50 pm  

I'm not sure I'm understanding your question, but, I do know that connecting a phone to 2 BT devices is possible, but tricky

Why do you want to use 2 arduinos ?

Is this for 2 separate locations ?


ReplyQuote
Gulshan
(@gulshan)
Estimable Member
Joined: 1 year ago
Posts: 127
2020-10-29 7:32 am  

@spyderYes it is possible to control two bt devices with android phone but i don't want to use android phone for it.I want to make it Permanent. i just want to keep it as single master with buttons and pot as transmitter communicating with slave that has led and a few servos as reciever.

 

I made the code to control leds using button on the uno which works as a toggle switch and turns led on off through BT devices but I am unable to control the servo with pot. Here below the code that i made to control led using bt devices wirelessly.

Transmitter

 

#define button 2
int value = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  int val = digitalRead(button);
  if(val == 0 && value == 0)
  {
    Serial.println("A");
    value = 1;
    delay(200);
  }
  else if(val == 0 && value == 1)
  {
    Serial.println("a");
    value = 0;
    delay(200);
  }
  delay(200);
}

Receiver


#define led 2 void setup() {  // put your setup code here, to run once:  Serial.begin(9600);  pinMode(led, OUTPUT); } void loop() {  // put your main code here, to run repeatedly:  if(Serial.available())  {    char val = Serial.read();    if(val == 'A')    {      digitalWrite(led, HIGH);    }    else if(val == 'a')    {      digitalWrite(led, LOW);    }  } }

ReplyQuote
frogandtoad
(@frogandtoad)
Honorable Member
Joined: 1 year ago
Posts: 540
2020-10-31 9:07 am  

@gulshan

Your transmitter code appears to suffer from the same issues as a recent post to which I have replied to... as for your receiver code, there is nothing wrong with it, but I'd like to suggest an alternative if it doesn't hinder your design goals:

For example, from this:

    char val = Serial.read();
    if(val == 'A')
    {
      digitalWrite(led, HIGH);
    }
    else if(val == 'a')
    {
      digitalWrite(led, LOW);
    }

To this:

   String ledState = Serial.readStringUntil('\n');
   
   if(ledState.equals("on")) {
      digitalWrite(led, HIGH);
    }
   else if(ledState.equals("off")) {
      digitalWrite(led, LOW);
    }

The reason I am providing this alternative, is because to often, code can become very unreadable, not only to oneself, but to others who try to interpret it.  I think it's much better to state the intent of the code, rather than taking shortcuts with single letter acronyms for variables, etc... programming languages are written to accommodate readable code, and that means you can in the end, include less comments too.

Given that this example is to turn an led on or off, doesn't it make sense to actually write the program code to reflect the intent of the actions desired?

It does not take anymore time to write the example I provided over your original.  If this code was to be used in MQTT for a light switch, wouldn't it be better to read "on" or "off" vs "A" or "a"?

I know which on I would choose every time... just some food for thought 🙂

As for your Bluetooth question, you haven't provided any code for that at all, so like the big @spyder, I'm a little confused too.  I have never worked with Bluetooth, so not sure I can really help with that, but unless I see some code and any associated error messages I won't know for sure, nor will anyone else.

Have you tried any of the library examples to start with, and if so, what were the errors?

Cheers.


ReplyQuote
byron
(@byron)
Reputable Member
Joined: 1 year ago
Posts: 376
2020-10-31 4:34 pm  

@gulshan

if I understand correctly you have managed to transmit and receive a value such as 'A' or 'a' between your arduino's but cannot see your way to using a potentiometer on one arduino to control a motor on an other arduino.

A potentiometer should be connected to an AD pin and can be made to read values from 0 to 1023 (assuming you use one of the Arduinos AD pins) as you increase the voltage through the potentiometer.  You can output this number to the serial monitor as well as using to control your servo motor.  (no doubt you have seen the excellent dronebot video on this).  Likewise you can transmit this number just as you did when you sent the 'A' or 'a' values.   Then at the receiving arduino end you take this number and use it to  control the attached motor.

As you have not submitted any code in relation to this, its not clear a what point along this chain you might be stuck at.

Posted by: @gulshan

is it just like the normal code

yes, nothing special, and what with your transmit and receive code and the code in the dronebot article you are almost there. 😀 


ReplyQuote
Gulshan
(@gulshan)
Estimable Member
Joined: 1 year ago
Posts: 127
2020-11-03 2:32 pm  

@frogandtoad Thank you again, i am gonna try changing it again tomorrow.

Sorry for the late reply actually i was quarantined so all i could do is read your reply but couldn't reply back to you. out of all the tutorial videos that i have seen about this topic some people simply used the potentiometer knob sketch and split it into a transmitter and a reciever sketch. just as one shown below:

TRANSMITTER

 

#define ledPin 9

int state = 0;
int potValue = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600); // Default communication rate of the Bluetooth module
}

void loop() {
 if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
 }
 // Controlling the LED
 if (state == '1') {
  digitalWrite(ledPin, HIGH); // LED ON
  state = 0;
 }
 else if (state == '0') {
  digitalWrite(ledPin, LOW); // LED ON
  state = 0;
 }
 // Reading the potentiometer
 potValue = analogRead(A0);
 int potValueMapped = map(potValue, 0, 1023, 0, 255);
 Serial.write(potValueMapped); // Sends potValue to servo motor
 delay(10);
}
In this code at transmitter has a potentiometer is connected at pin A0 and a led connected to
pin 9 which can be controlled by the button on the slave end as given below:





RECEIVER
#include <Servo.h> #define button 8 Servo myServo; int state = 20; int buttonState = 0; void setup() {  pinMode(button, INPUT);  myServo.attach(9);  Serial.begin(9600); // Default communication rate of the Bluetooth module } void loop() { if(Serial.available() > 0){ // Checks whether data is comming from the serial port    state = Serial.read(); // Reads the data from the serial port } // Controlling the servo motor myServo.write(state); delay(10); // Reading the button buttonState = digitalRead(button); if (buttonState == HIGH) {   Serial.write('1'); // Sends '1' to the master to turn on LED } else {   Serial.write('0'); }   }
Here a button is connected to pin 8 which controls led of the master module and
servo motor {our main topic of discussion} connected to pin 9.

Here is my problem the led is blinking as it should be but in my setup but i have a joystick
module at the transmitter end which controls the servo motor of the receiver end and it is not
working, the servo motor are not even responding to it and i can't understand why. Cause i thought potentiometer and joystick module were
suppose to be same thing
This post was modified 1 month ago 2 times by Gulshan

ReplyQuote
frogandtoad
(@frogandtoad)
Honorable Member
Joined: 1 year ago
Posts: 540
2020-11-04 7:15 am  

@gulshan

Posted by: @gulshan

@frogandtoad Thank you again, i am gonna try changing it again tomorrow.

Sorry for the late reply actually i was quarantined so all i could do is read your reply but couldn't reply back to you.

No problem... you have no idea what kind of totalitarian dictatorship I'm going through here in Aus!

Go Trump!

Anyway, back to the fun stuff!

Posted by: @gulshan

Here is my problem the led is blinking as it should be but in my setup but i have a joystick
module at the transmitter end which controls the servo motor of the receiver end and it is not
working, the servo motor are not even responding to it and i can't understand why. Cause i thought potentiometer and joystick module were
suppose to be same thing

Ok, assuming they are connected via serial, I think I can see your problem.

Serial.write(...) has overloaded versions of it so it can send a single byte, or a string etc... you are using the byte overload of write() to send an integer of 2 bytes, and on the receiving end, read() is also looking to read one character at a time.

The easiest way to solve this problem, is to write a string or array of bytes from the transmitter end, and then read it back using on the receiver end using Serial.parseInt(), or if you have a need to stuff it in a string for some reason, Serial.readLine() or Serial.readLineUntil('\n').

Cheers.


ReplyQuote
byron
(@byron)
Reputable Member
Joined: 1 year ago
Posts: 376
2020-11-04 12:36 pm  
Posted by: @gulshan

i thought potentiometer and joystick module were
suppose to be same thing

My joystick has 2 potentiometers for the x and y axis and additionally has an addition switch connection.  (so 3 signal wires).  The joystick potentiometer will be centralised so the values can go up or down and the analogue signals are usually mapped something like

  mapX = map(xPosition, 0, 1023, -512, 512);
  mapY = map(yPosition, 0, 1023, -512, 512);

So your 'potentiometer' mapping looks wrong to me if using a joystick.  Have you checked you are on the right track by getting  the joystick/motor working with just one Arduino to check out the correct wiring etc.?


ReplyQuote