Skip to content

Commit

Permalink
Improved Arduino
Browse files Browse the repository at this point in the history
  • Loading branch information
pbosetti committed Jun 20, 2024
1 parent 37744a3 commit a705067
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions arduino/mads/mads.ino
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <ArduinoJson.h>
#define VERSION "1.0.0"
#define BAUD_RATE 115200
#define CURRENT_X A2
#define CURRENT_Y A3
#define CURRENT_Z A4
#define CURRENT_X A0
#define CURRENT_Y A1
#define CURRENT_Z A2
#define CURRENT_B A3
#define CURRENT_C A4
#define DELAY 50UL // microseconds
#define TIMESTEP 500UL // milliseconds
#define TIMESTEP 160UL // milliseconds
#define DATA_FIELD "data"

JsonDocument doc;
Expand All @@ -15,18 +17,24 @@ void setup() {
// put your setup code here, to run once:
Serial.begin(BAUD_RATE);
Serial.print("# Starting power meter v" VERSION "\n");
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
static unsigned long prev_time = 0;
static const unsigned long timestep_us = TIMESTEP * 1000;
static const float factor = 1.0 / (1024.0 * 20.0);
static bool onoff = LOW;
unsigned long now = micros();
if (now - prev_time >= timestep_us) {
digitalWrite(LED_BUILTIN, onoff);
onoff = !onoff;
doc["millis"] = millis();
doc[DATA_FIELD]["AX"] = analogRead(CURRENT_X) * factor;
doc[DATA_FIELD]["AY"] = analogRead(CURRENT_Y) * factor;
doc[DATA_FIELD]["AZ"] = analogRead(CURRENT_Z) * factor;
doc[DATA_FIELD]["AB"] = analogRead(CURRENT_B) * factor;
doc[DATA_FIELD]["AC"] = analogRead(CURRENT_C) * factor;
serializeJson(doc, out);
Serial.print(out);
Serial.print("\n");
Expand Down

0 comments on commit a705067

Please sign in to comment.