Arduino Forum

Topics => Robotics => Topic started by: Steewz on Apr 30, 2019, 04:17 am

Title: Using a bluetooth module to send/receive more than 1 information
Post by: Steewz on Apr 30, 2019, 04:17 am
Hey guys,

I recently bought a Bluetooth module ( HC-05) to use it in a personal project but I'm wondering if we can send/receive multiple informations. I use MIT app inventor to create my app and send my data to the Bluetooth module. I also plug the Tx and Rx pins of the HC-05 to the pins Rx and Tx of my Arduino.
So can we to that or the limit is one information.

Thank you for your help.
Steewz
Title: Re: Using a bluetooth module to send/receive more than 1 information
Post by: PaulS on Apr 30, 2019, 01:13 pm
Quote
So can we to that or the limit is one information.
What have you tried?

Is "<12,34,56>" one information, or more than one?
Title: Re: Using a bluetooth module to send/receive more than 1 information
Post by: Steewz on Apr 30, 2019, 02:41 pm
This is my code here the goal was to turn on a led depending on which information ( 1 2 or 3 ) I send from my phone to the BT module.
I tried to control with a slider on my app the brightness of my led ( from 0 to 255 ), and there is the problem if the slider reaches the values 0 1 or 3 it will activate the other instructions, that's why I'm asking if there is a way to send two different information, so can we for example set two variables that read the serial at the same time, one for numbers(int) and the other for a text for example(char) or there is another way to do it.



int state = 0;
const int ledr = 8 ;
const int ledb = 9 ;
const int ledg = 10 ;

void setup() {
   Serial.begin(9600);
   pinMode(ledr, OUTPUT);
   pinMode(ledb, OUTPUT);
   pinMode(ledg, OUTPUT);
   delay(1000);
}

void loop() {
   
    if(Serial.available() > 0){
    state = Serial.read();
    }
   
    if (state == '0') {
    digitalWrite(ledr, LOW);
    digitalWrite(ledb, LOW);
    digitalWrite(ledg, LOW);
    Serial.println("LED: OFF");
    state = 0;
   
      delay(1000);}
 
    if (state == '1') {
    digitalWrite(ledr, HIGH);
    Serial.println("Red Led: ON");
   
      delay(1000);
    }


    if (state == '2') {
    digitalWrite(ledb, HIGH);
    Serial.println("Blue Led: ON");
   
      delay(1000);
    }


    if (state == '3') {
    digitalWrite(ledg, HIGH);
    Serial.println("Green Led: ON");
   
      delay(1000);
    }

   
   }
Title: Re: Using a bluetooth module to send/receive more than 1 information
Post by: Idahowalker on Apr 30, 2019, 02:59 pm
I tried to control with a slider on my app the brightness of my led ( from 0 to 255 ), and there is the problem if the slider reaches the values 0 1 or 3 it will activate the other instructions, that's why I'm asking if there is a way to send two different information, so can we for example set two variables that read the serial at the same time, one for numbers(int) and the other for a text for example(char) or there is another way to do it.

