Hello,
I am currently trying to read off a sensor (LPMS-ME1), an accelerometer that uses UART to send data to an Arduino Portenta H7, using the M7 core through serial communication.
The code below works perfectly on an Arduino Due and an Arduino Mega, but it does not work with the Portenta H7 because the Portenta does not support the *SerialEvent1()*function -- nor any of the SerialEvent#() functions. Is there a workaround without using the SerialEvent1() function?
I tried putting everything inside the SerialEvent1() function into void loop but it just completely froze the IDE -- and did not produce an error message. Additionally, I verified that I am using the correct TX & RX pins, and have previously established serial communication with the Portenta.
Any suggestion are appreciated,
/*
Code to test getting data from the LPMS-ME1 IMU sensor
Modified from:
http://www.arduino.cc/en/Tutorial/SerialEvent
Written by Cole
*/
char inputdata[66];// reserve 66 bytes for the input data as a default message length is 66 bytes
bool dataComplete = false; // whether the string is complete
union accZBytes {
byte data[4];
float fval;
} accZ;
union accYBytes {
byte data[4];
float fval;
} accY;
union accXBytes {
byte data[4];
float fval;
} accX;
float accZf;
float accYf;
float accXf;
float accMag;
int dataIter = 0;
void setup() {
// initialize serial:
Serial.begin(9600);
Serial1.begin(19200); // Default datarate of LPMS sensor is 115200 from PC/USB testing
// Prep LEDs
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// print the string when a newline arrives:
if (dataComplete) {
// Serial.println(inputString);
// Parse the input data (just yoink the z acceleration for now)
accZ.data[0] = inputdata[31];
accZ.data[1] = inputdata[32];
accZ.data[2] = inputdata[33];
accZ.data[3] = inputdata[34];
accY.data[0] = inputdata[27];
accY.data[1] = inputdata[28];
accY.data[2] = inputdata[29];
accY.data[3] = inputdata[30];
accX.data[0] = inputdata[23];
accX.data[1] = inputdata[24];
accX.data[2] = inputdata[25];
accX.data[3] = inputdata[26];
accZf = accZ.fval;
accYf = accY.fval;
accXf = accX.fval;
accMag = sqrt(sq(accZf) + sq(accYf) + sq(accXf));
// Print the resulting acceleration to the serial monitor
Serial.print("Acceleration is: [");
Serial.print(accXf);
Serial.print(",");
Serial.print(accYf);
Serial.print(",");
Serial.print(accZf);
Serial.println("]");
Serial.print("Magnitude : ");
Serial.println(accMag);
// clear the string:
dataIter = 0;
dataComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent1() {
if (Serial1.available()) {
digitalWrite(LED_BUILTIN,HIGH);
// get the new byte:
char inChar = (char)Serial1.read();
// add it to the inputString:
inputdata[dataIter] = inChar;
dataIter++;
// if the packet is done, reset
if (inChar == '\n') { // \n is 0x0A, the temination character for the data packet
dataComplete = true;
dataIter = 0;
}
}
}