Hi,
I’m trying to create a portable ECG with ESP32, AD8232 and battery 18650. I modified a sketch for arduino and processing pde to use bluetooth communication instead of serial over usb.
My Arduino sketch work perfectly with Arduini Serial monitor. I’m able to plt using both usb serial communication and bluetooth one and in both cases the resolution is fine (baud rate is 115200).
When I use Processing sketch, the issue pops-up. Using serial over usb the plot is quite acceptable while with bluetooth the resolution is poor. Here below the *.pde:
Is there any known issue on serial bluetooth in Processing?
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 5; // horizontal position of the graph
float height_old = 0;
float height_new = 0;
float inByte = 0;
void setup () {
// set the window size:
size(1600, 900);
// List all the available serial ports
printArray(Serial.list());
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 115200);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0xff);
}
void draw () {
//Map and draw the line for new data point
// inByte = map(inByte, 0, 5000, 0, height);
height_new = height - inByte;
line(xPos - 5, height_old, xPos, height_new);
height_old = height_new;
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0xff);
} else {
// increment the horizontal position:
//xPos++;
xPos = xPos + 5;
}
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// If leads off detection is true notify with blue line
if (inString.equals("!")) {
stroke(0, 0, 0xff); //Set stroke to blue ( R, G, B)
inByte = 2048; // middle of the ADC range (Flat Line)
}
// If the data is good let it through
else {
stroke(0xff, 0, 0); //Set stroke to red ( R, G, B)
inByte = float(inString);
}
// inByte = map(inByte, 0, 4200, 0, height);
inByte = inByte * 0.2;
}
}
Arduino sketch is below:
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
SerialBT.begin("ECG"); //Bluetooth device name
pinMode(23, INPUT); // Setup for leads off detection LO +
pinMode(33, INPUT); // Setup for leads off detection LO -
}
void loop() {
if((digitalRead(23) == 1)||(digitalRead(33) == 1)){
SerialBT.println('!');
Serial.println('!');
}
else{
SerialBT.println(analogRead(4));
Serial.println(analogRead(4));
}
delay(1);
}