Trouble using 2 timers on my arduino simultaneously and void setup keeps runni

Hi, guys, I am trying to make an alarm system where I would need two timers to work at the same time kind of like 2 stopwatches timing 2 different things. But I am facinf issue with my recursive algo.

The other thing that is bugging me is that my void setup() keeps on running. Its supposed to just run once.

Could you please assist me. My code is below.

#include <Timer.h>

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pres1 = A0;
int pres2 = A1;
int pres3 = A2;
int vib = 6;
int led1 = 7;
int led2 = 10;
int led3 = 13;
int START = 8;
int buzz = 9;
byte z = 1;
Timer t;
byte y = 1;
unsigned long interval_25min = 10E3;
unsigned long interval_3min = 2E3;
unsigned long previousMillis = 0;

int load;
unsigned int load1;

void setup()
{
pinMode(START,INPUT);
pinMode(vib,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led1,OUTPUT);
pinMode(buzz,OUTPUT);

lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Please press the”);
lcd.setCursor(0,1);
lcd.print("START button :slight_smile: ");

t.every(1E3,check());

}

void cycle (byte x)
{
Serial.print(“entered cycle”);
Serial.print("\n");

lcd.clear();
lcd.print(“This is Cycle”);
lcd.setCursor(7,1);
lcd.print(x);
delay(100);

unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) <= interval_25min) //start 25 min cycle
{
Serial.print(“in cycle if”);
Serial.print("\n");

byte error = check() + error;

Serial.print(“error value “);
Serial.print(error);
Serial.print(”\n”);

if(error >= 6)
{
lcd.clear();
lcd.print(“You have taken your”);
lcd.setCursor(0,1);
lcd.print(“Break”);
delay(3E3);
lcd.clear();
y++;
previousMillis = currentMillis;
error = 0;
cycle(y);
}
}

else //25 mins over and no break has been taken
{
Serial.print(“left 25min cycle and in else “);
Serial.print(”\n”);

unsigned long previousMillisx = currentMillis;
unsigned long currentMillisx = millis();
if ((unsigned long)(currentMillisx - previousMillisx) >= interval_3min) // Force person to take 3 min break
{
digitalWrite(led1,HIGH);
digitalWrite(buzz,HIGH);
digitalWrite(vib,HIGH);
delay(3E3);
digitalWrite(led1,LOW);
digitalWrite(buzz,LOW);
digitalWrite(vib,LOW);
delay(100);
y++;
cycle(y);
}

}

} // end of cycle function

byte check()
{
Serial.print(“running check”);
Serial.print("\n");

if(load1 <= 45000)
{
return 1;
}

else
{
return 0;
}

} // End of load check function

void loop()
{

load = analogRead(A0);
load1 = map(load,0,1023,0,50000);

if((digitalRead(START)==HIGH) && (load1>=45000))
{
Serial.print(“in if loop”);
Serial.print("\n");

cycle(z);

}

Serial.print(load1);
delay(1E3);
Serial.print("\n");

t.update();

}

load1 is used by loop() and the check() function, which is an interrupt service routine. loop() has no idea that it might have been interrupted, or that the interruption might have altered the value in load1. You MUST define load1 as volatile, so that loop() will always fetch the value for load1.

On most Arduinos, you can be absolutely certain that load1's value will always be less than 45000.

Have a look at how millis() is used to manage timing without blocking in Several things at a time.

If setup() is repeating it means that there is something seriously wrong. If you have recursive code it is probably running out of SRAM.

...R