HM-10. BLE. Arduino UNO. Notify. Bluetooth. AT. iBeacon. Arrhythmia

5.- Floats numbers.

p110_UNO_HM10_float.aia (184.6 KB)

Sometimes we want to work with float numbers, in this example I perform the operation with float numbers and then convert it to String and send it as String.

  • It receives number (mensaje) as String, converts it toFloat, performs the operation on float, converts the result and send String.

flotante = 7.101031f * mensaje.toFloat() / 3.303071f;

// Juan A. Villalpando
// http://kio4.com/arduino/161_HM10_BLE.htm

#include <SoftwareSerial.h>
// El TXD del módulo al pin 2 (RX) del Arduino.
// El RXD del módulo al pin 3 (TX) del Arduino.
SoftwareSerial HM10(2, 3);

char caracter = "";  
String mensaje = "";
float flotante = 1.0f;

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

void loop(){
  if(HM10.available()) { 
  caracter = HM10.read();
  mensaje = mensaje + caracter;
 
  if(caracter == NULL ) { 
     Serial.println(mensaje); 
     flotante = 7.101031f * mensaje.toFloat() / 3.303071f;
     Serial.println(flotante,7); // Muestra con 7 decimales.
     HM10.print(flotante,7); // Envía con 7 decimales.
     mensaje = "";
     delay(100);
     }
  } 
} 

This lines:

Serial.println(flotante,7); // Shows in Serial Monitor with 7 decimals.
HM10.print(flotante,7); // Sends with 7 decimals.

Convert flotante to String with 7 decimals.

2 Likes