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
unsigned long period = 10000;
unsigned long start = 0;
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:
}
}
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
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.