From 0dee19f28ca086933637317325ab4daecb6b1cae Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Sat, 5 Jun 2021 13:11:09 +0100 Subject: [PATCH] Updated main to use portable Ticker Lib --- Control/data/index.html | 8 +------- Control/src/main.cpp | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Control/data/index.html b/Control/data/index.html index e9ed800..152b6f2 100644 --- a/Control/data/index.html +++ b/Control/data/index.html @@ -108,17 +108,11 @@ var BTRY_VOLT = 0; var ODO_DIST = 0; - function round(value, precision) { - var multiplier = Math.pow(10, precision || 0); - return Math.round(value * multiplier) / multiplier; - } - connection.onmessage = function (event) { var raw_data = event.data; console.log(raw_data); var data = JSON.parse(raw_data); - digiBTRY_VOLT = data.BTRY_VOLT; - BTRY_VOLT = round((digiBTRY_VOLT * (4.8e-4) + 4), 1) + BTRY_VOLT = data.BTRY_VOLT; ODO_DIST = data.ODO_DIST; document.getElementById("btry_meter").value = BTRY_VOLT; document.getElementById("Odometer").innerHTML = ODO_DIST; diff --git a/Control/src/main.cpp b/Control/src/main.cpp index b122c92..245331e 100644 --- a/Control/src/main.cpp +++ b/Control/src/main.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include "TickerV2.h" #include #include #include @@ -27,10 +27,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) // Global objects AsyncWebServer webserver(80); WebSocketsServer websocketserver(81); -Ticker ticker(returnSensorData, 500, 0, MILLIS); +Ticker ticker; // Global variables -int battery_voltage = 0; +float battery_voltage = 4.0f; int distance_travelled = 0; void setup() @@ -71,7 +71,7 @@ void setup() websocketserver.begin(); websocketserver.onEvent(webSocketEvent); - ticker.start(); + ticker.attach(0.5, returnSensorData); } void loop() @@ -101,7 +101,17 @@ void printFPGAoutput() void returnSensorData() { // Collect sensor data here? + distance_travelled++; + if (battery_voltage < 6) + { + battery_voltage += 0.2; + } + else + { + battery_voltage = 4; + } String JSON_Data = String("{\"BTRY_VOLT\":") + battery_voltage + String(",\"ODO_DIST\":") + distance_travelled + "}"; + Serial.println(JSON_Data); websocketserver.broadcastTXT(JSON_Data); }