Go Down

Topic: SEND RECEIVE MULTIPLE DATA (Read 355 times) previous topic - next topic

David_Pires

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.

horace

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

Robin2

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
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

David_Pires

#3
May 23, 2018, 06:09 pm Last Edit: May 23, 2018, 06:11 pm by David_Pires Reason: complete
How could I make the bytes tactic?

Code: [Select]


//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?

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.


Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

David_Pires

#7
May 24, 2018, 01:36 pm Last Edit: May 24, 2018, 01:38 pm by David_Pires
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.
Code: [Select]


//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();

}


Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

David_Pires

#9
May 24, 2018, 02:44 pm Last Edit: May 24, 2018, 02:47 pm by David_Pires
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.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

David_Pires

#11
May 24, 2018, 05:17 pm Last Edit: May 25, 2018, 12:47 pm by David_Pires
I don´t have test the codes.


bellow the sending code

Code: [Select]


#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

Code: [Select]


#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;

    }
  }
}





Robin2

Please post complete programs so I can compile them.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

David_Pires

#13
May 25, 2018, 10:43 am Last Edit: May 25, 2018, 10:50 am by David_Pires
I´ve modified the #11 reply and complete the code.

Robin2

I´ve modified the #11 reply and complete the code.
I suspect you have not tried to compile your sending code.

Have another go :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up