# mathematical calculations

gud day sirs .. i would like to ask on how to make mathematical calculations based on time be exact what i mean is i have a program which counts the time using millis() and at a corresponding time i would like to increment a value to x which is x++ but in the incrementation instead of increameanting 1 it increaments 155 - 160 which i think because its too fast for a second to increment only 1 what would you guys suggest

counter = millis()/1000; divider = counter % 3;

if (divider == 0) { ++constant; }

lcd.setCursor(0,0); lcd.print(constant);

it prints per second constant + 160....i only need to increment 1

What do you think this line is doing?

``````divider = counter % 3;
``````

code at every 3 seconds the divider == 0 and then thats where i want to increment 1 to constant

my problem i think is the difference from 1 second to 2 seconds is too long for the arduino so it increments atleast 160 times before changing the next second...

That's ok. I wasn't sure if that was your intention.

It would help if you showed your whole sketch, so we could see how variables are defined, and where the snippet fits in.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int constant = 0;
int seconds =0;
unsigned long counter = 0;
float divider =0;
int cons;

void setup()
{
lcd.begin(20,4);
}
void loop()
{
counter = millis()/1000;
divider = counter % 3;
seconds = counter;

if (divider == 0)
{
++constant;
}

lcd.setCursor(0,2);

lcd.print("value: ");
lcd.print(constant);
lcd.print(“sec”);
lcd.print(seconds);
lcd.setCursor(0,0);
lcd.print(divider);
}

what appears in my lcd in the first row it appears 1 , 2, 0
in the 3rd row it appears value: (constant) → the value here is too high…
i know im doing something wrong because in every 1 second it increments about 150 to 160 times…

DneX: gud day sirs .. i would like to ask on how to make mathematical calculations based on time be exact what i mean is i have a program which counts the time using millis() and at a corresponding time i would like to increment a value to x which is x++ but in the incrementation instead of increameanting 1 it increaments 155 - 160 which i think because its too fast for a second to increment only 1 what would you guys suggest

counter = millis()/1000; divider = counter % 3;

if (divider == 0) { ++constant; }

lcd.setCursor(0,0); lcd.print(constant);

it prints per second constant + 160....i only need to increment 1

The problem is that you only really want to increment 'constant' (not a good name for a variable, by the way!) when the divider changes to zero, not every time you recalculate it and find it's still zero.

So you need to store the previous value of divider in a variable, calculate the current value as you have got it above, if the new value is zero and the previous value was not zero then increment your 'constant'. Remember to update the previous value at the end so you are ready to detect the next change.

Alternatively, since you seem to be trying to do something every three seconds, you could just do that in one go using the technique used in the 'blink without delay' example to determine when three seconds have passed. Either way would work fine.

ok thx ill try it again...