Working Code
Here, I just tested this code and it works (4 files below):
Nicla.ino:
#include "Nicla_System.h"
#include "Arduino.h"
#include "Arduino_BHY2.h"
void colorCycle(int loops) {
for(int i = 0; i < loops; i++) {
nicla::leds.setColor(red);
delay(1000);
nicla::leds.setColor(green);
delay(1000);
nicla::leds.setColor(blue);
delay(1000);
}
nicla::leds.setColor(off);
}
void setup(){
BHY2.begin(NICLA_I2C, NICLA_VIA_ESLOV);
nicla::leds.begin();
colorCycle(5);
}
void loop(){
// Update and then sleep
nicla::leds.setColor(blue);
BHY2.update(100);
nicla::leds.setColor(off);
}
Portenta.ino:
#include "Arduino.h"
#include "Arduino_BHY2Host.h"
#include <LandoRGBLedPortenta.h>
SensorXYZ accel(SENSOR_ID_ACC);
LandoRGBLedPortenta portentaLeds;
const int MAX_DUPLICATE_READINGS = 10;
String colorToToggle = "white";
int duplicateReadings = 0;
static unsigned long printTime = 0;
static unsigned long startTime = 0;
static String timeToCrash = "";
static String lastAccelReading = "";
void setup()
{
Serial.begin(115200);
while(!Serial);
Serial.println("Serial started!");
portentaLeds.setColor("red");
BHY2Host.begin(false, NICLA_VIA_ESLOV);
Serial.println("BHY2HostSuccess!");
portentaLeds.setColor("green");
accel.begin(10, 1);
Serial.println("Leaving setup!");
portentaLeds.setColor("blue");
printTime = millis();
startTime = printTime;
}
void loop()
{
BHY2Host.update();
if (millis() - printTime >= 100) {
printTime = millis();
String accelReading = accel.toString();
if(accelReading == lastAccelReading) {
duplicateReadings++;
if(duplicateReadings == MAX_DUPLICATE_READINGS) {
timeToCrash = " " + String(printTime - startTime) + " ms";
colorToToggle = "red";
}
}
else {
duplicateReadings = 0;
}
Serial.println(String("Acceleration values: ") + String(accelReading) + String(timeToCrash));
portentaLeds.toggleColor(colorToToggle);
lastAccelReading = accelReading;
}
}
Add this folder and the following 2 files to your Arduino libraries (C:\Users\lando\Documents\Arduino\libraries\LandoRGBLedPortenta
on my PC):
LandoRGBLedPortenta.h:
#ifndef LandoRGBLedPortenta_h
#define LandoRGBLedPortenta_h
#include "Arduino.h"
class LandoRGBLedPortenta
{
public:
LandoRGBLedPortenta();
void setColor(String targetColor);
void toggleColor(String targetColor);
private:
String _ledColor;
};
#endif
LandoRGBLedPortenta.cpp:
#include "Arduino.h"
#include "LandoRGBLedPortenta.h"
//Constructors:
LandoRGBLedPortenta::LandoRGBLedPortenta()
{
this->_ledColor = "off";
}
//Publics:
void LandoRGBLedPortenta::setColor(String targetColor)
{
this->_ledColor = targetColor;
if (targetColor == "off")
{
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
}
else if (targetColor == "red")
{
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
}
else if (targetColor == "green")
{
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
}
else if (targetColor == "blue")
{
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, LOW);
}
else if (targetColor == "yellow")
{
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
}
else if (targetColor == "magenta")
{
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, LOW);
}
else if (targetColor == "cyan")
{
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, LOW);
}
else if (targetColor == "white")
{
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, LOW);
}
}
void LandoRGBLedPortenta::toggleColor(String targetColor)
{
if (this->_ledColor == "off")
{
this->setColor(targetColor);
}
else
{
this->setColor("off");
}
}
Setup and Output
This is what my setup looks like:
And some serial data:
06:35:52.000 -> Acceleration values: XYZ values - X: 194 Y: 1129 Z: 3969
06:35:52.000 ->
06:35:52.094 -> Acceleration values: XYZ values - X: 194 Y: 1129 Z: 3970
06:35:52.094 ->
06:35:52.236 -> Acceleration values: XYZ values - X: 196 Y: 1130 Z: 3970
06:35:52.236 ->
06:35:52.327 -> Acceleration values: XYZ values - X: 193 Y: 1129 Z: 3970
06:35:52.327 ->
06:35:52.468 -> Acceleration values: XYZ values - X: 195 Y: 1126 Z: 3970
06:35:52.468 ->
06:35:52.562 -> Acceleration values: XYZ values - X: 196 Y: 1128 Z: 3970
06:35:52.562 ->
06:35:52.655 -> Acceleration values: XYZ values - X: 194 Y: 1130 Z: 3969
06:35:52.655 ->
06:35:52.795 -> Acceleration values: XYZ values - X: 195 Y: 1130 Z: 3970
06:35:52.795 ->
06:35:52.888 -> Acceleration values: XYZ values - X: 196 Y: 1128 Z: 3973
06:35:52.888 ->
06:35:53.028 -> Acceleration values: XYZ values - X: 196 Y: 1129 Z: 3969
06:35:53.028 ->
06:35:53.213 -> Acceleration values: XYZ values - X: 194 Y: 1128 Z: 3968
06:35:53.213 ->
06:35:53.352 -> Acceleration values: XYZ values - X: 195 Y: 1127 Z: 3970
06:35:53.352 ->
You can see the timestamp differences range from 90–150 ms -> 7–11 Hz.
Bugs
Do note however that there is a potential bug with the Nicla (only verified on my board right now) where the included code only works for half an hour before the connection screws up. Here are some links to my bug reports if you want to stay updated: