//Global variables
unsigned char a=0, b=0, c=0, SqCntr=0;
static bool fsq1_strt;
static bool fsq1_end;
static bool fsq2_strt;
static bool fsq2_end;
static bool fsq3_strt;
static bool fsq3_end;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
SqCntr=1;
FlagRst(); //To start with, reset all flags
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Hello, This is main loop");
// Serial.printf("value of a, b, c are", a,b,c);
if((SqCntr==1) && fsq1_end)
{
SqCntr=2; //Next step
fsq1_end=0; //For the next iteration
fsq2_end=0; //Just to be sure
Serial.println("Set Sequence to 2 in Sq1 loop");
}
if((SqCntr==2) && fsq2_end)
{
SqCntr=3; //Next step
fsq2_end=0; //For the next iteration
Serial.println("Set Sequence to 3 in Sq2 loop");
}
if((SqCntr==3) && fsq3_end)
{
SqCntr=4; //Next step
fsq3_end=0; //For the next iteration
Serial.println("Set Sequence to 4 in Sq3 loop");
}
if(SqCntr==4) //All sequences over
{
SqCntr=1; //Restart
fsq3_end=0; //For the next iteration
FlagRst(); //For relooping
Serial.println("Set Sequence to 1 and call reset");
}
Serial.println("Before while loop Step No is:");
Serial.println(SqCntr);
delay(2000);
while(SqCntr<=3) //Three loops
{
Serial.println("After while loop Step No is:");
Serial.println(SqCntr);
delay(2000);
if((SqCntr==1) && (!fsq1_end))
{ Sq1Routine(); //Sequence routine
//No need of break here
//it will exit while loop
break;
}
if((SqCntr==2) && (!fsq2_end))
{ Serial.println("Sequence 2 routine entry");
Sq2Routine(); //Sequence routine
break;
}
if((SqCntr==3) && (!fsq3_end))
{
Serial.println("Sequence 3 routine entry");
Sq3Routine(); //Sequence routine
break;
}
}
} //Main loop ends
//Now each sequence routine
//Sequence1
void Sq1Routine(void)
{
if (!fsq1_end) //Already ended? Then no work to do
{
if (!fsq1_strt) //Already started?
{
Serial.println("This is Sequence1 starting");
fsq1_strt=1; //Start of routine
delay(2000); //Show this
a=0; //Start counter
}
else {
a++;
Serial.println("Inside Sq1 value of a is");
Serial.println(a);
delay(2000);
if(a>=4)
{
fsq1_strt=0; //Start of routine
fsq1_end=1; //Sequence end
}
}
}
}
//Sequence2
void Sq2Routine(void)
{
if (!fsq2_end) //Already ended? Then no work to do
{
if (!fsq2_strt) //Already started?
{
Serial.println("This is Sequence2 starting");
fsq2_strt=1; //Start of routine
delay(2000); //Show this
b=0; //Start counter
}
else {
b++;
Serial.println("Inside Sq2 value of b is");
Serial.println(b);
delay(2000);
if(b>=4)
{
fsq2_strt=0; //Start of routine
fsq2_end=1; //Sequence end
}
}
}
}
//Sequence1
void Sq3Routine(void)
{
if (!fsq3_end) //Already ended? Then no work to do
{
if (!fsq3_strt) //Already started?
{
Serial.println("This is Sequence3 starting");
fsq3_strt=1; //Start of routine
delay(2000); //Show this
c=0; //Start counter
}
else {
c++;
Serial.println("Inside Sq3 value of c is");
Serial.println(c);
delay(2000);
if(c>=4)
{
fsq3_strt=0; //Start of routine
fsq3_end=1; //Sequence end
}
}
}
}
//Reset all flags
void FlagRst(void)
{
//Start
fsq1_end=0;
fsq2_end=0;
fsq3_end=0;
fsq1_strt=0;
fsq2_strt=0;
fsq2_strt=0;
fsq2_strt=0;
}