zkouším převádět 2 int hodnoty do jedné hexadecimální char proměnné. U prvního čísla mi to funguje a převádí se dobře, ale u toho druhého se vždy na druhém místě objeví nějaký náhodný znak.
Kód: Vybrat vše
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <SoftwareSerial.h>
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
sht31.begin(0x44);
Serial.begin(9600);
delay(1000);
}
void loop() {
char data1[5];
float t = sht31.readTemperature();
float h = sht31.readHumidity();
int rh = int(h + 0.5); //rounded humidity
int wt = int(t * 100); //whole number
// převod na hex
sprintf(data1, "%04X%02X", wt, rh);
Serial.print(wt);
Serial.print(", ");
Serial.print(rh);
Serial.print(", hex: ");
Serial.println(data1);
delay(5000);
}
Kód: Vybrat vše
21:32:22.906 -> 2817, 63, hex: 0B013?
21:32:27.967 -> 2817, 63, hex: 0B013?
21:32:32.969 -> 2816, 62, hex: 0B003>
21:32:38.010 -> 2817, 62, hex: 0B013>
21:32:43.083 -> 2817, 62, hex: 0B013>
21:32:48.094 -> 2820, 62, hex: 0B043>
21:32:53.165 -> 2822, 62, hex: 0B063>
21:32:58.185 -> 2822, 62, hex: 0B063>
21:33:03.244 -> 2822, 62, hex: 0B063>
21:33:08.264 -> 2823, 62, hex: 0B073>
21:33:13.307 -> 2823, 63, hex: 0B073?