diff --git a/arduino/node-code/node-code-final.ino b/arduino/node-code/node-code-final.ino index 092a142..5fdb07e 100644 --- a/arduino/node-code/node-code-final.ino +++ b/arduino/node-code/node-code-final.ino @@ -31,16 +31,16 @@ WebSocketsClient webSocket; // define variables uint16_t TVOC_base, eCO2_base; uint16_t counter = 0; -float temperature = 0; -float humidity = 0; uint16_t eCO2 = 0; uint16_t TVOC = 0; -bool noise = false; -unsigned long currentCounter; -unsigned long lastCounter; uint16_t interval = 5000; +float temperature = 0; +float humidity = 0; +unsigned long currentMillis; +unsigned long lastMillis; bool errorSGP30 = false; bool errorDHT11 = false; +bool noise = false; // hexdump function for websockets binary handler void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { @@ -119,8 +119,8 @@ void resetValues() { eCO2 = 0; TVOC = 0; noise = false; - lastCounter = 0; - currentCounter = 0; + lastMillis = 0; + currentMillis = 0; } // function to check for errors in sensors @@ -159,8 +159,7 @@ void update(){ temperature = float(dht.readTemperature()); humidity = float(dht.readHumidity()); - // display sensordata on oled screen - displayData(); + } // function to display data on oled screen @@ -203,12 +202,18 @@ void setup() { // loop function void loop() { + // loop the websocket connection so it stays alive webSocket.loop(); - if (currentCounter - lastCounter >= interval){ - lastCounter = millis(); + // display sensordata on oled screen + displayData(); + + // update when interval is met + if (currentMillis - lastMillis >= interval){ + lastMillis = millis(); update(); } - currentCounter = millis(); + // update the counter + currentMillis = millis(); } \ No newline at end of file