dunque sto provando questo codice:
#include "cactus_io_SHT31.h"
cactus_io_SHT31 sht31;
float t;
float statTemp[8];
int i = 0;
unsigned long tempo = 0;
void setup() {
Serial.begin(9600);
if (!sht31.begin()) {
Serial.println("Count not find sensor. Check wiring and I2C address");
while (1) delay(1);
}
}
void loop() {
if ((millis() - tempo) > 5000) {
t = sht31.getTemperature_C();
statTemp[i++] = t;
}
if (i >= 8) {
Serial.print(statTemp[0],1);
Serial.print(",");
Serial.print(statTemp[1],1);
Serial.print(",");
Serial.print(statTemp[2],1);
Serial.print(",");
Serial.print(statTemp[3],1);
Serial.print(",");
Serial.print(statTemp[4],1);
Serial.print(",");
Serial.print(statTemp[5],1);
Serial.print(",");
Serial.print(statTemp[6],1);
Serial.print(",");
Serial.println(statTemp[7],1);
i = 0; // Torno al primo elemento
}
}
ogni 5 secondi stampa qualcosa del genere:
20.3,20.3,20.3,20.3,20.3,20.3,20.3,20.3
siccome la stampa dell'array vorrei stamparla anche prima del suo riempimento, allora ho provato a fare questo:
#include "cactus_io_SHT31.h"
cactus_io_SHT31 sht31;
float t;
float statTemp[8];
int i = 0;
unsigned long tempo = 0;
void setup() {
Serial.begin(9600);
if (!sht31.begin()) {
Serial.println("Count not find sensor. Check wiring and I2C address");
while (1) delay(1);
}
}
void loop() {
if ((millis() - tempo) > 5000) {
t = sht31.getTemperature_C();
statTemp[i++] = t;
}
if (i >= 8) {
i = 0; // Torno al primo elemento
}
Serial.print(statTemp[0], 1);
Serial.print(",");
Serial.print(statTemp[1], 1);
Serial.print(",");
Serial.print(statTemp[2], 1);
Serial.print(",");
Serial.print(statTemp[3], 1);
Serial.print(",");
Serial.print(statTemp[4], 1);
Serial.print(",");
Serial.print(statTemp[5], 1);
Serial.print(",");
Serial.print(statTemp[6], 1);
Serial.print(",");
Serial.println(statTemp[7], 1);
}
la stampa è questa:
per i primi 5 secondi, ottengo giustamente sempre
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
poi
20.2,0.0,0.0,0.0,0.0,0.0,0.0,0.0
20.2,20.2,0.0,0.0,0.0,0.0,0.0,0.0
20.2,20.2,20.2,0.0,0.0,0.0,0.0,0.0
20.2,20.2,20.2,20.2,0.0,0.0,0.0,0.0
20.2,20.2,20.2,20.2,20.2,0.0,0.0,0.0
20.2,20.2,20.2,20.2,20.2,20.2,0.0,0.0
20.2,20.2,20.2,20.2,20.2,20.2,20.2,0.0
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
20.2,20.2,20.2,20.2,20.2,20.2,20.2,20.2
e giusto? Perchè non aspetta 5 secondi prima di riempire il valore successivo dell'array?