Trying to stop the serial monitor at 10 seconds

so im using the arduino uno as the microcontroller to power my myoware 2.0 to create an EMG glove, the code for which is coded on arduino IDE, then exported to jupyter for post processing.

I need the code to stop exactly at 10 seconds so i can record the same amount of data per patient in terms of samples recorded per tremor. Im using millis to do that but im unable to stop the data flow at the serial monitor at 10 seconds.

the code is as follows, i need my serial monitor output to stop at 10000 milli seconds while displaying an output of (time,value)

any amount of assisstance would be greatly appreciated :smile:
unsigned long period = 10000;
unsigned long start = 0;

void setup()
{
Serial.begin(2000000);
Serial.println("Signal value");
start = millis();
}

void loop()
{

unsigned long current = millis();
unsigned long x = current - start;

if(x == period) //if 10s pass, i stop to read the values
{
return;
start = current;
}
else
{
float sensorValue = analogRead(A0);
float millivolt = (sensorValue/1023)*5;

Serial.print("0");
Serial.print(",");
Serial.print("500");
Serial.print(",");

Serial.print(x);
Serial.print(",");
Serial.println(millivolt*100);
delay(2);
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:

}

}

Here as a public service is @manavtri7 's code control-T'd for nice indents, and copied for forum (control shift C) so it's in code tags:

unsigned long period = 10000;
unsigned long start = 0;

void setup()
{
  Serial.begin(2000000);
  Serial.println("Signal value");
  start = millis();
}

void loop()
{

  unsigned long current = millis();
  unsigned long x = current - start;

  if (x == period) //if 10s pass, i stop to read the values
  {
    return;
    start = current;
  }
  else
  {
    float sensorValue = analogRead(A0);
    float millivolt = (sensorValue / 1023) * 5;

    Serial.print("0");
    Serial.print(",");
    Serial.print("500");
    Serial.print(",");

    Serial.print(x);
    Serial.print(",");
    Serial.println(millivolt * 100);
    delay(2);
    // wait 2 milliseconds before the next loop for the analog-to-digital
    // converter to settle after the last reading:

  }

}
1 Like

@manavtri7
Welcome to the forum

Your topic was MOVED to its current forum category as it is more suitable than the original

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

I'm not 100% clear what you're trying to do. Is it that you want to take the readings every 10seconds and display them?

hey there, i need to take my code for every 10 milliseconds for 10 seconds and stop there

thank you for the edit, im sorry this is my first post here, didnt know how to do it

1 Like

Sorry to say that's no more clear to me.

Try > rather then == ... your chance of catching something within 0.001 of a second is slim.

Not a good idea to do this inside loop... what do you think happens after this?

Your current logic appears to be the opposite of what you want... every 10 seconds you do nothing... every other time through the loop you print stuff out?

// Intervals in milliseconds
#define REPORT_CYCLE 10
#define REPORT_INTERVAL 10000
unsigned long nextStart, endTime;
int analogInPin = A0;


void setup() {
  Serial.begin(2000000);
  nextStart = millis(); // Time for "right now"
  endTime = nextStart + REPORT_INTERVAL;
}

void loop() {
  unsigned long now = millis();
  if (now < endTime) { // still need to do more reporting
    if (now >= nextStart) { // time to get and report another reading
      nextStart += REPORT_CYCLE; // When the next reading should be done
              
               float sensorValue = analogRead(A0); 
               float millivolt = (sensorValue/1023)*5;

               Serial.print(now);
               Serial.print(",");
               Serial.println(millivolt*1000);  
    }
  }
}

rewrote my code with defined intervals, works much better now

Gotta say that's some pretty interesting code.. but hey, if it works for you then all good... enjoy.

Cool, because I for one couldn't help, not understanding what you actually wanted to do...

yessir ! thanks lads

I would have gone with something similar to this:

#define TEN_SECONDS 10000

float getReading(){
  float sensorValue = analogRead(A0);
  float millivolt = (sensorValue / 1023) * 5;
  return millivolt * 100;
}

void printReading(unsigned long timestamp, float reading) {
    Serial.print("0");
    Serial.print(", ");
    Serial.print("500");
    Serial.print(", ");
    Serial.print(timestamp);
    Serial.print(", ");
    Serial.println(reading);
}

bool hasExpired(float since, float interval) {
  return millis() > (since + interval);
}

void setup() {
  Serial.begin(2000000);
  Serial.println("Signal value");

  unsigned long loopStartTime = millis();

  while (!hasExpired(loopStartTime, TEN_SECONDS)) {
    unsigned long timestamp = millis() - loopStartTime;
    printReading(timestamp, getReading());
    delay(2);
  }
}

void loop() { }

ill try this too ! thank you ! any idea on how i can do a frequency analysis for the output ? Im trying FFT on matlab, i dont get a single answer, Im trying to determine whether a recorded tremor is parkinsonian or not.

I've not needed to learn how to do that yet, but if it was me, I would start by searching 'FFT Matlab' on YouTube and watching a few different vids.

FYI - I think it's pretty widely accepted that Arduinos are not suitable for medical diagnoses or life critical systems. Hobbies and prototypes, sure. But nothing that MUST be relied on.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.