5

So am I using an Arduino with a shield to gain some voltage values for an ECG. I am then plotting that data in Python. However whenever I "observe" the serial port, be that through the Arduino serial monitor or in attempting to plot the values of time (using millis()) against ECG (voltage) the values reset, so millis() resets the zero, which is fine, except that some of the older values still show up. Now this is especially a problem in Python because it means the plot is malfunctioning at the start of the script, because it is plotting some of the older values and then after a couple of seconds it resets and the plot looks normal again.

To demonstrate this I have recorded it on video and put it on YouTube, here it is:

https://www.youtube.com/watch?v=dNpUakcRPec

Now initially I thought the problem was with Python or the Pyserial module so I labelled the video as such, but since it occurs in the serial monitor I realised the solution would be in the Arduino sketch. I have tried flushing, didn't work, I have tried even to not start the data till I send a start byte, but this didn't work on Python.

Ideally I could start the Python plotting script and it would skip, or ignore those initial values from the serial port before beginning again.

Here is the Arduino sketch I am using:

#include <eHealth.h>


unsigned long time;
// The setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600); 

}

// The loop routine runs over and over again forever:
void loop() {

  float ECG = eHealth.getECG();
  time = millis();
  Serial.print(time);
  Serial.print(" ");
  Serial.print(ECG, 3); 
  Serial.println(""); 


  delay(50);    // wait for 50 millisecond
}
1

The Arduino is designed to reset when a Serial monitor or script connects to it. The best way to mitigate this would be to send the Arduino some kind of "start sending" signal when you connect to the serial port. It would then send the data when your script was ready.

  • I understand that, however this is affecting my script because it means the script plots garbage at the start, my question is can I stop this from occuring. From where I see it, it is something I need to change in the Arduino sketch first. – user3284376 Mar 6 '14 at 17:52
  • I removed millis() and used a counter instead inside my loop. However when I try to send a start signal to my serial port, using ser.write('S') on Python, the arduino doesn't receieve it – user3284376 Mar 8 '14 at 14:59
  • You could try putting something in your setup function, like while (Serial.read() != 'S'); – TheDoctor Mar 8 '14 at 15:30
  • It's fine on the Arduino end of things, when I open the serial monitor, it happily prints, the problem is in Python, when I try to send the 'S' byte, the serial port appears not to receive it. – user3284376 Mar 8 '14 at 15:32
1

Take a look at if(Serial)

That makes the Sketch wait for the serial port before continuing.

void setup() {
//Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
; // wait for serial port to connect.
  }
} 

I have a sneaking suspicion that may only work on the Leonardo.

If so you could try an alternative:

  1. Have Python send something to Arduino after it connects.

  2. Wait until this is received in setup(): if (Serial.available() > 0) {...}

  3. Execute the loop() normally

e.g.

// send data only when you receive data:
    if (Serial.available() > 0) {
            // read the incoming byte:
            incomingByte = Serial.read();

            // say what you got:
            Serial.print("I received: ");
            Serial.println(incomingByte, DEC);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.