Yes.
Title: Re: Using a bluetooth module to send/receive more than 1 information
Post by: Steewz on Apr 30, 2019, 10:05 pm
Can you please give me an example
Title: Re: Using a bluetooth module to send/receive more than 1 information
Post by: slipstick on Apr 30, 2019, 10:49 pm
Serial input basics (http://forum.arduino.cc/index.php?topic=396450) - I think example 5 has everything you need but read the rest as well.

Steve
Title: Re: Using a bluetooth module to send/receive more than 1 information
Post by: Idahowalker on May 01, 2019, 12:04 am
Yes, I can give you an example. Here are 3 tasks that ae sending different data sets meant for different destinations over the same serial port.
Code: [Select]

void fLIDAR_Is_OK ( void *pvParameters )
{
  String sSerial = "<$>";

  TickType_t xLastWakeTime;
  const TickType_t xFrequency = pdMS_TO_TICKS( 1000 );
  xLastWakeTime = xTaskGetTickCount();
  for ( ;; )
  {
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
    bool LIDAR_OK = true;
    if ( xSemaphoreTake( sema_SendSerialToBrain, xZeroTicksToWait ) == pdTRUE )
    {
      if (xHANDLE_LIDAR_ServoAspectChange == NULL )
      {
        Serial.println ( " xHANDLE_LIDAR_ServoAspectChange " );
        LIDAR_OK = false;
      }
      if ( xHANDLE_DoLIDAR == NULL )
      {
        Serial.println ( " xHANDLE_DoLIDAR " );
        LIDAR_OK = false;
      }
      if ( xHANDLE_SendLIDAR_InfoSerialToBrain == NULL )
      {
        Serial.println ( " xHANDLE_SendLIDAR_InfoSerialToBrain " );
        LIDAR_OK = false;
      }
      if ( xHANDLE_SendSerialToBrain == NULL )
      {
        Serial.println ( " xHANDLE_SendSerialToBrain " );
        LIDAR_OK = false;
      }
      if ( xHANDLE_TweakServoX == NULL )
      {
        Serial.println ( " xHANDLE_TweakServoX " );
        LIDAR_OK = false;
      }
      if ( xHANDLE_TweakServoY == NULL )
      {
        Serial.println ( " xHANDLE_TweakServoY " );
        LIDAR_OK = false;
      }
      if ( xHANDLE_GetIMU == NULL )
      {
        Serial.println ( " xHANDLE_GetIMU " );
        LIDAR_OK = false;
      }
      if ( LIDAR_OK )
      {
        SerialBrain.println ( sSerial );
      }
    }
    xSemaphoreGive ( sema_SendSerialToBrain );
    xLastWakeTime = xTaskGetTickCount();
  }
  vTaskDelete( NULL );
}
//******************************************
void fSendLIDAR_InfoSerialToBrain( void *pvParameters )
{
  struct stu_LIDAR_INFO pxLIDAR_INFO;
  for ( ;; )
  {
    xEventGroupWaitBits (eg, evtGetIMU, pdTRUE, pdTRUE, portMAX_DELAY);
    xSemaphoreTake( sema_LIDAR_FOR_ALARM, xSemaphoreTicksToWait );
    xQueueReceive ( xQ_LIDAR_FOR_ALARM, &pxLIDAR_INFO, QueueReceiveDelayTime );
    xSemaphoreGive( sema_LIDAR_FOR_ALARM );
    int CellCount = 1 ;
    String sSerial = "";
    sSerial.reserve ( 300 );
    sSerial.concat( "<#," );
    sSerial.concat( String(ScanPoints) + "," );
    sSerial.concat( String(pxLIDAR_INFO.ServoSweepUp) + "," );
    for ( CellCount; CellCount <= ScanPoints; CellCount++ )
    {
      sSerial.concat( String(pxLIDAR_INFO.Range[CellCount]) + "," );
    }
    sSerial.concat( ">" );
    vTaskDelay( 10 );
    SerialBrain.println ( sSerial );
  }
  vTaskDelete( NULL );
} // void fSendLIDAAR_InfoSerialToBrain( void *pvParameters )
////
void fSendSerialToBrain( void *pvParameters )
{
  struct stu_LIDAR_INFO pxLIDAR_INFO;
  xSemaphoreGive ( sema_SendSerialToBrain );
  for (;;)
  {
    xEventGroupWaitBits (eg, evtSendSerialToBrain, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( xSemaphoreTake( sema_SendSerialToBrain, xZeroTicksToWait ) == pdTRUE )
    {
      String sSerial = "";
      sSerial.reserve ( 300 );
      int CellCount = 1;
      xSemaphoreTake( sema_LIDAR_INFO, xSemaphoreTicksToWait );
      xQueueReceive ( xQ_LIDAR_INFO, &pxLIDAR_INFO, QueueReceiveDelayTime );
      xSemaphoreGive( sema_LIDAR_INFO );
      sSerial.concat( "<!," );
      sSerial.concat( String(ScanPoints) + "," );
      for ( CellCount; CellCount <= ScanPoints; CellCount++ )
      {
        sSerial.concat( String(pxLIDAR_INFO.Range[CellCount]) + "," );
      }
      sSerial.concat( ">" );
      SerialBrain.println ( sSerial );
    }
  }
  vTaskDelete( NULL );
} // void fSendSerialToBrain( void *pvParameters )