SEND RECEIVE MULTIPLE DATA

Hi everyone, I have two Arduino and two HC-05 Bluetooth modules and I can send/receive data between them. I have the master and the slave defined.

However, I am having difficulty separating data because I want to send values that are between 0 and 180 for five different motors since these values between 0 and 180 will be different for each of the motors.

I can do this with a single engine because there is no problem in the data processing, someone knows how I can handle this data without controlling the wrong motor with the wrong value.

In addition to doing this process in "real time" my goal would be to be watching the movement and when I click on a button to save those values in an array that I could already do, but again I wanted to leave the "real-time" mode where I was and send the positions to the 5 guardrails on the other side in an array and act on the engines with the values stored in the array.

Thanks in advance, I hope it was clear.

David_Pires:
However, I am having difficulty separating data because I want to send values that are between 0 and 180 for five different motors since these values between 0 and 180 will be different for each of the motors.

you can send two bytes of a data

  1. motor 1 to 5
  2. the value 0 to 180
    you would probably require another byte to indicate staart of data frame

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

How could I make the bytes tactic?

//all this is just for the "Real-time" situation

//the hc-05 that sends

Serial.write(map(potentiometer1,0,1024,0,180));
Serial.write(map(potentiometer2,0,1024,0,180));
Serial.write(map(potentiometer3,0,1024,0,180));
Serial.write(map(potentiometer4,0,1024,0,180));
Serial.write(map(potentiometer5,0,1024,0,180));

//the hc-05 that receive

if (Serial.available() == 5)
{
  motorA=Serial.read();
  motorB=Serial.read();
  motorC=Serial.read();
  motorD=Serial.read();
  motorE=Serial.read();

}

Is this right?

David_Pires:
How could I make the bytes tactic?

What does "tactic" mean? Maybe it is a typo but I don't know what you intended.

...R

technic

David_Pires:
technic

Sorry, I still don't know what you mean by How could I make the bytes technic?

Maybe you can give an example of a technic and a non-technic byte?

...R

My question is how can I have a byte for motor and one byte for the value for which one.
And if my code below makes this or not.

//all this is just for the "Real-time" situation

//the HC-05 that send

Serial.write(map(potentiometer1,0,1024,0,180));
Serial.write(map(potentiometer2,0,1024,0,180));
Serial.write(map(potentiometer3,0,1024,0,180));
Serial.write(map(potentiometer4,0,1024,0,180));
Serial.write(map(potentiometer5,0,1024,0,180));

//the hc-05 that receive

if (Serial.available() == 5)
{
  motorA=Serial.read();
  motorB=Serial.read();
  motorC=Serial.read();
  motorD=Serial.read();
  motorE=Serial.read();

}

David_Pires:
My question is how can I have a byte for motor and one byte for the value for which one.

Do you mean that you want to send something like “<A,134,R>” meaning set the PWM value for motor A to 134 moving in Reverse?

If so, most of what you need is in the link I gave you in Reply #2 - have you studied it?

…R

A,134 I want exactly this, but I think I don´t need the reverse because I want to control a servo motor by angle.

I studied the link that you gave me but I don´t understand at all and I don´t know how to implement it in my future code, I´m would like to have something easier to do and understand.

Post the program that you have that gathers the data that needs to be transmitted - it does not matter if it does not (at this stage) actually send the data.

Also post the program that will use the data when it receives it.

...R

I don´t have test the codes.

bellow the sending code

#define buttonPin 8

int pA1 = A0;
int pA2 = A1;
int pA3 = A2;
int pA4 = A3;
int pA5 = A4;

int bm = 0;

int buttonPinState = 0;
int i = 0;

const unsigned int n = 2;

unsigned int flep1[n];
unsigned int flep2[n];
unsigned int flep3[n];
unsigned int flep4[n];
unsigned int flep5[n];

unsigned int angle1[n];
unsigned int angle2[n];
unsigned int angle3[n];
unsigned int angle4[n];
unsigned int angle5[n];

void setup() 
{
  
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  
}

