Hey friends.
Its me again and i stuck again. I am using a Nextion and an ESP32
I try to learn how to stop a running function. To be more specific. I have a Start and a Stop button.
When i push the START button: A counter in a textfield starts to count down and a LED turns off.
When i push the STOP button: I want to stop the counter and turn the LED off. (In the futre the LED will be replaced with a motor, and i want a STOP button to stop everything)
My problem right now is, i cant figure it out how to manage it, that my code recognizes that the STOP button was pushed and cancles my counter routine.
Here the code:
class EineKlasse
{
private:
double value;
int value2;
public:
void set_value(double v);
double get_value();
void set_value2(int v);
int get_value2();
};
void EineKlasse::set_value(double v)
{
value = v;
}
void EineKlasse::set_value2(int v)
{
value2 = v;
}
double EineKlasse::get_value()
{
return value;
}
int EineKlasse::get_value2()
{
return value2;
}
class Kalibrieren
{
private:
int menge;
int zeit;
double durchschnitt;
public:
Kalibrieren(int z, int m)
{
zeit = z,
menge = m;
}
int get_time()
{
return zeit;
}
int get_menge()
{
return menge;
}
double CalcDurchschnitt()
{
return (((double(menge)/double(zeit))*3600)/1000);
}
};
/***************************************************
* *************************************************
***************************************************/
//Librarys
#include <Nextion.h>
#include <SimpleTimer.h>
#include <iostream>
using namespace std;
//Klassenobjekte
EineKlasse r1;
SimpleTimer timer;
//Kalibrieren pcal4time(int a, int b);
Kalibrieren *kal2;
//Fading LED
int freq = 5000;
int ledChannel = 0;
int resolution = 8;
// LED Pins
int Dosierungs_LED = 25;
int Betriebs_LED = 27;
int Impuls_Button = 13;
//Display
HardwareSerial Serial2(2);
void startPopCallback(void *ptr);
void stopPopCallback(void *ptr);
int buttonState2 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
NexText t0 = NexText(2, 5, "t0");
NexText t5 = NexText(8, 15, "t5");
NexText t6 = NexText(8, 16, "t6");
NexText t7 = NexText(8, 17, "t7");
NexButton start = NexButton(8, 19, "start");
NexButton stop = NexButton(8, 18, "stop");
//memset-buffer
char buffer[100] = {0};
NexTouch *nex_listen_list[] =
{
&start,
&stop,
NULL
};
void startPopCallback(void *ptr)
{
CalibrationWithTime();
}
void stopPopCallback(void *ptr)
{
buttonState2 = 1;
}
void setup(void)
{
//Serial COM
Serial.begin(115200);
Serial2.begin(9600);
// LED - Pins
pinMode(Dosierungs_LED, OUTPUT);
pinMode(Betriebs_LED, OUTPUT);
pinMode(Impuls_Button, INPUT_PULLUP);
nexInit();
start.attachPop(startPopCallback);
stop.attachPop(stopPopCallback);
//PWM für ESP32
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(25, ledChannel);
}
void loop(void)
{
nexLoop(nex_listen_list);
timer.run();
}
//----------------------------------------------------------------
//------------------------ FUNCTIONS------------------------------
//----------------------------------------------------------------
void setPumpoff()
{
ledcWrite(ledChannel, map(0, 0, 100, 0, 255));
}
//////// wenn Zeit eingegeben wird /////////////////
void CalibrationWithTime()
{
int zeit;
int menge;
int period = 1000;
unsigned long time_now = 0;
memset(buffer, 0, sizeof(buffer));
t6.getText(buffer, sizeof(buffer));
zeit = atoi(buffer);
memset(buffer, 0, sizeof(buffer));
t5.getText(buffer, sizeof(buffer));
menge = atoi(buffer);
int zeit_timer = zeit;
Kalibrieren pcal4time (zeit,menge);
ledcWrite(ledChannel, map(100, 0, 100, 0, 255));
timer.setTimeout((pcal4time.get_time())*1000, setPumpoff);
while (!ifcondtionismetbreak(buttonState2))
{
while(zeit_timer>=0)
{
if(millis() > time_now + period)
{
time_now = millis();
memset(buffer, 0, sizeof(buffer));
itoa(zeit_timer, buffer, 10);
t6.setText(buffer);
zeit_timer--;
//if (ifcondtionismetbreak(buttonState2))
//break;
}
}return;
}
}
bool ifcondtionismetbreak(int x){
if (x == 1)
{
return true;
}
else
{
//cout <<"Continue";
return false;
}
}
bool ifcondtionismetbreakII(int x){
if (x == 1)
{
return true;
}
else
{
//cout <<"Continue";
return false;
}
}
I am kinda frustrated. I know that this code is false. But i am unable to figure it out how to solve the puzzle. I know that somehow the info, that the STOP button was pushed must interrupt the while loop.
Anyone has an idea?
Thank you for all the support
best regards