integers recieved from 2 paired HC05 modules

hi all im new to the arduino forums and i need some help :slight_smile:
i have 2 hc05 modules and ive paired them one as a master one as a slave and linked the addresses im receiving on the master side integer numbers in the Serial window these numbers represent an analog input controlled by potentiometer from the slave arduino
check attachment if needed:).
so im sending the analog numbers wirelessly to the master which then displays those numbers.

great so far

but my problem is what i want to do is control pwm on the master with what i do on the slave as i turn the potentiometer on the slave i want led to increase and decrease in brightness wirelessly
on the master.

eventually il be controlling a servo over bluetooth or thats the aim anyway

any ideas? or advice could be greatly appreciated many thanks.

here is my code ;

master arduino >>> my problem :stuck_out_tongue:
#include <SoftwareSerial.h>// import the serial library

SoftwareSerial Genotronex(7, 8); // RX, TX
int ledpin=13;
int BluetoothData;
int bytee;

void setup() {
Serial.begin(9600);

Genotronex.begin(9600);
pinMode(ledpin,OUTPUT);
pinMode(12,OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
if (Genotronex.available()>0){
bytee=Genotronex.read();
Serial.write(bytee);
}
i got this far and couldnt think of what to do
}

my code for the slave :

#include <SoftwareSerial.h>// import the serial library

SoftwareSerial Genotronex(7, 8); // RX, TX

int BluetoothData;

void setup() {

Genotronex.begin(9600);
pinMode(13,OUTPUT);
pinMode(A0,INPUT);
}

void loop() {
int var=analogRead(A0);
int vare=map(var,0,1023,0,255);
Genotronex.println(vare);
analogWrite(13,vare);
delay(1000);
}

many thanks to anyone who can help if you need more info please let know and hope someone can help :slight_smile:

datatataa.jpg

It's generally not a good idea to use the smiley face pin for software serial. Use that pin for some other purpose. Or, post your code correctly. You DID read the how to guide at the top of the forum, didn't you?

i got this far and couldnt think of what to do

You've read one character. What do you think you are going to do with one character?

You need to store the characters in an array, until the end-of-packet marker arrives. That would be a carriage return or line feed (10 and 13; ignore the other one). When that happens, atoi() seems like a useful function.

i actually didnt read it haha i just figured i could post and didnt realize the softare serial also had a code for a smiley face on here anyays thankyou for the reply

and how would i go about storing the characters in to an array

many thanks friend :slight_smile:

and how would i go about storing the characters in to an array

This code assumes a start and end marker that are not carriage return and line feed. You could change it so that the cr or lf was the end end of packet marker, or you could change the sender to send '<', the value, and '>'.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

ok thats great i can see what you did there but may i ask how would i get that to control the pwm on the master or how would i read the characters to the analog pin again many thanks for your help im in the process of learning this part of arduino and c++

To control PWM on Arduino B based on a potentiometer setting on Arduino A all you need to do is send the value that should be used in analogWrite().

...R

Ok then for example would this work

#include <SoftwareSerial.h>// import the serial library

SoftwareSerial Genotronex(7, ; // RX, TX
int ledpin=13;
int BluetoothData;
int bytee;

void setup() {
Serial.begin(9600);

Genotronex.begin(9600);
pinMode(ledpin,OUTPUT);
pinMode(12,OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
if (Genotronex.available()>0){
bytee=Genotronex.read();
Serial.write(bytee);
If (Serial.available()>0){
Int bluedata = Serial.read();

analogWrite(13,bluedata);
}

Ok then for example would this work

No. Are you sending binary data to the Serial port? From what application?

Im sending analog values from arduino bluetooth slave to master in the hopes of wirelessly controlling a pwm pin to dim or brighten an led via potentiometer on the slave so i think im sending raw data but it is seen as integers on the serial monitor of the master so basically its just printing what it sees from the slave bluetooth

So i beleive it to be binary data :slight_smile:

Sorry ascii data is being received

Suppose the sending device has calculated that the correct value for pwm would be 135 (i.e analogWrite(pin, 135) and suppose that number 135 is in a byte variable called pwmVal

Then it needs to send that number using Serial.write(pwmVal); - obviously you need to replace Serial with your bluetooth device name). Serial.write() sends binary values whereas Serial.print() converts numbers to their ascii equivalent.

And the receiving device will do something like

byte newPwmVal = Serial.read();
analogWrite(pin, newPwmVal);

...R

Robin's suggestion is good, since what you want to send and receive is byte-sized data. If you ever exceed that requirement, though, you should learn how to deal with sending the data as ASCII data and converting it back to ints, floats, etc.

On the sender:

Serial.print('<');
Serial.print(pwmVal);
Serial.print('>');

On the receiver:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says "Process the packet", you can use strtok(), atoi(), atof(), etc., depending on what you send in the packet. In your case:

int inPwmVal = atoi(inData);
digitalWrite(pin, inPwmVal);

is all that is needed.