system
March 10, 2012, 3:31pm
1
Bonjour,
J'essaye d'envoyer une valeur de type float mais bien évidement cela ne marche pas.
Comment faire une convertion ou autre ?
Voici le code de base :
#include <VirtualWire.h>
float temp=23.2;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}
void loop()
{
const char *msg = temp;
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
delay(200);
}
Merci
Bonjour, pour convertir ta variable float en string c' est possible mais y a mieux!
Envoi directement soit un int (si tu as toujours, deux unités après ta virgule tu multiplie par 100 avant l'envoi et tu divise par 100 a la réception) regarde ce topic =
http://arduino.cc/forum/index.php/topic,92696.0.html
Mais tu peux faire un array de type float et a mon avis ça roule encore mieux 8)
Bonjour,
Michel_B:
J'essaye d'envoyer une valeur de type float mais bien évidement cela ne marche pas.
Comment faire une convertion ou autre ?
Le cast d'adresse en pointeur est ton amis
#include <VirtualWire.h>
float temp = 23.2;
void setup()
{
vw_set_ptt_inverted(true);
vw_setup(2000);
}
void loop()
{
vw_send((uint8_t *)&temp, sizeof(float));
vw_wait_tx();
delay(200);
}
(Le type float étant un type émulé en software par avr-gcc, je suis pas à 100% sur que le cast en uint8_t* soit vraiment effectif, à tester ;))
system
March 10, 2012, 6:19pm
4
Entre temps j'ai fait ceci en regardant le topic de cutprod :
Je regarderais après l'idée de Skywodd.
++
TX
#include <VirtualWire.h>
int report[2];
float temp=22.35;
int tmp1;
void setup()
{
Serial.begin(9600);
Serial.println("setup");
vw_setup(2000);
}
void loop()
{
tmp1 = (temp * 100);
report[0] = tmp1;
vw_send((uint8_t *)report, sizeof(report));
vw_wait_tx();
delay(1000);
}
RX
#include <VirtualWire.h>
float temperature;
void setup()
{
Serial.begin(9600);
Serial.println("setup");
vw_setup(2000);
vw_rx_start();
}
void loop()
{
int buf[2];
uint8_t buflen = sizeof(buf);
if (vw_get_message((uint8_t *)buf, &buflen))
{
Serial.print("Got: ");
Serial.println();
temperature=((buf[0])*0.01);
Serial.print(temperature);
Serial.println();
}
}