-
Notifications
You must be signed in to change notification settings - Fork 1
Getting Started
In this quide i will tell how to install EUCSerialInterface, and start working with it.
There are two options how to install EUCSerialInterface.
Note: this library is not present in Arduino Library Manager for now. It is in development, so you need to manually install it using second method.
You can install EUCSerialInterface through Arduino Library Manager. It is easy:
- Go to Arduino Library Manager using
Sketch -> Include Library -> Manage Libraries, or ⇧⌘I and Ctrl + Shift + I on Mac and Windows respectively - Click on the search field, and write EUCSerialInterface
- Click
Install
Done! You have installed this library using Arduino Library Manager!
To install EUCSerialInterface manually, you need to do this:
- To go Releases page on this repo
- Download the latest release by clicking on release asset
- Go to
Arduino IDE -> Sketch -> Include Library -> Add .ZIP Library - Select downloaded release
- Click
Install
Done! Library is installed :D
There are two options of communicating with an EUC, Bluetooth and physical wiring to serial pins of the controller.
This is the snippet we will investigate now:
#include <SoftwareSerial.h>
#include "EUCSerialInterface.h"
VeteranSherman unicycle(BluetoothSerial, BluetoothSerial);
SoftwareSerial BluetoothSerial(9,10);
void setup {
Serial.begin(9600);
BluetoothSerial.begin(9600);
unicycle.setCallback(dataCallback);
}
void loop {
unicycle.tick();
}
void dataCallback(float voltage, float speed, float tempMileage, float current, float temperature, float mileage, bool dataIsNew) {
if (dataIsNew) {
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
Serial.print("Current: "); Serial.print(current); Serial.println("A");
Serial.print("Speed: "); Serial.print(speed); Serial.println("km/h");
Serial.print("Total mileage: "); Serial.print(mileage,3); Serial.println("km");
Serial.print("Temp mileage: "); Serial.print(tempMileage,3); Serial.println("km");
Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" deg Celsius");
Serial.println("");
Serial.println("");
}
}Let's break down our code to explain every part.
First, we include the actual library
#include "EUCSerialInterface.h"And create an EUC object:
VeteranSherman unicycle(BluetoothSerial, BluetoothSerial);Lemme explain every part. VeteranSherman means the model of a unicycle, to which we will talk. unicycle(BluetoothSerial, BluetoothSerial) means that you use you Bluetooth Serial to connect to an EUC.
void setup {
Serial.begin(9600);
BluetoothSerial.begin(9600);
unicycle.setCallback(dataCallback);
}Here we start hardware serial to print out the info, and start bluetooth serial to receive data.
unicycle.setCallback(dataCallback); means that dataCallback() function will be called as a callback.
void loop {
unicycle.tick();
}This function actually calls the callback function. Simply needs to be called regularly.
void dataCallback(float voltage, float speed, float tempMileage, float current, float temperature, float mileage, bool dataIsNew) {
...
}This is an actual callback function. It is called every time .tick() is called.
float voltage, float speed, float tempMileage, float current, float temperature, float mileage, bool dataIsNew is needed to actually receive data. When the function is called by the library, data is arranged like this. Remember this, if there is some variable is missing, or is in diff order, sketch wont compile, or work as expected.
if (dataIsNew) {
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
Serial.print("Current: "); Serial.print(current); Serial.println("A");
Serial.print("Speed: "); Serial.print(speed); Serial.println("km/h");
Serial.print("Total mileage: "); Serial.print(mileage,3); Serial.println("km");
Serial.print("Temp mileage: "); Serial.print(tempMileage,3); Serial.println("km");
Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" deg Celsius");
Serial.println("");
Serial.println("");
}This snippet will just output every variable to the Serial port of the PC.
That's all! More info at API Documentation.
Let's see our code, which we will examine:
#include "EUCSerialInterface.h"
VeteranSherman unicycle(Serial, Serial);
void setup {
Serial.begin(115200);
unicycle.setCallback(dataCallback);
}
void loop {
unicycle.tick();
}
void dataCallback(float voltage, float speed, float tempMileage, float current, float temperature, float mileage, bool dataIsNew) {
if (dataIsNew) {
// Do smth with data
}
}This is pretty much the same as the previous example, but there are some diffs:
VeteranSherman unicycle(Serial, Serial);Instead of using BluetoothSerial, it uses normal Serial.
Serial.begin(115200);This is the baudrate setting for every unicycle.
That's all! For more info see API Documentation.
EUCSerialInterface wiki (c) GGorAA