Hello guys! I need your help about a project of mine...
I have an analog input that is read from a myoware sensor and I want to count the number of blinkings of my eyes.
-
First of all, do you have any ideas about restrictions I could impose on the read values in order to classify a blinking or not? Only a threshold? I did it that way, but many times the threshold isn't something strict and for example simply by moving my head or my eyes (not blinking) is satisfied and thus it is wrongly recognized as blinking...
-
I thought about implementing some FFT in realtime, in order to impose some stricter restrictions, depending on the frequency of the blinking waveform. But since I'm new to Arduino and this is the first time I implement (besides in theory) FFT, I don't know how to do that.
The logic of my program is that in every loop an analog value is read and when some flags and the threshold are satisfied then I have an eye blinking. For the FFT I use the library "arduinoFFT.h" and my complete code is the following:
#include "arduinoFFT.h"
#define SAMPLES 128 //Must be a power of 2
#define SAMPLING_FREQUENCY 1000 //Hz, must be less than 10000 due to ADC
arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES];
double vImag[SAMPLES];
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
}
int blinkings = 0;
int up=0, down=0;
// the loop routine runs over and over again forever:
void loop() {
/*SAMPLING*/
for(int i=0; i<SAMPLES; i++)
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
vReal[i] = sensorValue;
//unsigned long time_up = 0, time_down, time_diff;
vImag[i] = 0;
while(micros() < (microseconds + sampling_period_us)){
}
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
//Serial.println(voltage);
if (voltage > 1.35) {
up = 1;
}
if (up) {
if (voltage < 1.35) {
down = 1;
}
}
}
//Serial.println(blinkings);
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
if (up && down && (peak >= 20 || peak <=150)) {
blinkings++;
up = 0;
down = 0;
//Serial.println('a');
Serial.println(blinkings);
//Serial.println(peak);
}
delay(20);
}
I found the frequency values of 20 and 150 by trial and error but in vain, since they are not very correct as restrictions...
I have an Arduino Uno and the MyoWare Sensor with the 3 extended electrodes shield.
Thank you all for advance, I am struggling over this for days!