I am a novice trying to design a time watch machine using ESP32, and I am having problems being able to detect tics. You can see on the attached picture, my approach for detecting tics. First, we look for a value between 5 and 0, this value marks the beginning and the end of a tic. So (1) we look for 5>value>0, once found, we initialize the variables. (2) we have to detect the growth of the values, the values grow until a peak, (3) we find this peak and take the time, once found, (4) we have to detect the decrease of the values, and to be sure that they are decreasing we have to (5) search for 5>value>3 once found, we initialize the variables, we count and detect the growth of the values, and the cycle starts again until count = 6. The conditions are independent of each other, for (2) to be executed, (1) must be done, for (3) to be executed, (2) must be done, for (4) to be executed, (3) must be done, for (5) to be executed, (4) must be done.
Here you can see what I have written as code, but which obviously doesn't behave as I want, because I can't make the conditions independent of each other and I can't do the detection of the increase and decrease of the values. Can anyone suggest a solution to my problem? Thanks in advance
Code: Select all
#define pinn 34
uint32_t timePreviousPeak;
uint32_t timeCurrentPeak;
int count = 0;
int minValue = 100000;
int maxValue = 0;
int value = 0;
uint32_t beat1;
uint32_t beat2;
uint32_t beat3;
uint32_t beat4;
uint32_t beat5;
uint32_t beat6;
uint32_t beat7;
void setup ()
{
Serial.begin(115200);
}
void loop()
{
value = analogRead(pinn);
if( 5 >= value >= 3){ //Initialisation de valeur max et min
maxValue = 0;
minValue = 10000;
}
if( value < minValue )
{
minValue = value;
}
if( value > maxValue ) //Detection du peak
{
maxValue = value;
timeCurrentPeak = micros();
}
if( 5 >= value >= 3){ //Initialisation de valeur max et min
maxValue = 0;
minValue = 10000;
}
count++;
if (count >= 6){
calculation();
}
}
void calculation(){
if( count <= 6 ){
switch(count){
case 1:
beat1 = timeCurrentPeak;
break;
case 2:
beat2 = timeCurrentPeak;
break;
case 3:
beat3 = timeCurrentPeak;
break;
case 4:
beat4 = timeCurrentPeak;
break;
case 5:
beat5 = timeCurrentPeak;
break;
case 6:
beat6 = timeCurrentPeak;
break;
}
}
uint32_t frek = 333333;
uint32_t period = (beat6 - beat1);
uint32_t difference = period - frek;
uint32_t beatError = ((beat3-beat1)-(beat6-beat3));
Serial.print(period);
Serial.print(" ||| ");
Serial.print(difference);
Serial.print(" ||| ");
Serial.println(beatError);
}