Hello Jack and thank you so much for your idea. But I'm not sure that I understand it.
So this is my code at the moment:
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 3 // what pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
const int buttonPin = 2;
const int buttonHumid = 4;
const int ledPin = 13;
int buttonState = 0;
int buttonStateHumid = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonHumid, INPUT);
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
buttonState = digitalRead(buttonPin);
buttonStateHumid = digitalRead(buttonHumid);
if (digitalRead(buttonPin) == HIGH) {
digitalWrite(ledPin, HIGH);
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
Serial.print(t);
}
}
if (digitalRead(buttonHumid) == HIGH) {
digitalWrite(ledPin, HIGH);
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
Serial.print(h);
}
}
digitalWrite(ledPin, LOW);
}
So it's very simple, I use the DHT example. If I press the first button I send the temperature float and if I press the second one I send the humidity.
At this point something that I don't understand is if I send a float why I can't receive directly a float on the receiver part ?
This is my receiver code:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()>0) { //If there is data in the Serial Line
int dataByte = Serial.read(); //save data into integer variable dataByte
Serial.write(dataByte);
}
}
}
So I send a float like "24.10 and I receive an int that I store in dataByte. If I use "Serial.print(dataByte)" I can see on the monitor something like "5052464948" and you told my that it's the ASCII code, so if I use "Serial.write(dataByte)" I can see what I want: "24.10".
But it's just on the screen, I don't understand in what kind of type my dataByte value is. I want to print my "24.10" on a 7seg ments 4 digit screen. I wrote a function to print a number on it, for example I call "print_function(2,3)" and we see the number "2" on the 3rd digit. If I call "print_function(5,4)" we can see the number 5 on the fourth digit.
I this situation I wanted to send my float, receive a float separate it on 4 number and store it on a tab. For 24.10 I will have something like that:
tab[0]=2
tab[1]=4
tab[2]=1
tab[3]=0
And then I can easily call my "print_function" with a loop for 0 to 3 and with each case of the tab.
I'm not sure that I understand your example, your create a type with a float and a 4 bytes. You store the float in xmitTemp.f but just after that you send the four cases of the byte type but we didn't save anything on it ? Where we convert the float in a byte type and store it in b[4] ?
And in the receiver code, we read the four parts of the byte type and we store the float on the first case of myValues "myValues[0]".
So it's just like we separate the float in 4 bytes on the sender code, we send it, we received the 4 bytes on the receiver code but we store the float. So why don't send and receive directly a float ?
Thank you so much for your help !