Skip to content

Commit d2db7f9

Browse files
committed
Added example sketch to readout the battery stats
1 parent b513c9d commit d2db7f9

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Motor Core with battery stats readout.
2+
3+
This code for the Arduino Robot's motor board
4+
is the stock firmware with added code printing
5+
the battery status.
6+
7+
*/
8+
9+
#include <TwoWayIntegerEasyTransfer.h>
10+
#include <LottieLemon.h>
11+
#include <SoftwareSerial.h>
12+
13+
enum {
14+
SW_RX = SDA,
15+
SW_TX = SCL
16+
};
17+
18+
SoftwareSerial swSerial{ SW_RX, SW_TX };
19+
20+
LottieLemon::MotorBoard motorBoard;
21+
22+
void setup() {
23+
// start serial communication
24+
swSerial.begin(19200);
25+
// initialize the libraries
26+
Serial1.begin(9600);
27+
TwoWayIntegerEasyTransfer.begin(&Serial1);
28+
TwoWayIntegerEasyTransfer.attach([]() { doSystemReset(); });
29+
doSystemReset();
30+
}
31+
32+
void loop() {
33+
if (TwoWayIntegerEasyTransfer.hasReceivedData()) {
34+
TwoWayIntegerEasyTransfer.processInput();
35+
}
36+
motorBoard.run();
37+
38+
measureBattery();
39+
}
40+
41+
void doSystemReset() {
42+
motorBoard.reset();
43+
}
44+
45+
void measureBattery() {
46+
static String bar; // string for storing the information
47+
static unsigned long tStart = millis();
48+
unsigned long tStop = millis();
49+
if ((tStop - tStart) > 100) {
50+
bar = String(""); // empty the string
51+
// read the sensors and add them to the string
52+
bar = bar + "Vbat=" + motorBoard.getBatteryTerminalVolts() + 'V' +
53+
"\tIcharge=" + motorBoard.getBatteryChargeMilliamps() + "mA" +
54+
"\tIdischarge=" + motorBoard.getBatteryDischargeMilliamps() + "mA";
55+
56+
swSerial.println(bar);
57+
tStart = tStop;
58+
}
59+
}

0 commit comments

Comments
 (0)