How to in an array go from integer to decimal by multiply by 100?

Hi all. I have a problem sending a array of data thru a RF24 transmitter. It worls when i use integer (int data[5]:wink: but i dont get any decimals of course. I have read long time ago that you can multiply by 100 before sending and after receiving divide the result by 100 and get around the decimal problem.

I am a newbie so you have other ideas about making the code better, please do.

/*This Sketch is the transmitter part of transmitter No:1 of a nRF24L01 system.
The transmitter has an array that can hold 5 differens values and send them via a nRF24L01
transmitter to a receiver, (Se receiver Sketch). You can use more sensors, just extend the array.
Orginal code from Gökhan Göl http://www.gokhangol.com/arduino/nrf24l01-sensor-istasyonu-weather-station/
Changed to use with 3pcs BME280 temp, moist, pressure sensors.
Changes and code by oletborg@gmail.com Sweden 24 october 2017
 */

// PIN connections ********************************************************************************************
// MOSI:11             // Connect pin MOSI on RF24 to Arduino pin 11
// MISO:12             // Connect pin MISI on RF24 to Arduino pin 12
// SCK :13             // Connect pin SCK on RF24 to Arduino pin 13
// RF_CS 7             // Connect pin CS on RF24 to Arduino pin 7
// RF_CSN 8            // Connect pin CSN on RF24 to Arduino pin 8
// DHT11PIN 2          // Connect the DHT11 pin11 on the sensor to Arduino pin digital 2
// LDR A0              // Connect the light LDR sensor to Arduino pin analog A0
// GAS A1              // Connect the GAS sensor to Arduino pin analog A1
// RAIN A2             // Connect the RAIN sensor to Arduino pin analog A2

// Define pins and variables ************************************************************************************
#include <SPI.h>       // Add SPI library
#include <RF24.h>      // Add nrf24l01 communication library
//#include <dht11.h>   // Add the dht11 library.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h> //Radio

#define DHT11PIN 2     // Pin number defined for DHT11 temperature and humidity sensor
#define ldr A0         // The pin number defined for the ldr sensor
#define gas A1         // the pin number defined for the gas sensor
#define rain A2        // the pin number defined for the rain sensor
#define RF_CS 7        // The CS pin on RF24 (in some models, it is written as CE) connected to Arduino pin 7
#define RF_CSN 8       // RF24 CSN is connected to Arduino pin 8
//dht11 DHT11;

//BME280****************************************************************************
#define BME_SCK  13  // (SCL)
#define BME_MISO 12  // (SDO)
#define BME_MOSI 11  // (SDA)
#define BME_CS1   4  // (CSB) Chip select for sensor 1
#define BME_CS2   5  // (CSB) Chip select for sensor 2
#define BME_CS3   6  // (CSB) Chip select for sensor 3
#define SEALEVELPRESSURE_HPA (1005.87)

Adafruit_BME280 bme1(BME_CS1); // hardware SPI
Adafruit_BME280 bme2(BME_CS2); // hardware SPI
Adafruit_BME280 bme3(BME_CS3); // hardware SPI

RF24 radio(RF_CS, RF_CSN); // RF24 define the CS and SSN
const uint64_t pipes[3] = { 0xe7e7e7e7e7LL, 0xc2c2c2c2c2LL, 0xF0F0F0F0E3LL }; // The two different pipes we use


int data[5];

void setup() //***********************************************************************************
{
  Serial.begin(9600);  // communication started at serial communication speed 9600

// Test communication for BME280******************************************************************
  delay(1000);
  Serial.println(F("BME280 test"));
  bool status;
  status = bme1.begin();
    if (!status) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
  while (1);
}
Serial.println("Sensor Temp ,Moist ,Light ,Gas ,Rain");
  radio.begin();                      //  rf24 communication started
  radio.setDataRate(RF24_250KBPS);    // Transmitter speed to lowest get the safest and long distance
  //radio.setChannel(108);            // Above most Wifi Channels
  radio.setPALevel(RF24_PA_MAX);
  radio.openWritingPipe(pipes[0]);    //  opens the address needed for transmitter. That is the part that sends the address 0xe7e7e7e7e7LL.
  radio.openReadingPipe(1, pipes[1]); //  Can be used if you want the module to act as a receiver to.
  radio.startListening();             // Checks whether data is coming from radio modules.
  radio.printDetails();               // This is a debug where you can se the response of your RF24 module.
}
 