void loop() 
{

  while (i < n)
  {
    Serial.write('r');

    int pote = analogRead(pA1);
    int mapote = (map(pote, 0, 1024, 0, 180));
    Serial.write(mapote);

    /*

    int flex1 = analogRead(pA2);
    int mapf1 = (map(flex1, 0, 1024, 0, 180));
    Serial.write(mapf1);

    int flex2 = analogRead(pA3);
    int mapf2 = (map(flex2, 0, 1024, 0, 180));
    Serial.write(mapf2);

    int flex3 = analogRead(pA4);
    int mapf3 = (map(flex3, 0, 1024, 0, 180));
    Serial.write(mapf3);

    int flex4 = analogRead(pA5);
    int mapf4 = (map(flex4, 0, 1024, 0, 180));
    Serial.write(mapf4);
    
    */

    
    buttonPinState = digitalRead(buttonPin);
    
    if (buttonPinState == HIGH)
    {
      
      bm = 1;
      
    }
    
    if ((buttonPinState == LOW) && (bm == 1))
    {
      
      flep1[i] = analogRead(pA1);

      /*

      flep2[i] = analogRead(pA2);

      flep3[i] = analogRead(pA3);

      flep4[i] = analogRead(pA4);

      flep5[i] = analogRead(pA5);

      */

      i++;

      bm = 2;
      
    }
  }
  
  if ((i == 2) && (bm == 2))
  {
    
    for (int pr = 0; pr < n; pr++)
    {
      
      angle1[pr] = map(flep1[pr], 0, 1024, 0, 180);

      /*
      
      angle2[pr] = map(flep2[pr], 0, 1024, 0, 180);

      angle3[pr] = map(flep3[pr], 0, 1024, 0, 180);

      angle4[pr] = map(flep4[pr], 0, 1024, 0, 180);

      angle5[pr] = map(flep5[pr], 0, 1024, 0, 180);

      */

      bm = 3;
      
    }
  }
  
  buttonPinState = digitalRead(buttonPin);
  
  if ((buttonPinState == HIGH) && (bm == 3) || (buttonPinState == HIGH) && (bm == 5))
  {
    
    bm = 4;
    
  }
  
  if ((buttonPinState == LOW) && (bm == 4))
  {
    
    for (int c = 0; c < n; c++)
    {
      
      Serial.write('m');

      Serial.write(angle1[c]);

      /*

      Serial.write(angle2[c]);

      Serial.write(angle3[c]);

      Serial.write(angle4[c]);

      Serial.write(angle5[c]);

       */

      delay(500);

      bm = 5;
      
    }
  }
}

bellow the receiving code

#include <Servo.h>
Servo myservo;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int memomode = 0;
int i = 0;
int state = 0;
const unsigned int n = 2;

unsigned int angle1[n];
unsigned int angle2[n];
unsigned int angle3[n];
unsigned int angle4[n];
unsigned int angle5[n];

void setup()
{
  myservo.attach(3);
  myservo1.attach(4);
  myservo2.attach(5);
  myservo3.attach(6);
  myservo4.attach(7);
  myservo5.attach(8);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) { 
    state = Serial.read(); 
  }
  
  if ((state == 'r') || (memomode = 1))
  {
    memomode = 1;
    
    if (Serial.available() == 5)
    {
      myservo.write(Serial.read());
      myservo1.write(Serial.read());
      myservo2.write(Serial.read());
      myservo3.write(Serial.read());
      myservo4.write(Serial.read());
      myservo5.write(Serial.read());
      memomode = 0;

    }
  }
  if ((state == 'm') || (memomode = 2))
  {
    memomode = 2;
    if (Serial.available() == 5)
    {
      angle1[i] = Serial.read();
      angle2[i] = Serial.read();
      angle3[i] = Serial.read();
      angle4[i] = Serial.read();
      angle5[i] = Serial.read();
      i++;
      memomode = 0;

    }
  }
}

Please post complete programs so I can compile them.

...R

I´ve modified the #11 reply and complete the code.

David_Pires:
I´ve modified the #11 reply and complete the code.

I suspect you have not tried to compile your sending code.

Have another go :slight_smile:

...R

oh no, I put the code that was after the loop inside the void loop god !!! :disappointed_relieved:

I hope this time is ok I have compiled the codes on my laptop and they have successfully compiled.

Reply #11

You have a lot of stuff in your sending program that is commented out - I assume I can delete it all for clarity?

In your sending program you have this

  while (i < n)

and there seems to be no code to update the variable n so the WHILE will never end.

Because you use a single character variable name n I can’t be certain because if I try to highlight every instance of the variable name every n in the program gets highlighted. Don’t use single character variable names - use meaningful names.

I can’t tell from your program which variables hold the data that you want to send.

Also, in your program you are printing data inside the WHILE and at the bottom of the program. Are those all supposed to be data that you want to send? It will be much easier just to send the data in one place.

I won’t look at the receiving program until we sort out the sending program.

…R

So I will explain…

indeed the while function ends. Because of the condition, i<n. I am only using n to define the number of positions of the array and limiting the while.

If you go see inside the while when I press the button I read the position of the potentiometer and save this position to the first position of the array and I do an increment on i variable (i++);

So when I press the second time the button the same process appends but for the last time because i variable now is i=2 and the condition of while was while(i<n). If you see n is defined as 2. So we are not inside the while anymore and we are now in the second part of the code the part of memory mode, so in this time I have already the values from potentiometer saves in arrays so there I converted the values to 0 to 180 degrees, and then send this values.

I clear some of the comments and left some of them because I was just testing for one servo motor. But I have made the code for the 5.

I hope it is helpful, by the way, you are awesome, always here to help. I just want to say thanks just for the attention.

I understand about the WHILE - I should have checked both variables, but I got (get) very confused by meaningless single character variable names. Presumably n represents something that could be expressed in a meaningful name? And the variable i also?

You did not deal with the other (more important) points in my Reply #17

...R