Hi everyone, I'm trying to do a Timegrapher using ESP32 (30 Pins), Nextion Display and the Pmod MIC3 for recording the ''tic tac'' of the watch. You can see the code below, when compiling there's no error, but I've some problems.
1- On the serial plotter and monitor, nothing comes out, I don't know what I've done wrong?
2- With the Button Plus and Minus, I increase or decrease the variable LA. But when using those buttons on the display, there's no reaction. What have I done wrong, or how is the right way to do it?
3- Is my Pmod MIC3 recording code is good?
4- Did I set the Low Pass Filter very well. Does 125 Hz is good enough as cut-off-frequency for the watch range noise?
5- The program has 3 pages (zero, one, two), am I displaying those pages very well?
I need your help to correct the''errors''
//#include <SoftwareSerial.h>
#include <Nextion.h>
#include <filters.h>
#include <SPI.h>
#define SS 5
/********************************************************************
Filter Setting, LPF
********************************************************************/
const float cutoff_freq = 125.0; /* Cutoff frequency in Hz */
const float sampling_time = 0.001; /* Sampling time in seconds. */
IIR::ORDER order = IIR::ORDER::OD4; /* Order (OD1 to OD4) */
Filter f(cutoff_freq, sampling_time, order);
/********************************************************************
Variables
********************************************************************/
int n = 0;
int count = 0;
int time_elapsed;
int lift_time;
int start_time;
int end_time;
int time1;
int time2;
int Rtic;
int Rtac;
float Rate;
float BeatError;
float Amplitude;
float BeatRate;
uint16_t LA; //Lift Angle
const unsigned long event = 5000;
/********************************************************************
Nextion Function
********************************************************************/
void ButtonMinusPopCallBack(void *ptr);
void ButtonPlusPopCallBack(void *ptr);
void ButtonWeiterPopCallBack(void *ptr);
void ButtonEnterPopCallBack(void *ptr);
void ButtonClosePopCallBack(void *ptr);
void LiftAnglePopCallBack(void *ptr);
/********************************************************************
Nextion component for page0
********************************************************************/
NexButton ButtonWeiter = NexButton(0, 2, "ButtonWeiter");
/********************************************************************
Nextion component for page1
********************************************************************/
NexText LiftAngle = NexText(1, 1, "LiftAngle");
NexButton ButtonMinus = NexButton(1, 2, "ButtonMinus");
NexButton ButtonPlus = NexButton(1, 3, "ButtonPlus");
NexButton ButtonEnter = NexButton(1, 4, "ButtonEnter");
/********************************************************************
Nextion component for page2
********************************************************************/
NexButton ButtonClose = NexButton(2, 5, "ButtonClose");
NexText Rte = NexText(2, 1, "Rte");
NexText Amp = NexText(2, 2, "Amp");
NexText BErr = NexText(2, 3, "BErr");
NexText BRte = NexText(2, 4, "BRte");
/********************************************************************
Nextion screens
********************************************************************/
NexPage zero = NexPage(0, 0, "zero");
NexPage one = NexPage(1, 0, "one");
NexPage two = NexPage(2, 0, "two");
/********************************************************************
Buffer zone of various variables
********************************************************************/
char buffer[100] = {0};
char buffer_Rte[10] = {0};
char buffer_Amp[10] = {0};
char buffer_BErr[10] = {0};
char buffer_BRte[10] = {0};
char buffer_LiftAngle[10] = {0};
NexTouch *nex_listen_list[] =
{
&ButtonClose,
&ButtonEnter,
&ButtonPlus,
&ButtonMinus,
&ButtonWeiter,
&LiftAngle,
NULL
};
/********************************************************************
Lift Angle Function
********************************************************************/
void LiftAnglePopCallBack(void *ptr)
{
dbSerialPrintln("LiftAnglePopCallBack");
LiftAngle.setText("52");
}
/********************************************************************
Button Minus Function
********************************************************************/
void ButtonMinusPopCallBack(void *ptr)
{
uint16_t len;
dbSerialPrintln("ButtonMinusPopCallBack");
memset(buffer, 0, sizeof(buffer));
LiftAngle.getText(buffer, sizeof(buffer));
LA = atoi(buffer);
LA -= 1;
memset(buffer, 0, sizeof(buffer));
itoa(LA, buffer, 10);
LiftAngle.setText(buffer);
}
/********************************************************************
Button Plus Function
********************************************************************/
void ButtonPlusPopCallBack(void *ptr)
{
uint16_t len;
dbSerialPrintln("ButtonPlusPopCallBack");
memset(buffer, 0, sizeof(buffer));
LiftAngle.getText(buffer, sizeof(buffer));
LA = atoi(buffer);
LA += 1;
memset(buffer, 0, sizeof(buffer));
itoa(LA, buffer, 10);
LiftAngle.setText(buffer);
}
/********************************************************************
Button Weiter Function
********************************************************************/
void ButtonWeiterPopCallBack(void *ptr)
{
dbSerialPrintln("ButtonWeiterPopCallBack");
Serial.print("page one");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
/********************************************************************
Button Enter Function
********************************************************************/
void ButtonEnterPopCallBack(void *ptr)
{
dbSerialPrintln("ButtonEnterPopCallBack");
Serial.print("page two");
delay (1000);
Serial.print("Rte.txt=");
Serial.print(Rate);
Serial.print("Amp.txt=");
Serial.print(Amplitude);
Serial.print("BErr.txt=");
Serial.print(BeatError);
Serial.print("BRte.txt=");
Serial.print(BeatRate);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
/********************************************************************
Button Close Function
********************************************************************/
void ButtonClosePopCallBack(void *ptr)
{
dbSerialPrintln("ButtonClosePopCallBack");
Serial.print("page one");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
void setup(void)
{
nexInit();
Serial.begin(9600); /* initialization of serial communication */
SPI.begin(); /* initialization of SPI port */
SPI.setDataMode(SPI_MODE0); /* configuration of SPI communication in mode 0 */
SPI.setClockDivider(SPI_CLOCK_DIV64); /* configuration of clock at 1MHz */
pinMode(SS, OUTPUT);
Serial.println("Start");
/********************************************************************
Register all event callback function of all component.
********************************************************************/
LiftAngle.attachPop(LiftAnglePopCallBack);
ButtonPlus.attachPop(ButtonPlusPopCallBack);
ButtonMinus.attachPop(ButtonMinusPopCallBack);
ButtonEnter.attachPop(ButtonEnterPopCallBack);
ButtonClose.attachPop(ButtonClosePopCallBack);
ButtonWeiter.attachPop(ButtonWeiterPopCallBack);
dbSerialPrintln("Setup done");
}
void loop(void)
{
nexLoop(nex_listen_list); /* When a pop or push event occured every time, the corresponding component[right page id and component id] in touch event list will be asked. */
int16_t* samples;
samples = (int16_t*)malloc(sizeof(int16_t)*100);
if(millis()>event)
{
if(samples == NULL)
{
Serial.print("insufficient memory");
}
else
{
float filteredval = f.filterIn(MIC3_getSound());
samples[n++] = filteredval;
}
}
if(*samples>18000)
{
count++;
start_time = millis();
if(count==10)
{
if(count == 4)
{
time1 = millis();
}
if(count == 7)
{
Rtic = millis();
}
end_time = millis();
}
}
time_elapsed = (end_time - start_time) / 1000; //Elapsed time for 10 Beats
lift_time = (time_elapsed / 10000) * 3; // Elapsed time for 3 Beats
Rtac = (end_time - time1) / 1000;
time2 = (Rtic - time1) / 1000;
Rate = (Rtic + Rtac) / 2;
BeatError = (time1 - time2) / 2;
Amplitude = LA / (2 * sin(180 * lift_time * (Rate / 7200)));
BeatRate = (count / time_elapsed) * 3600; //Beat Per Hour
Serial.println(*samples);
free(samples);
n = 0;
}
int MIC3_getSound(void) {
digitalWrite(SS, LOW); //activate chip select
int sound = SPI.transfer(0) | (SPI.transfer(0) << 8); //reconstruct 12-bit data
digitalWrite(SS, HIGH); //deactivate chip select
return sound;
}