We are about to switch to a new forum software. Until then we have removed the registration on this forum.
This is the code for the ECG graph display using Arduino and AD8232 heart rate sensor.
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
float height_old = 0;
float height_new = 0;
float inByte = 0;
void setup () {
// set the window size:
size(1000, 400);
// List all the available serial ports
println(Serial.list());
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0xff);
}
void draw () {
// everything happens in the serialEvent()
}
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 = 512; // 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);
}
//Map and draw the line for new data point
inByte = map(inByte, 0, 1023, 0, height);
height_new = height - inByte;
line(xPos - 1, 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++;
}
}
}
Any help would be highly appreciated.
Answers
The comments do a fine job of explaining it.
There is data coming in over a serial line.
The data is used to draw a line on the screen.
Have you tried running it? Does it work?
please don't shout.
i have edited your post to be easier to read.
@utkarsh101 -- re:
Can you please explain which lines you don't understand? Ask specific questions to get more help. Most lines have extensive comments that already "explain the code line by line".
by help i mean , like it says int xPos=1; what does this means (i know we declaring int variable named xPos whoes value is equal to 1 ) what i want to know is how the program flows like that in eclipse u can dry run the program and see what every line is doing , but im not able to follow up this in processing language.
Again, you're going to have to be specific. If there are certain lines you do not understand (which can't be all of them as you seem to have a firm grasp on the
int xPos = 1;
line), ask about them.Also, do yourself a favor and learn how to "read" how the code will be executed. It's a lot like cooking - you have to know how to follow the recipe. When you cook, you don't just read the ingredients list and throw them on a plate. You gather the ingredients first, then follow the steps below them. Similarly, you don't grasp a sketch's code by having every line explained to you.
Start with the variables. What type does each variable have? What values do they start with? How will they be used? What does
setup()
prepare, and how many times does it need to do those things? Doesdraw()
do anything? If not, what makes things appear in the sketch? What has to occur for that function to run? What does that function do first? What next? What if that last step worked to produce meaningful data? What if it didn't?I have exactly the same code and it works on V2 but not in V3.It essentially gets the serial data and plots it in the first horizontal and plots the serial input then advances horizontal and plots the next serial input from the Arduino. I don't know why it wont work in version 3.
try this
`
void draw () {
}
void serialEvent (Serial myPort) { // get the ASCII string: String inString = myPort.readStringUntil('\n');
if (inString != null) { // trim off any whitespace: inString = trim(inString);
}
`