Trouble using 2 timers on my arduino simultaneously and void setup keeps runni
hi, guys, trying make alarm system need 2 timers work @ same time kind of 2 stopwatches timing 2 different things. facinf issue recursive algo.
the other thing bugging me void setup() keeps on running. supposed run once.
could please assist me. code 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 ");
t.every(1e3,check());
}
void cycle (byte x)
{
serial.print("entered cycle");
serial.print("\n");
lcd.clear();
lcd.print("this 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 on , no break has been taken
{
serial.print("left 25min cycle , in else ");
serial.print("\n");
unsigned long previousmillisx = currentmillis;
unsigned long currentmillisx = millis();
if ((unsigned long)(currentmillisx - previousmillisx) >= interval_3min) // force person 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();
}
the other thing bugging me void setup() keeps on running. supposed run once.
could please assist me. code 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 ");
t.every(1e3,check());
}
void cycle (byte x)
{
serial.print("entered cycle");
serial.print("\n");
lcd.clear();
lcd.print("this 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 on , no break has been taken
{
serial.print("left 25min cycle , in else ");
serial.print("\n");
unsigned long previousmillisx = currentmillis;
unsigned long currentmillisx = millis();
if ((unsigned long)(currentmillisx - previousmillisx) >= interval_3min) // force person 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 used loop() , check() function, interrupt service routine. loop() has no idea might have been interrupted, or interruption might have altered value in load1. must define load1 volatile, loop() fetch value load1.
on arduinos, can absolutely load1's value less 45000.
on arduinos, can absolutely load1's value less 45000.
Arduino Forum > Using Arduino > Programming Questions > Trouble using 2 timers on my arduino simultaneously and void setup keeps runni
arduino
Comments
Post a Comment