Hi guys
Can any one explain to me what is the meaning of the code line sec%60?
The code attached making a count down but im not getting the part of the "sec%60 "
Thanks
int min = 1;
int sec = 30;
unsigned long oneSecond = 1000UL;
unsigned long startTime;
void setup(){
Serial.begin(9600);
sec = sec + 60 * min;
}
//----------------------------------------------------
void loop()
{
if (millis() - startTime >= oneSecond)
{
sec--;
startTime += oneSecond;
if (sec < 0) KaBoom();
int displayMin = sec/60;
if (displayMin < 10) Serial.print("0");
Serial.print(displayMin);
Serial.print(":");
int displaySec = sec % 60;
if (displaySec < 10) Serial.print("0");
Serial.println(sec % 60);
}
}
void KaBoom()
{
Serial.println("The world has ended");
while(1)
{
}
}
sec % 60 gives the remainder of sec / 60
I know what is % but ib this code spesific it wont make sense to me
When the sec will get down to 59 why i need to make the %60??
Delta_G:
minutes = sec / 60 == 1
seconds = sec%60 == 6
Should the operations be in the reversed order and with more completeness? although, we are getting correct result in this simple case.
int sec;
int int displaySec = sec%60;
sec = sec/60;
//-------------
int displayMin = sec%60;
sec = sec/60;
Let us take the example of int x = 0x12CD, and we are interested to find its decimal value using % and / operations:
void setup()
{
Serial.begin(9600);
byte index[4];
char decNumber[5];
int x = 0x12CD; //4813
for (int i = 3; i >= 0; i--)
{
index[i] = x % 10;
x = x / 10;
}
for (int i = 0; i < 4; i++)
{
decNumber[i] = index[i] + '0';
}
decNumber[4] = 0x00; //null-byte
Serial.print(decNumber); //shows: 4813
}
void loop()
{
}