Variables keep getting modified on gets
hello, struggling should quite simple.
i have little program count time, keep track of 4 different time. of seems staying in memory fine, 2 timestamps acting strange. 1 of them diminishing or increasing 1, while other decrease 1 on every update.
i have tried running code in visual studio , works smoothly.... thought perhaps memory after running 1 of method available on forum display free ram, looks still had 1.5kb
here's sample of code. same thing except millis set millis()
it output following. notice 1 of variable keep swapping while other somehow decrease.
i feel broken board.
i have little program count time, keep track of 4 different time. of seems staying in memory fine, 2 timestamps acting strange. 1 of them diminishing or increasing 1, while other decrease 1 on every update.
i have tried running code in visual studio , works smoothly.... thought perhaps memory after running 1 of method available on forum display free ram, looks still had 1.5kb
here's sample of code. same thing except millis set millis()
code: [select]
// pins
const byte pin_ir = 11;
const byte pin_irpower = 9;
const byte pin_ledgreen =10;
const byte pin_ledred =7;
const byte pin_optopower = 8;
const byte pin_relay = 3;
// time
const byte const_water_min = 0;
const byte const_water_second = 10;
const byte const_water_interval_day = 0;
const byte const_water_interval_hour = 0;
const byte const_water_interval_minute = 0;
const byte const_water_interval_second = 20;
// vars
static unsigned long prevtime;
static unsigned long currenttime;
static unsigned long nextwateringtimestamp;
static unsigned long nextwateringintterupttimestamp;
static bool enabled = false;
void setup()
{
// start serial, init date, start receiver
serial.begin(9600);
// bind pins
pinmode(pin_ledgreen, output);
pinmode(pin_ledred, output);
pinmode(pin_relay, output);
pinmode(pin_optopower, output);
pinmode(pin_irpower, output);
// power
digitalwrite(pin_optopower, high);
digitalwrite(pin_irpower, high);
}
void loop() {
currenttime = millis() / 1000;
if(currenttime != prevtime)
{
serial.print("current time =");
serial.print(currenttime);
serial.print(" watering time =");
serial.print(nextwateringtimestamp);
serial.print(" interrupt time =");
serial.println(nextwateringintterupttimestamp);
prevtime = currenttime;
checkwateringtimestamp();
}
}
unsigned long gettime(byte days, byte hours, byte minutes, byte seconds)
{
return seconds + minutes * 60 + hours * 3600 + days * 86400;
}
unsigned long getnextwatertimestamp()
{
return currenttime + gettime(const_water_interval_day, const_water_interval_hour, const_water_interval_minute, const_water_interval_second);
}
unsigned long getnextinterrupttimestamp()
{
return currenttime + gettime(0, 0, const_water_min, const_water_second);
}
unsigned long getwateringtime()
{
return gettime(0, 0, const_water_min, const_water_second);
}
void checkwateringtimestamp()
{
if(nextwateringtimestamp == 0)
{
nextwateringtimestamp = getnextwatertimestamp();
}
if(nextwateringtimestamp == currenttime)
{
nextwateringtimestamp = getnextwatertimestamp();
nextwateringintterupttimestamp = getnextinterrupttimestamp();
waterstart();
}
if(nextwateringintterupttimestamp == currenttime)
{
nextwateringintterupttimestamp = 0;
waterstop();
}
}
void waterstart()
{
digitalwrite(pin_ledgreen, high);
digitalwrite(pin_relay, high);
}
void waterstop()
{
digitalwrite(pin_ledgreen, low);
digitalwrite(pin_relay, low);
}
void waterresume()
{
waterstart();
nextwateringintterupttimestamp = getnextinterrupttimestamp();
}
it output following. notice 1 of variable keep swapping while other somehow decrease.
i feel broken board.
code: [select]
current time =20 watering time =21 interrupt time =0
current time =21 watering time =21 interrupt time =0
current time =22 watering time =41 interrupt time =31
current time =23 watering time =40 interrupt time =30
current time =24 watering time =41 interrupt time =29
current time =25 watering time =40 interrupt time =28
current time =26 watering time =41 interrupt time =27
current time =27 watering time =40 interrupt time =0
i noticed first overflowing here when multiplication:
try this:
code: [select]
unsigned long gettime(byte days, byte hours, byte minutes, byte seconds)
{
return seconds + minutes * 60 + hours * 3600 + days * 86400;
}
try this:
code: [select]
unsigned long gettime(byte days, byte hours, byte minutes, byte seconds)
{
return ((seconds + minutes) * 60ul) + (hours * 3600ul) +(days * 86400ul);
}
Arduino Forum > Using Arduino > Programming Questions > Variables keep getting modified on gets
arduino
Comments
Post a Comment