Hello, I'm trying to make a code for the continuous display of parameters extracted from the ECG (HR, RMSSD, SDNN, pNN50) and parameters extracted from the GSR signal (average SCL, SCR frequency and average SCR amplitude), but it displays the wrong parameters, can someone help me with a code idea for this? I mention that I have an AD8232 ECG sensor and a Grove GSR
Welcome to the Arduino forum! Is your problem with the original values being wrong or from the averaging being wrong? Did you develop your program one step at time so you proved the original data was correct before doing the averaging?
Have you swapped the analog input pins?
The values are wrong, those for the HRV parameters, the RMSSD parameter is too high compared to the SDNN. I want it to display these values in real time.
This is the code:
// ============================================================
// 1. CONFIGURARI GENERALE
// ============================================================
const int PIN_ECG = A0;
const int PIN_GSR = A1;
const int PIN_LO_PLUS = 10;
const int PIN_LO_MINUS = 11;
const unsigned long PERIOADA_ECG_US = 4000; // 250 Hz
const unsigned long PERIOADA_GSR_US = 100000; // 10 Hz
const unsigned long INTERVAL_AFISARE_MS = 10000UL; // afisare la 10 sec
unsigned long ultim_afisare_ms = 0;
unsigned long start_ms = 0;
// DEBUG ECG - pune false dupa ce confirmi ca HR se calculeaza corect
const bool DEBUG_ECG = true;
unsigned long ultim_debug_ecg_ms = 0;
// ============================================================
// 2. FILTRARE ECG
// ============================================================
float ecg_hp_prev_in = 0;
float ecg_hp_prev_out = 0;
const float ALPHA_HP = 0.987f; // trece-sus 0.5 Hz
float ecg_lp_prev = 0;
const float ALPHA_LP = 0.36f; // trece-jos 40 Hz
float ecg_prev1 = 0;
float ecg_prev2 = 0;
// ============================================================
// 3. DETECTIE R-PEAK (Pan-Tompkins cu integrare)
// ============================================================
// Fereastra de integrare 20 esantioane = 80 ms (la 250 Hz)
const int FER_INT = 20;
float int_buffer[FER_INT];
int int_index = 0;
float int_suma = 0;
float ecg_integrat = 0;
float prag_R = 0.0f;
float ecg_max_recent = 0.0f;
const float COEF_PRAG = 0.35f; // 35% din max recent (mai sensibil)
const unsigned long REFRACTAR_MS = 350;
unsigned long ultim_R_ms = 0;
// Buffer circular RR (ms) - ultimele 200 RR-uri
const int MAX_RR = 200;
unsigned int RR_buffer[MAX_RR];
int RR_index = 0;
int RR_count = 0;
unsigned int RR_anterior = 0; // pentru filtrare RR succesive
// ============================================================
// 4. GSR cu CALIBRARE EMPIRICA
// ============================================================
float gsr_lp_prev = 0;
const float ALPHA_GSR = 0.76f;
// Calibrare: in primele 30 sec masuram ADC mediu (baseline)
const unsigned long DURATA_CALIBRARE_MS = 30000UL;
bool GSR_calibrat = false;
float ADC_GSR_baseline = 0;
float ADC_calib_suma = 0;
unsigned long ADC_calib_n = 0;
// Scala empirica: SCL (uS) = 5.0 + (ADC_baseline - ADC) * SCALA
float SCALA_GSR = 0.1f; // se stabileste la calibrare (poate fi negativa)
const int FER_SCL = 100;
float SCL_buffer[FER_SCL];
int SCL_index = 0;
float SCL_suma = 0;
float SCL_curent = 0;
float SCL_acumulator = 0;
unsigned long SCL_n = 0;
float SCR_prag = 0.1f;
const unsigned long REFRACTAR_SCR_MS = 1000;
unsigned long ultim_SCR_ms = 0;
float SCR_prev = 0;
unsigned int SCR_count = 0;
float SCR_amp_suma = 0;
// ============================================================
// 5. VARIABILE TIMP
// ============================================================
unsigned long ultim_us_ecg = 0;
unsigned long ultim_us_gsr = 0;
// ============================================================
// SETUP
// ============================================================
void setup() {
Serial.begin(9600);
pinMode(PIN_LO_PLUS, INPUT);
pinMode(PIN_LO_MINUS, INPUT);
for (int i = 0; i < FER_SCL; i++) SCL_buffer[i] = 0;
for (int i = 0; i < FER_INT; i++) int_buffer[i] = 0;
Serial.println(F("============================================"));
Serial.println(F(" MONITOR STRES - HRV + GSR (continuu) "));
Serial.println(F("============================================"));
Serial.println(F("Plasati electrozii FERM."));
Serial.println(F("Start in 5 secunde..."));
delay(5000);
start_ms = millis();
ultim_afisare_ms = millis();
ultim_debug_ecg_ms = millis();
ultim_us_ecg = micros();
ultim_us_gsr = micros();
Serial.println(F(">>> START - calibrare GSR primele 30 sec"));
}
// ============================================================
// LOOP PRINCIPAL
// ============================================================
void loop() {
unsigned long acum_us = micros();
unsigned long acum_ms = millis();
// ECG la 250 Hz
if (digitalRead(PIN_LO_PLUS) == 0 && digitalRead(PIN_LO_MINUS) == 0) {
if (acum_us - ultim_us_ecg >= PERIOADA_ECG_US) {
ultim_us_ecg = acum_us;
proceseazaECG(acum_ms);
}
}
// GSR la 10 Hz
if (acum_us - ultim_us_gsr >= PERIOADA_GSR_US) {
ultim_us_gsr = acum_us;
proceseazaGSR(acum_ms);
}
// Calibrare GSR la 30 sec
if (!GSR_calibrat && acum_ms - start_ms >= DURATA_CALIBRARE_MS) {
finalizeazaCalibrareGSR();
}
// DEBUG ECG la fiecare 3 sec
if (DEBUG_ECG && acum_ms - ultim_debug_ecg_ms >= 3000) {
ultim_debug_ecg_ms = acum_ms;
Serial.print(F(" [ECG_DBG] integrat="));
Serial.print(ecg_integrat, 6);
Serial.print(F(" max="));
Serial.print(ecg_max_recent, 6);
Serial.print(F(" prag="));
Serial.print(prag_R, 6);
Serial.print(F(" RR_count="));
Serial.println(RR_count);
}
// Afisare metrici la fiecare 10 sec
if (acum_ms - ultim_afisare_ms >= INTERVAL_AFISARE_MS) {
ultim_afisare_ms = acum_ms;
afiseazaMetrici(acum_ms);
}
}
// ============================================================
// PROCESARE ECG (Pan-Tompkins)
// ============================================================
void proceseazaECG(unsigned long acum_ms) {
float ecg_brut = analogRead(PIN_ECG) * (5.0f / 1023.0f);
// Trece-sus 0.5 Hz
float ecg_hp = ALPHA_HP * (ecg_hp_prev_out + ecg_brut - ecg_hp_prev_in);
ecg_hp_prev_in = ecg_brut;
ecg_hp_prev_out = ecg_hp;
// Trece-jos 40 Hz
float ecg_filt = ecg_lp_prev + ALPHA_LP * (ecg_hp - ecg_lp_prev);
ecg_lp_prev = ecg_filt;
// Derivata
float deriv = ecg_filt - ecg_prev2;
ecg_prev2 = ecg_prev1;
ecg_prev1 = ecg_filt;
// Patrat
float ecg_pat = deriv * deriv;
// INTEGRARE pe fereastra 80 ms (Pan-Tompkins)
int_suma -= int_buffer[int_index];
int_buffer[int_index] = ecg_pat;
int_suma += ecg_pat;
int_index = (int_index + 1) % FER_INT;
ecg_integrat = int_suma / FER_INT;
// Prag adaptiv (35% din max recent, decay lent)
if (ecg_integrat > ecg_max_recent) {
ecg_max_recent = ecg_integrat;
} else {
ecg_max_recent *= 0.9995f;
}
prag_R = COEF_PRAG * ecg_max_recent;
// Detectie R-peak cu filtrare RR succesive
if (ecg_integrat > prag_R && (acum_ms - ultim_R_ms) > REFRACTAR_MS) {
if (ultim_R_ms > 0) {
unsigned long RR = acum_ms - ultim_R_ms;
if (RR > 300 && RR < 2000) {
// Filtrare RR succesive: max 20% diferenta (elimina artefactele)
bool valid = true;
if (RR_anterior > 0) {
float diff_rel = abs((float)RR - (float)RR_anterior) / (float)RR_anterior;
if (diff_rel > 0.20f) valid = false;
}
if (valid) {
RR_buffer[RR_index] = (unsigned int)RR;
RR_index = (RR_index + 1) % MAX_RR;
if (RR_count < MAX_RR) RR_count++;
RR_anterior = RR;
}
}
}
ultim_R_ms = acum_ms;
}
}
// ============================================================
// PROCESARE GSR
// ============================================================
void proceseazaGSR(unsigned long acum_ms) {
int adc = analogRead(PIN_GSR);
// Calibrare in primele 30 sec
if (!GSR_calibrat) {
ADC_calib_suma += adc;
ADC_calib_n++;
return;
}
// Conversie liniara empirica
float gsr_brut = 5.0f + ((float)ADC_GSR_baseline - (float)adc) * SCALA_GSR;
if (gsr_brut < 0.5f) gsr_brut = 0.5f;
if (gsr_brut > 30.0f) gsr_brut = 30.0f;
// Trece-jos 5 Hz
float gsr_filt = gsr_lp_prev + ALPHA_GSR * (gsr_brut - gsr_lp_prev);
gsr_lp_prev = gsr_filt;
// SCL = media mobila pe 10 sec
SCL_suma -= SCL_buffer[SCL_index];
SCL_buffer[SCL_index] = gsr_filt;
SCL_suma += gsr_filt;
SCL_index = (SCL_index + 1) % FER_SCL;
SCL_curent = SCL_suma / FER_SCL;
SCL_acumulator += SCL_curent;
SCL_n++;
// SCR = componenta fazica
float SCR = gsr_filt - SCL_curent;
// Detectie SCR
if (SCR > SCR_prag && SCR_prev <= SCR_prag &&
(acum_ms - ultim_SCR_ms) > REFRACTAR_SCR_MS) {
SCR_count++;
SCR_amp_suma += SCR;
ultim_SCR_ms = acum_ms;
}
SCR_prev = SCR;
}
// ============================================================
// FINALIZARE CALIBRARE GSR
// ============================================================
void finalizeazaCalibrareGSR() {
if (ADC_calib_n > 10) {
ADC_GSR_baseline = ADC_calib_suma / ADC_calib_n;
// Daca ADC baseline e mare (>500), scala e inversata (versiune noua Grove)
if (ADC_GSR_baseline > 500) {
SCALA_GSR = -0.1f;
} else {
SCALA_GSR = 0.1f;
}
SCR_prag = 0.1f;
}
GSR_calibrat = true;
Serial.println();
Serial.print(F(">>> Calibrare GSR finalizata. ADC baseline = "));
Serial.print(ADC_GSR_baseline, 0);
Serial.print(F(", SCALA = "));
Serial.println(SCALA_GSR, 3);
Serial.println();
}
// ============================================================
// AFISARE METRICI CONTINUU
// ============================================================
void afiseazaMetrici(unsigned long acum_ms) {
unsigned long timp_total = (acum_ms - start_ms) / 1000; // secunde
Serial.print(F("[t="));
Serial.print(timp_total);
Serial.print(F("s] "));
// ====== HRV ======
if (RR_count < 3) {
Serial.print(F("HR=-- (astept R-peaks) "));
} else {
// Mediana RR (pentru filtrare suplimentara)
float med_RR = medianaRR();
float suma = 0;
int n_valid = 0;
for (int i = 0; i < RR_count; i++) {
float rr = RR_buffer[i];
if (rr > 0.8f * med_RR && rr < 1.2f * med_RR) {
suma += rr;
n_valid++;
}
}
if (n_valid >= 3) {
float media_RR = suma / n_valid;
float HR = 60000.0f / media_RR;
// SDNN
float ss = 0;
for (int i = 0; i < RR_count; i++) {
float rr = RR_buffer[i];
if (rr > 0.8f * med_RR && rr < 1.2f * med_RR) {
ss += (rr - media_RR) * (rr - media_RR);
}
}
float SDNN = sqrt(ss / n_valid);
// RMSSD si pNN50
float ss_dif = 0;
int n_dif = 0;
int NN50 = 0;
float rr_prev = -1;
for (int i = 0; i < RR_count; i++) {
float rr = RR_buffer[i];
if (rr > 0.8f * med_RR && rr < 1.2f * med_RR) {
if (rr_prev > 0) {
float d = rr - rr_prev;
ss_dif += d * d;
if (fabs(d) > 50) NN50++;
n_dif++;
}
rr_prev = rr;
}
}
float RMSSD = (n_dif > 0) ? sqrt(ss_dif / n_dif) : 0;
float pNN50 = (n_dif > 0) ? (100.0f * NN50 / n_dif) : 0;
Serial.print(F("HR=")); Serial.print(HR, 0); Serial.print(F("bpm "));
Serial.print(F("SDNN=")); Serial.print(SDNN, 0); Serial.print(F("ms "));
Serial.print(F("RMSSD=")); Serial.print(RMSSD, 0); Serial.print(F("ms "));
Serial.print(F("pNN50=")); Serial.print(pNN50, 1); Serial.print(F("% "));
} else {
Serial.print(F("HR=-- "));
}
}
// ====== GSR ======
if (!GSR_calibrat) {
Serial.print(F("GSR=calibrare..."));
} else {
float SCL_mediu = (SCL_n > 0) ? (SCL_acumulator / SCL_n) : 0;
float durata_min = (timp_total > 30) ? (timp_total - 30) / 60.0f : 0.001f;
float SCR_frecv = SCR_count / durata_min;
float SCR_amed = (SCR_count > 0) ? (SCR_amp_suma / SCR_count) : 0;
Serial.print(F("SCL=")); Serial.print(SCL_curent, 2); Serial.print(F("uS "));
Serial.print(F("SCLmed=")); Serial.print(SCL_mediu, 2); Serial.print(F("uS "));
Serial.print(F("nSCR=")); Serial.print(SCR_count); Serial.print(F(" "));
Serial.print(F("frSCR=")); Serial.print(SCR_frecv, 1); Serial.print(F("/min "));
Serial.print(F("ampSCR=")); Serial.print(SCR_amed, 2); Serial.print(F("uS"));
}
Serial.println();
}
// ============================================================
// FUNCTII AJUTATOARE
// ============================================================
float medianaRR() {
unsigned int copie[MAX_RR];
for (int i = 0; i < RR_count; i++) copie[i] = RR_buffer[i];
for (int i = 0; i < RR_count - 1; i++) {
for (int j = 0; j < RR_count - 1 - i; j++) {
if (copie[j] > copie[j+1]) {
unsigned int tmp = copie[j];
copie[j] = copie[j+1];
copie[j+1] = tmp;
}
}
}
if (RR_count % 2 == 1) {
return copie[RR_count / 2];
} else {
return (copie[RR_count/2 - 1] + copie[RR_count/2]) / 2.0f;
}
}
`
Show these values and describe what you expect the value ranges to be.
For GSR values I managed to make this code: `//
============================================================
// GSR COMPLET (SCL + SCR)
// ============================================================
const int PIN_GSR = A1;
// ===== TIMP =====
const unsigned long PERIOADA_GSR_US = 100000; // 10 Hz
unsigned long ultim_us_gsr = 0;
unsigned long start_ms = 0;
unsigned long ultim_afisare_ms = 0;
// ===== FILTRARE =====
float gsr_lp_prev = 0;
const float ALPHA_GSR = 0.76f;
// ===== CALIBRARE =====
const unsigned long DURATA_CALIBRARE_MS = 30000UL;
bool GSR_calibrat = false;
float ADC_GSR_baseline = 0;
float ADC_calib_suma = 0;
unsigned long ADC_calib_n = 0;
float SCALA_GSR = 0.1f;
// ===== SCL =====
const int FER_SCL = 100;
float SCL_buffer[FER_SCL];
int SCL_index = 0;
float SCL_suma = 0;
float SCL_curent = 0;
float SCL_acumulator = 0;
unsigned long SCL_n = 0;
// ===== SCR =====
float SCR_prag = 0.1f;
const unsigned long REFRACTAR_SCR_MS = 1000;
unsigned long ultim_SCR_ms = 0;
float SCR_prev = 0;
unsigned int SCR_count = 0;
float SCR_amp_suma = 0;
// ================= SETUP =================
void setup() {
Serial.begin(9600);
for (int i = 0; i < FER_SCL; i++) {
SCL_buffer[i] = 0;
}
delay(3000);
start_ms = millis();
ultim_afisare_ms = millis();
Serial.println("START GSR - calibrare 30s");
}
// ================= LOOP =================
void loop() {
unsigned long acum_us = micros();
unsigned long acum_ms = millis();
// sampling GSR
if (acum_us - ultim_us_gsr >= PERIOADA_GSR_US) {
ultim_us_gsr = acum_us;
proceseazaGSR(acum_ms);
}
// final calibrare
if (!GSR_calibrat && (acum_ms - start_ms) >= DURATA_CALIBRARE_MS) {
finalizeazaCalibrareGSR();
}
// afisare la 5 sec
if (acum_ms - ultim_afisare_ms >= 5000) {
ultim_afisare_ms = acum_ms;
afiseazaGSR(acum_ms);
}
}
// ================= PROCESARE =================
void proceseazaGSR(unsigned long acum_ms) {
int adc = analogRead(PIN_GSR);
if (!GSR_calibrat) {
ADC_calib_suma += adc;
ADC_calib_n++;
return;
}
float gsr_brut = 5.0f + ((float)ADC_GSR_baseline - (float)adc) * SCALA_GSR;
if (gsr_brut < 0.5f) gsr_brut = 0.5f;
if (gsr_brut > 30.0f) gsr_brut = 30.0f;
// filtru
float gsr_filt = gsr_lp_prev + ALPHA_GSR * (gsr_brut - gsr_lp_prev);
gsr_lp_prev = gsr_filt;
// SCL (medie mobila)
SCL_suma -= SCL_buffer[SCL_index];
SCL_buffer[SCL_index] = gsr_filt;
SCL_suma += gsr_filt;
SCL_index = (SCL_index + 1) % FER_SCL;
SCL_curent = SCL_suma / FER_SCL;
SCL_acumulator += SCL_curent;
SCL_n++;
// SCR
float SCR = gsr_filt - SCL_curent;
if (SCR > SCR_prag && SCR_prev <= SCR_prag &&
(acum_ms - ultim_SCR_ms) > REFRACTAR_SCR_MS) {
SCR_count++;
SCR_amp_suma += SCR;
ultim_SCR_ms = acum_ms;
}
SCR_prev = SCR;
}
// ================= CALIBRARE =================
void finalizeazaCalibrareGSR() {
ADC_GSR_baseline = ADC_calib_suma / ADC_calib_n;
if (ADC_GSR_baseline > 500) {
SCALA_GSR = -0.1f;
} else {
SCALA_GSR = 0.1f;
}
GSR_calibrat = true;
Serial.print("Calibrare gata. Baseline=");
Serial.println(ADC_GSR_baseline);
}
// ================= AFISARE =================
void afiseazaGSR(unsigned long acum_ms) {
if (!GSR_calibrat) {
Serial.println("Calibrare...");
return;
}
float timp_total = (acum_ms - start_ms) / 1000.0;
float durata_min = (timp_total > 30) ? (timp_total - 30) / 60.0 : 0.001;
float SCL_med = (SCL_n > 0) ? (SCL_acumulator / SCL_n) : 0;
float SCR_frecv = SCR_count / durata_min;
float SCR_amp_med = (SCR_count > 0) ? (SCR_amp_suma / SCR_count) : 0;
Serial.print("SCL=");
Serial.print(SCL_curent, 2);
Serial.print(" uS | SCLmed=");
Serial.print(SCL_med, 2);
Serial.print(" | nSCR=");
Serial.print(SCR_count);
Serial.print(" | frSCR=");
Serial.print(SCR_frecv, 2);
Serial.print("/min | ampSCR=");
Serial.print(SCR_amp_med, 2);
Serial.println(" uS");
}`,
but I still have problems with the code for HRV
I think you should test them separately with separate codes first. If both works well separately, then you can cascade your codes and components. If you're using Groove GSR sensor, you will notice that it has an on-board small potentiometer. You need may need to calibrate that.
I tested the Grove sensor, the code for it works ok, for HRV it doesn't work at all, can someone help me with a code that displays those metrics?
You have not explained what parameters are expected.
These probes read a value from 0 to 1023. That's it.
You must verify:
- The data wire connecting the probe to the Arduino is correct (two devices = two probes)
- The sensor wires.
- The sensor amplifiers (ECG and GSR boards).
You say all the results are bad (but do not show what is "good")... so f you say the wiring is right, then the problem is in your convoluted calculations. If you say your calculations are correct, then the problem is in your device wiring (one wire to the Arduino and probe wires to the person).
Test this by removing the probe wires from the Arduino and run the sketch. If the results are "the same" then your sensors are the problem.
What HRV? Your topic title says ECG and GSR.
Yes, but from the ECG signal, using R peaks detection, the heart rate variability (HRV) parameters are calculated, namely RMSSD, SDNN and pNN50
I convert the values output by the sensors into medically interpretable values. The code calculates my heart rate correctly, but the rest of the parameters do not come out according to the physiological ranges. The sensors do not have a problem, but I do not know how to make the code to filter the signal and manage to correctly detect all the R waves so that the parameters can be calculated correctly
Third try: What ranges?
What's an "R wave"? (define before abbreviation)
What (HRV) code? You only gave GSR code.
I have edited your GSR code to add many "debug" statements. I have nothing connected to the Arduino Nano, meaning, all the Analog Inputs are "random" ranging between 0 and 1023, but mostly between 200 and 800. The output looks like this:
START GSR - calibrare 30s // '.' = procGSR // '<F>' = finGSR // '<A>' = afiGSR // # = Calibrare count
proceseazaGSR .................................................
afiseazaGSR
Calibrare...1..................................................<A>
Calibrare...2..................................................<A>
Calibrare...3..................................................<A>
Calibrare...4..................................................<A>
Calibrare...5.................................................
finalizeazaCalibrareGSR Calibrare gata. Baseline=486.47
<A>
SCL=0.00 uS | SCLmed=0.00 | nSCR=0 | frSCR=0.00/min | ampSCR=0.00 uS
..................................................<A>
SCL=1.07 uS | SCLmed=0.36 | nSCR=2 | frSCR=24.00/min | ampSCR=0.35 uS
..................................................<A>
SCL=3.65 uS | SCLmed=1.54 | nSCR=5 | frSCR=30.00/min | ampSCR=0.70 uS
..................................................<A>
SCL=3.51 uS | SCLmed=2.20 | nSCR=6 | frSCR=24.00/min | ampSCR=0.94 uS
..................................................<A>
SCL=1.37 uS | SCLmed=2.15 | nSCR=7 | frSCR=21.00/min | ampSCR=0.92 uS
..................................................<A>
SCL=1.06 uS | SCLmed=2.04 | nSCR=9 | frSCR=21.60/min | ampSCR=1.28 uS
..................................................<A>
SCL=2.17 uS | SCLmed=1.98 | nSCR=12 | frSCR=24.00/min | ampSCR=1.51 uS
The edited/debug code is here...
// ============================================================
// GSR COMPLET (SCL + SCR)
// ============================================================
const int PIN_GSR = A1;
// ===== TIMP =====
const unsigned long PERIOADA_GSR_US = 100000; // 10 Hz
unsigned long ultim_us_gsr = 0;
unsigned long start_ms = 0;
unsigned long ultim_afisare_ms = 0;
// ===== FILTRARE =====
float gsr_lp_prev = 0;
const float ALPHA_GSR = 0.76f;
// ===== CALIBRARE =====
const unsigned long DURATA_CALIBRARE_MS = 30000UL;
bool GSR_calibrat = false;
float ADC_GSR_baseline = 0;
float ADC_calib_suma = 0;
unsigned long ADC_calib_n = 0;
float SCALA_GSR = 0.1f;
// ===== SCL =====
const int FER_SCL = 100;
float SCL_buffer[FER_SCL];
int SCL_index = 0;
float SCL_suma = 0;
float SCL_curent = 0;
float SCL_acumulator = 0;
unsigned long SCL_n = 0;
// ===== SCR =====
float SCR_prag = 0.1f;
const unsigned long REFRACTAR_SCR_MS = 1000;
unsigned long ultim_SCR_ms = 0;
float SCR_prev = 0;
unsigned int SCR_count = 0;
float SCR_amp_suma = 0;
bool proceseazaGSRflag, afiseazaGSRflag, finalizeazaCalibrareGSRflag;
int calibrare_count;
// ================= SETUP =================
void setup() {
Serial.begin(115200);
for (int i = 0; i < FER_SCL; i++) {
SCL_buffer[i] = 0;
}
// delay(3000);
start_ms = millis();
ultim_afisare_ms = millis();
Serial.println("START GSR - calibrare 30s // '.' = procGSR // '<F>' = finGSR // '<A>' = afiGSR // # = Calibrare count");
}
// ================= LOOP =================
void loop() {
unsigned long acum_us = micros();
unsigned long acum_ms = millis();
// sampling GSR
if (acum_us - ultim_us_gsr >= PERIOADA_GSR_US) {
ultim_us_gsr = acum_us;
proceseazaGSR(acum_ms);
}
// final calibrare
if (!GSR_calibrat && (acum_ms - start_ms) >= DURATA_CALIBRARE_MS) {
finalizeazaCalibrareGSR();
}
// afisare la 5 sec
if (acum_ms - ultim_afisare_ms >= 5000) {
ultim_afisare_ms = acum_ms;
afiseazaGSR(acum_ms);
}
}
// ================= PROCESARE =================
void proceseazaGSR(unsigned long acum_ms) {
if (!proceseazaGSRflag) {
Serial.print("proceseazaGSR ");
proceseazaGSRflag = 1;
} else {
Serial.print(".");
}
int adc = analogRead(PIN_GSR);
if (!GSR_calibrat) {
ADC_calib_suma += adc;
ADC_calib_n++;
return;
}
float gsr_brut = 5.0f + ((float)ADC_GSR_baseline - (float)adc) * SCALA_GSR;
if (gsr_brut < 0.5f) gsr_brut = 0.5f;
if (gsr_brut > 30.0f) gsr_brut = 30.0f;
// filtru
float gsr_filt = gsr_lp_prev + ALPHA_GSR * (gsr_brut - gsr_lp_prev);
gsr_lp_prev = gsr_filt;
// SCL (medie mobila)
SCL_suma -= SCL_buffer[SCL_index];
SCL_buffer[SCL_index] = gsr_filt;
SCL_suma += gsr_filt;
SCL_index = (SCL_index + 1) % FER_SCL;
SCL_curent = SCL_suma / FER_SCL;
SCL_acumulator += SCL_curent;
SCL_n++;
// SCR
float SCR = gsr_filt - SCL_curent;
if (SCR > SCR_prag && SCR_prev <= SCR_prag &&
(acum_ms - ultim_SCR_ms) > REFRACTAR_SCR_MS) {
SCR_count++;
SCR_amp_suma += SCR;
ultim_SCR_ms = acum_ms;
}
SCR_prev = SCR;
}
// ================= CALIBRARE =================
void finalizeazaCalibrareGSR() {
if (!finalizeazaCalibrareGSRflag) {
Serial.print("\nfinalizeazaCalibrareGSR ");
finalizeazaCalibrareGSRflag = 1;
} else {
Serial.print("<F>");
}
ADC_GSR_baseline = ADC_calib_suma / ADC_calib_n;
if (ADC_GSR_baseline > 500) {
SCALA_GSR = -0.1f;
} else {
SCALA_GSR = 0.1f;
}
GSR_calibrat = true;
Serial.print("Calibrare gata. Baseline=");
Serial.println(ADC_GSR_baseline);
}
// ================= AFISARE =================
void afiseazaGSR(unsigned long acum_ms) {
if (!afiseazaGSRflag) {
Serial.print("\nafiseazaGSR ");
afiseazaGSRflag = 1;
} else {
Serial.print("<A>");
}
if (!GSR_calibrat) {
Serial.print("\nCalibrare...");
Serial.print(++calibrare_count);
return;
}
float timp_total = (acum_ms - start_ms) / 1000.0;
float durata_min = (timp_total > 30) ? (timp_total - 30) / 60.0 : 0.001;
float SCL_med = (SCL_n > 0) ? (SCL_acumulator / SCL_n) : 0;
float SCR_frecv = SCR_count / durata_min;
float SCR_amp_med = (SCR_count > 0) ? (SCR_amp_suma / SCR_count) : 0;
Serial.print("\nSCL=");
Serial.print(SCL_curent, 2);
Serial.print(" uS | SCLmed=");
Serial.print(SCL_med, 2);
Serial.print(" | nSCR=");
Serial.print(SCR_count);
Serial.print(" | frSCR=");
Serial.print(SCR_frecv, 2);
Serial.print("/min | ampSCR=");
Serial.print(SCR_amp_med, 2);
Serial.println(" uS");
}
SDNN— Standard Deviation of Normal-to-Normal Intervals ~30 – 70 ms
RMSSD — Root Mean Square of Successive Differences ~20 – 50 ms
pNN50 — percentage of Successive NN Intervals that Differ by More Than 50 ms ~3 – 25%
In specialized studies, RMSSD values are lower than SDNN values, and for me they are higher: ```
HR=76bpm SDNN=28ms RMSSD=32ms pNN50=12.5%
// ============================================================
// 1. CONFIGURARI GENERALE
// ============================================================
const int PIN_ECG = A0;
const int PIN_GSR = A1;
const int PIN_LO_PLUS = 10;
const int PIN_LO_MINUS = 11;
const unsigned long PERIOADA_ECG_US = 4000; // 250 Hz
const unsigned long PERIOADA_GSR_US = 100000; // 10 Hz
const unsigned long INTERVAL_AFISARE_MS = 10000UL;
unsigned long ultim_afisare_ms = 0;
unsigned long start_ms = 0;
const bool DEBUG_ECG = true;
unsigned long ultim_debug_ecg_ms = 0;
// ============================================================
// 2. FILTRARE ECG
// ============================================================
float ecg_hp_prev_in = 0;
float ecg_hp_prev_out = 0;
const float ALPHA_HP = 0.987f; // trece-sus 0.5 Hz
float ecg_lp_prev = 0;
const float ALPHA_LP = 0.36f; // trece-jos 40 Hz
float ecg_prev1 = 0;
float ecg_prev2 = 0;
// ============================================================
// 3. DETECTIE R-PEAK (Pan-Tompkins cu integrare)
// ============================================================
const int FER_INT = 20;
float int_buffer[FER_INT];
int int_index = 0;
float int_suma = 0;
float ecg_integrat = 0;
float prag_R = 0.0f;
float ecg_max_recent = 0.0f;
const float COEF_PRAG = 0.35f;
const unsigned long REFRACTAR_MS = 350;
unsigned long ultim_R_ms = 0;
const int MAX_RR = 200;
unsigned int RR_buffer[MAX_RR];
int RR_index = 0;
int RR_count = 0;
// ============================================================
// 4. GSR cu CALIBRARE EMPIRICA
// ============================================================
float gsr_lp_prev = 0;
const float ALPHA_GSR = 0.76f;
const unsigned long DURATA_CALIBRARE_MS = 30000UL;
bool GSR_calibrat = false;
float ADC_GSR_baseline = 0;
float ADC_calib_suma = 0;
unsigned long ADC_calib_n = 0;
float SCALA_GSR = 0.1f;
const int FER_SCL = 100;
float SCL_buffer[FER_SCL];
int SCL_index = 0;
float SCL_suma = 0;
float SCL_curent = 0;
float SCL_acumulator = 0;
unsigned long SCL_n = 0;
float SCR_prag = 0.1f;
const unsigned long REFRACTAR_SCR_MS = 1000;
unsigned long ultim_SCR_ms = 0;
float SCR_prev = 0;
unsigned int SCR_count = 0;
float SCR_amp_suma = 0;
// ============================================================
// 5. VARIABILE TIMP
// ============================================================
unsigned long ultim_us_ecg = 0;
unsigned long ultim_us_gsr = 0;
// ============================================================
// SETUP
// ============================================================
void setup() {
Serial.begin(9600);
pinMode(PIN_LO_PLUS, INPUT);
pinMode(PIN_LO_MINUS, INPUT);
for (int i = 0; i < FER_SCL; i++) SCL_buffer[i] = 0;
for (int i = 0; i < FER_INT; i++) int_buffer[i] = 0;
Serial.println(F("============================================"));
Serial.println(F(" MONITOR STRES - HRV + GSR (continuu) "));
Serial.println(F("============================================"));
Serial.println(F("Plasati electrozii FERM."));
Serial.println(F("Stati nemiscat - mai ales primele 30 sec!"));
Serial.println(F("Start in 5 secunde..."));
delay(5000);
start_ms = millis();
ultim_afisare_ms = millis();
ultim_debug_ecg_ms = millis();
ultim_us_ecg = micros();
ultim_us_gsr = micros();
Serial.println(F(">>> START - calibrare GSR primele 30 sec"));
}
// ============================================================
// LOOP PRINCIPAL
// ============================================================
void loop() {
unsigned long acum_us = micros();
unsigned long acum_ms = millis();
// ECG la 250 Hz
if (digitalRead(PIN_LO_PLUS) == 0 && digitalRead(PIN_LO_MINUS) == 0) {
if (acum_us - ultim_us_ecg >= PERIOADA_ECG_US) {
ultim_us_ecg = acum_us;
proceseazaECG(acum_ms);
}
}
// GSR la 10 Hz
if (acum_us - ultim_us_gsr >= PERIOADA_GSR_US) {
ultim_us_gsr = acum_us;
proceseazaGSR(acum_ms);
}
// Calibrare GSR la 30 sec
if (!GSR_calibrat && acum_ms - start_ms >= DURATA_CALIBRARE_MS) {
finalizeazaCalibrareGSR();
}
// DEBUG ECG la fiecare 3 sec
if (DEBUG_ECG && acum_ms - ultim_debug_ecg_ms >= 3000) {
ultim_debug_ecg_ms = acum_ms;
Serial.print(F(" [ECG_DBG] integrat="));
Serial.print(ecg_integrat, 6);
Serial.print(F(" max="));
Serial.print(ecg_max_recent, 6);
Serial.print(F(" prag="));
Serial.print(prag_R, 6);
Serial.print(F(" RR_count="));
Serial.println(RR_count);
}
// Afisare metrici la fiecare 10 sec
if (acum_ms - ultim_afisare_ms >= INTERVAL_AFISARE_MS) {
ultim_afisare_ms = acum_ms;
afiseazaMetrici(acum_ms);
}
}
// ============================================================
// PROCESARE ECG (Pan-Tompkins)
// ============================================================
void proceseazaECG(unsigned long acum_ms) {
float ecg_brut = analogRead(PIN_ECG) * (5.0f / 1023.0f);
// Trece-sus 0.5 Hz
float ecg_hp = ALPHA_HP * (ecg_hp_prev_out + ecg_brut - ecg_hp_prev_in);
ecg_hp_prev_in = ecg_brut;
ecg_hp_prev_out = ecg_hp;
// Trece-jos 40 Hz
float ecg_filt = ecg_lp_prev + ALPHA_LP * (ecg_hp - ecg_lp_prev);
ecg_lp_prev = ecg_filt;
// Derivata
float deriv = ecg_filt - ecg_prev2;
ecg_prev2 = ecg_prev1;
ecg_prev1 = ecg_filt;
// Patrat
float ecg_pat = deriv * deriv;
// INTEGRARE pe fereastra 80 ms
int_suma -= int_buffer[int_index];
int_buffer[int_index] = ecg_pat;
int_suma += ecg_pat;
int_index = (int_index + 1) % FER_INT;
ecg_integrat = int_suma / FER_INT;
// Prag adaptiv cu PROTECTIE ANTI-ARTEFACT MISCARE
// (limitam cresterea brusca a maximului la max 1.5x valoarea curenta)
if (ecg_integrat > ecg_max_recent) {
float nou_max = ecg_integrat;
if (ecg_max_recent > 0 && nou_max > 1.5f * ecg_max_recent) {
nou_max = 1.5f * ecg_max_recent;
}
ecg_max_recent = nou_max;
} else {
ecg_max_recent *= 0.9995f;
}
prag_R = COEF_PRAG * ecg_max_recent;
// Detectie R-peak SIMPLA (fara filtrare RR succesive)
// Filtrarea artefactelor se face la afisare, prin mediana ±20%
if (ecg_integrat > prag_R && (acum_ms - ultim_R_ms) > REFRACTAR_MS) {
if (ultim_R_ms > 0) {
unsigned long RR = acum_ms - ultim_R_ms;
if (RR > 300 && RR < 2000) {
RR_buffer[RR_index] = (unsigned int)RR;
RR_index = (RR_index + 1) % MAX_RR;
if (RR_count < MAX_RR) RR_count++;
}
}
ultim_R_ms = acum_ms;
}
}
// ============================================================
// PROCESARE GSR
// ============================================================
void proceseazaGSR(unsigned long acum_ms) {
int adc = analogRead(PIN_GSR);
if (!GSR_calibrat) {
ADC_calib_suma += adc;
ADC_calib_n++;
return;
}
// Conversie liniara empirica
float gsr_brut = 5.0f + ((float)ADC_GSR_baseline - (float)adc) * SCALA_GSR;
if (gsr_brut < 0.5f) gsr_brut = 0.5f;
if (gsr_brut > 30.0f) gsr_brut = 30.0f;
// Trece-jos 5 Hz
float gsr_filt = gsr_lp_prev + ALPHA_GSR * (gsr_brut - gsr_lp_prev);
gsr_lp_prev = gsr_filt;
// SCL = media mobila pe 10 sec
SCL_suma -= SCL_buffer[SCL_index];
SCL_buffer[SCL_index] = gsr_filt;
SCL_suma += gsr_filt;
SCL_index = (SCL_index + 1) % FER_SCL;
SCL_curent = SCL_suma / FER_SCL;
SCL_acumulator += SCL_curent;
SCL_n++;
// SCR = componenta fazica
float SCR = gsr_filt - SCL_curent;
// Detectie SCR
if (SCR > SCR_prag && SCR_prev <= SCR_prag &&
(acum_ms - ultim_SCR_ms) > REFRACTAR_SCR_MS) {
SCR_count++;
SCR_amp_suma += SCR;
ultim_SCR_ms = acum_ms;
}
SCR_prev = SCR;
}
// ============================================================
// FINALIZARE CALIBRARE GSR
// ============================================================
void finalizeazaCalibrareGSR() {
if (ADC_calib_n > 5) {
ADC_GSR_baseline = ADC_calib_suma / ADC_calib_n;
if (ADC_GSR_baseline > 500) {
SCALA_GSR = -0.1f;
} else {
SCALA_GSR = 0.1f;
}
SCR_prag = 0.1f;
}
GSR_calibrat = true;
Serial.println();
Serial.print(F(">>> Calibrare GSR finalizata. ADC baseline = "));
Serial.print(ADC_GSR_baseline, 0);
Serial.print(F(", SCALA = "));
Serial.println(SCALA_GSR, 3);
Serial.println();
}
// ============================================================
// AFISARE METRICI CONTINUU
// ============================================================
void afiseazaMetrici(unsigned long acum_ms) {
unsigned long timp_total = (acum_ms - start_ms) / 1000;
Serial.print(F("[t="));
Serial.print(timp_total);
Serial.print(F("s] "));
// ====== HRV ======
if (RR_count < 3) {
Serial.print(F("HR=-- (astept R-peaks) "));
} else {
// Mediana RR (filtrare ±20% - identic cu MATLAB)
float med_RR = medianaRR();
float suma = 0;
int n_valid = 0;
for (int i = 0; i < RR_count; i++) {
float rr = RR_buffer[i];
if (rr > 0.8f * med_RR && rr < 1.2f * med_RR) {
suma += rr;
n_valid++;
}
}
if (n_valid >= 3) {
float media_RR = suma / n_valid;
float HR = 60000.0f / media_RR;
// SDNN
float ss = 0;
for (int i = 0; i < RR_count; i++) {
float rr = RR_buffer[i];
if (rr > 0.8f * med_RR && rr < 1.2f * med_RR) {
ss += (rr - media_RR) * (rr - media_RR);
}
}
float SDNN = sqrt(ss / n_valid);
// RMSSD si pNN50
float ss_dif = 0;
int n_dif = 0;
int NN50 = 0;
float rr_prev = -1;
for (int i = 0; i < RR_count; i++) {
float rr = RR_buffer[i];
if (rr > 0.8f * med_RR && rr < 1.2f * med_RR) {
if (rr_prev > 0) {
float d = rr - rr_prev;
ss_dif += d * d;
if (fabs(d) > 50) NN50++;
n_dif++;
}
rr_prev = rr;
}
}
float RMSSD = (n_dif > 0) ? sqrt(ss_dif / n_dif) : 0;
float pNN50 = (n_dif > 0) ? (100.0f * NN50 / n_dif) : 0;
Serial.print(F("HR=")); Serial.print(HR, 0); Serial.print(F("bpm "));
Serial.print(F("SDNN=")); Serial.print(SDNN, 0); Serial.print(F("ms "));
Serial.print(F("RMSSD=")); Serial.print(RMSSD, 0); Serial.print(F("ms "));
Serial.print(F("pNN50=")); Serial.print(pNN50, 1); Serial.print(F("% "));
} else {
Serial.print(F("HR=-- "));
}
}
// ====== GSR ======
if (!GSR_calibrat) {
Serial.print(F("GSR=calibrare..."));
} else {
float SCL_mediu = (SCL_n > 0) ? (SCL_acumulator / SCL_n) : 0;
float durata_min = (timp_total > 30) ? (timp_total - 30) / 60.0f : 0.001f;
float SCR_frecv = SCR_count / durata_min;
float SCR_amed = (SCR_count > 0) ? (SCR_amp_suma / SCR_count) : 0;
Serial.print(F("SCL=")); Serial.print(SCL_curent, 2); Serial.print(F("uS "));
Serial.print(F("SCLmed=")); Serial.print(SCL_mediu, 2); Serial.print(F("uS "));
Serial.print(F("nSCR=")); Serial.print(SCR_count); Serial.print(F(" "));
Serial.print(F("frSCR=")); Serial.print(SCR_frecv, 1); Serial.print(F("/min "));
Serial.print(F("ampSCR=")); Serial.print(SCR_amed, 2); Serial.print(F("uS"));
}
Serial.println();
}
// ============================================================
// FUNCTII AJUTATOARE
// ============================================================
float medianaRR() {
unsigned int copie[MAX_RR];
for (int i = 0; i < RR_count; i++) copie[i] = RR_buffer[i];
for (int i = 0; i < RR_count - 1; i++) {
for (int j = 0; j < RR_count - 1 - i; j++) {
if (copie[j] > copie[j+1]) {
unsigned int tmp = copie[j];
copie[j] = copie[j+1];
copie[j+1] = tmp;
}
}
}
if (RR_count % 2 == 1) {
return copie[RR_count / 2];
} else {
return (copie[RR_count/2 - 1] + copie[RR_count/2]) / 2.0f;
}
}
The R wave is the positive peak in the QRS complex on an ECG. It marks the moment when the heart's ventricles depolarize, meaning they become electrically activated to contract and pump blood.
Thank you for the edited code for GSR signal. I have uploaded all the code that I ran below. It also contains the GSR, but now it works for me for the GSR, what still doesn't display the corresponding values is the ECG part.
Is this comment pertaining to your code in Post #15?
Also... concerning "unexpected values"... looking at the data in Post #14, the nSCR is constantly climbing.
What should this output look like? Here is my output of Post #15 code, using a stand-alone Arduino Nano (no devices connected)...
============================================
MONITOR STRES - HRV + GSR (continuu)
============================================
Plasati electrozii FERM.
Stati nemiscat - mai ales primele 30 sec!
Start in 5 secunde...
>>> START - calibrare GSR primele 30 sec
[ECG_DBG] integrat=0.010486 max=0.516248 prag=0.180687 RR_count=0
[ECG_DBG] integrat=0.014616 max=0.355305 prag=0.124357 RR_count=0
[ECG_DBG] integrat=0.030020 max=0.244537 prag=0.085588 RR_count=0
[t=10s] HR=-- (astept R-peaks) GSR=calibrare...
[ECG_DBG] integrat=0.005062 max=0.168301 prag=0.058905 RR_count=0
[ECG_DBG] integrat=0.003640 max=0.115832 prag=0.040541 RR_count=0
[ECG_DBG] integrat=0.014714 max=0.079721 prag=0.027902 RR_count=0
[t=20s] HR=-- (astept R-peaks) GSR=calibrare...
[ECG_DBG] integrat=0.007324 max=0.054867 prag=0.019204 RR_count=2
[ECG_DBG] integrat=0.008883 max=0.038187 prag=0.013365 RR_count=8
[ECG_DBG] integrat=0.013028 max=0.026282 prag=0.009199 RR_count=16
>>> Calibrare GSR finalizata. ADC baseline = 509, SCALA = -0.100
[t=30s] HR=162bpm SDNN=29ms RMSSD=42ms pNN50=18.7% SCL=0.00uS SCLmed=0.00uS nSCR=0 frSCR=0.0/min ampSCR=0.00uS
[ECG_DBG] integrat=0.006449 max=0.025646 prag=0.008976 RR_count=24
[ECG_DBG] integrat=0.018424 max=0.024091 prag=0.008432 RR_count=32
[ECG_DBG] integrat=0.013325 max=0.027620 prag=0.009667 RR_count=41
[ECG_DBG] integrat=0.004996 max=0.027182 prag=0.009514 RR_count=48
[t=40s] HR=165bpm SDNN=18ms RMSSD=25ms pNN50=5.3% SCL=4.80uS SCLmed=1.76uS nSCR=3 frSCR=18.0/min ampSCR=0.91uS
[ECG_DBG] integrat=0.013094 max=0.029763 prag=0.010417 RR_count=56
[ECG_DBG] integrat=0.018963 max=0.022167 prag=0.007758 RR_count=64
[ECG_DBG] integrat=0.010821 max=0.025973 prag=0.009090 RR_count=72
[t=50s] HR=166bpm SDNN=17ms RMSSD=24ms pNN50=3.3% SCL=7.77uS SCLmed=4.62uS nSCR=8 frSCR=24.0/min ampSCR=1.44uS
[ECG_DBG] integrat=0.008552 max=0.031850 prag=0.011148 RR_count=79
[ECG_DBG] integrat=0.011778 max=0.027088 prag=0.009481 RR_count=87
[ECG_DBG] integrat=0.022863 max=0.037694 prag=0.013193 RR_count=95
[t=60s] HR=166bpm SDNN=15ms RMSSD=22ms pNN50=2.5% SCL=5.84uS SCLmed=5.16uS nSCR=12 frSCR=24.0/min ampSCR=1.41uS
[ECG_DBG] integrat=0.007848 max=0.026151 prag=0.009153 RR_count=102
[ECG_DBG] integrat=0.006949 max=0.021344 prag=0.007470 RR_count=110
[ECG_DBG] integrat=0.011209 max=0.025065 prag=0.008773 RR_count=118
[ECG_DBG] integrat=0.015186 max=0.029834 prag=0.010442 RR_count=127
[t=70s] HR=166bpm SDNN=16ms RMSSD=23ms pNN50=4.9% SCL=6.37uS SCLmed=5.40uS nSCR=19 frSCR=28.5/min ampSCR=1.44uS
I see "ECG_DBG".... what is missing?
Yes, in post #15 I gave the standard ranges for metrics calculated in code.
Okay. You see my output in Post 18 with a "standalone" Arduino. Do those numbers translate?
Some boards do not generate "average" random numbers. This Arduino Nano seems to average 413 (perfect would b 512... out of 1024). At about 40 readings, the min/med/max seem to have found home.
Count = 0 | read = 196 | min= 196 | med= 196 | max= 196
Count = 1 | read = 213 | min= 196 | med= 204 | max= 213
Count = 2 | read = 263 | min= 196 | med= 229 | max= 263
Count = 3 | read = 272 | min= 196 | med= 234 | max= 272
Count = 4 | read = 295 | min= 196 | med= 245 | max= 295
Count = 5 | read = 361 | min= 196 | med= 278 | max= 361
Count = 6 | read = 340 | min= 196 | med= 278 | max= 361
Count = 7 | read = 391 | min= 196 | med= 293 | max= 391
Count = 8 | read = 358 | min= 196 | med= 293 | max= 391
Count = 9 | read = 411 | min= 196 | med= 303 | max= 411
Count = 10 | read = 424 | min= 196 | med= 310 | max= 424
Count = 11 | read = 396 | min= 196 | med= 310 | max= 424
Count = 12 | read = 377 | min= 196 | med= 310 | max= 424
Count = 13 | read = 402 | min= 196 | med= 310 | max= 424
Count = 14 | read = 438 | min= 196 | med= 317 | max= 438
Count = 15 | read = 495 | min= 196 | med= 345 | max= 495
Count = 16 | read = 473 | min= 196 | med= 345 | max= 495
Count = 17 | read = 449 | min= 196 | med= 345 | max= 495
Count = 18 | read = 438 | min= 196 | med= 345 | max= 495
Count = 19 | read = 399 | min= 196 | med= 345 | max= 495
Count = 20 | read = 396 | min= 196 | med= 345 | max= 495
Count = 21 | read = 422 | min= 196 | med= 345 | max= 495
Count = 22 | read = 462 | min= 196 | med= 345 | max= 495
Count = 23 | read = 499 | min= 196 | med= 347 | max= 499
Count = 24 | read = 479 | min= 196 | med= 347 | max= 499
Count = 25 | read = 529 | min= 196 | med= 362 | max= 529
Count = 26 | read = 487 | min= 196 | med= 362 | max= 529
Count = 27 | read = 461 | min= 196 | med= 362 | max= 529
Count = 28 | read = 462 | min= 196 | med= 362 | max= 529
Count = 29 | read = 501 | min= 196 | med= 362 | max= 529
Count = 30 | read = 516 | min= 196 | med= 362 | max= 529
Count = 31 | read = 544 | min= 196 | med= 370 | max= 544
Count = 32 | read = 562 | min= 196 | med= 379 | max= 562
Count = 33 | read = 564 | min= 196 | med= 380 | max= 564
Count = 34 | read = 543 | min= 196 | med= 380 | max= 564
Count = 35 | read = 541 | min= 196 | med= 380 | max= 564
Count = 36 | read = 570 | min= 196 | med= 383 | max= 570
Count = 37 | read = 603 | min= 196 | med= 399 | max= 603
Count = 38 | read = 597 | min= 196 | med= 399 | max= 603
Count = 39 | read = 630 | min= 196 | med= 413 | max= 630
Count = 40 | read = 571 | min= 196 | med= 413 | max= 630
The code I use to read the median random numbers...
// MIN_MED_MAX
int count, reading, readingMin = 1023, readingMed, readingMax = 0;
unsigned long timer, timeout = 200; // event timing
void setup() {
Serial.begin(115200);
randomSeed(analogRead(A0));
}
void loop() {
if (millis() - timer > timeout) { // check for event timeout
timer = millis(); // reset timer
reading = analogRead(A0); // read a pin
// reading = random(1024); // produce random value
if (reading < readingMin)
readingMin = reading;
if (reading > readingMax)
readingMax = reading;
readingMed = (readingMin + readingMax ) / 2; // simple average
// printing();
padding (count++, "Count = ");
padding(reading, " | read = ");
padding(readingMin, " | min=");
padding(readingMed, " | med=");
padding(readingMax, " | max=");
Serial.println();
}
}
void padding(int value, char text[]) {
Serial.print(text);
if (value < 1000) Serial.print(" ");
if (value < 100) Serial.print(" ");
if (value < 10) Serial.print(" ");
Serial.print(value);
}