void loop()  // ************************************************************************************
{
 // int kontrol=DHT11.read(DHT11PIN); 
  //temp1 = bme1.readTemperature();
  data[0]=bme1.readTemperature();
  data[1]=bme1.readHumidity();
  data[2]=analogRead(ldr);           // Read LDR light sensor 0-1023
  data[2]=map(data[2],0,1023,0,100); // Convert reading of light to 0= dark to 100% full daylight
  data[3]=analogRead(gas);           // Read the gas sensor
  data[4]=analogRead(rain);          // Read rain sensor

  
// Print the data from the sensors to the serial monitor. ******************************************
  Serial.print("Temp: ");      // Print Heading text. Temp:
  Serial.print(data[0]);       // Print Temperature data from dht11 sensor in celcius
  Serial.print("\t  ");        // Print a tab.
  Serial.print("Moist %: ");   // Print Heading text Moist %:
  Serial.print(data[1]);       // Print Moisture value from the dht11 sensor in %
  Serial.print("\t");          // Print a tab.
  Serial.print("Light %: ");   // Print Heading text. Light %:
  Serial.print(data[2]);       // Print data from LDR sensor
  Serial.print("\t");          // Print a tab.
  Serial.print("Gas: ");       // Print Heading text. Gas:
  Serial.print(data[3]);       // Print data from the GAS sensor
  Serial.print("\t");          // Print a tab.
  Serial.print("Rain: ");      // Print Heading text. Rain:
  Serial.println(data[4]);     // Print data from the GAS sensor
  
// Sending all sensor values to the receiver. *****************************************************
  radio.stopListening();
  radio.write(data,sizeof(data)); //the measurement data is transmitted from the 5 variables
  radio.startListening();
  delay(1000);
}

I have read long time ago that you can multiply by 100 before sending and after receiving divide the result by 100 and get around the decimal problem.

So, why don't you?

PaulS:
So, why don't you?

I have tried to first make

int data[5];

to

float data[5];

I have also tried to make a new float variable and pass the data to it and send it.

But non of them work.

So my question is still the same, i actuallt dont know how to do it. Where do i add the *100?

Where do i add the *100?

Anywhere you like. What, EXACTLY, do you want to multiply by 100?

It seems to me that you want to multiply the values stored in the first two elements of the array, and later divide them. But, I could be wrong. I was once.

PaulS:
Anywhere you like. What, EXACTLY, do you want to multiply by 100?

It seems to me that you want to multiply the values stored in the first two elements of the array, and later divide them. But, I could be wrong. I was once.

Hehe.. You are right, misunderstanding is the biggest issue. But you are totally right. It's the 2 first elements of the array that only give the temperature and humidity as an integer and i want to have at least one decimal.

I know almost everything about harvesting crops but this is really something else. I am sure i some time ago read that the easiest way is to use an integer and multiply with 100 to include the decimals and later divide it again. Maybe i misunderstand, but i cant figure it out.

On the sending end:

  data[0]=bme1.readTemperature() * 100;
  data[1]=bme1.readHumidity() * 100;

On the other end:

float temperature = data[0] / 100.0;
float humidity = data[1] / 100.0;

PaulS:
On the sending end:

  data[0]=bme1.readTemperature() * 100;

data[1]=bme1.readHumidity() * 100;




On the other end:


float temperature = data[0] / 100.0;
float humidity = data[1] / 100.0;

Thanks PaulS!!!
I thought i had to use float at the sending side. It's very helpful to have people like you here PoulS. Thank you very much!

You do not need floats on the receiving end either. Temperature and humidity easily fit into int variables. Just multiply everything with 100 and use integer calculations and comparisons

Temperature and humidity easily fit into int variables.

While it rarely makes sense to speak of 45.2% humidity, it often DOES make sense to speak of temperatures in less than whole degrees. So, floats MAY be necessary.

What I meant was: Just because the temperature has a decimal point does not mean you need a float. Multiply every value with 100, so 44.3 degrees becomes 4430. That is enough precision and range for any practical purpose (excluding nuclear power plants and astronomy :slight_smile: )

That is enough precision and range for any practical purpose (excluding nuclear power plants and astronomy :slight_smile: )

True. But, displaying 4430 when the temperature is 44.3 degrees will not endear you to your users. At some point, you may need to re-create the value as a float.