aarg:
If you want me to test something, you have to give me a complete sketch, clickable.
I'm odometer, not clickable.
Anyway, try this:
void setup() {
Serial.begin(9600); // or whatever speed you prefer
byte myFifty = 50;
Serial.print("Here --> ");
Serial.print(myFifty);
Serial.println(" <-- should be the number fifty");
}
void loop() {
// this is only here to make the Arduino compiler happy
}
countDown--;
previousMillis = currentMillis;
byte seconds = countDown % 60;
countDown /= 60; // this line has the bug
byte minutes = countDown % 60;
byte hours = countDown / 60;
In your calculation of minutes and hours, you are changing the value of countDown. This is a bug because you will need countDown for later. And this is what was causing Nilanj to see the funny output he got.
@odometer, dohhhhh, thanks for spotting that! Just quickly wrote the code without an Arduino connected
Fixed code:
unsigned long previousMillis = 0;
const unsigned int Interval = 1000;
unsigned int countDown = 10; //stick to one way of naming your variables
byte hold = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (hold != 1) {
if (millis() - previousMillis > Interval) {
previousMillis = millis();
countDown--;
unsigned int temp = countDown;
byte seconds = temp % 60;
temp /= 60;
byte minutes = temp % 60;
byte hours = temp / 60;
if ((hours == 0) && (minutes == 0) && (seconds == 0)) {
Serial.println (" done ");
hold = 1;
}
else {
serialPrintTime(hours, minutes, seconds);
}
}
}
}
void serialPrintLeading(byte value, char lead = '0'){
if(value < 10){
Serial.print(lead);
}
Serial.print(value);
}
void serialPrintTime(byte h, byte m, byte s){
serialPrintLeading(h, ' ');
Serial.print(':');
serialPrintLeading(m);
Serial.print(':');
serialPrintLeading(s);
Serial.println();
}
Printing byte muByte = 50 will just print 50 on the screen (so the ASCII "50" aka 0x35 0x30)
@aarg, it's the option to pass a leading character. For minutes and seconds you want the default 0 but TS did a space for the hours. This way you can do that. If you want to extend it you can also use make the width a parameter but is to complicated for this purpose alone.
void serialPrintLeading(int value, char lead, byte width){
int temp = value;
byte valWidth = 0;
//get width of the value
while(temp){
valWidth++;
temp /= 10;
}
//add another place for the sign
if(value < 0){
valWidth++;
if(lead == '0'){
Serial.print('-');
value = abs(value);
}
}
//width becomes width of white space,
//if value is wider (or as width) no spaces (but don't crop)
if(valWidth <= width){
width -= valWidth;
}
else{
width = 0;
}
//print leading chars
while(width){
Serial.print(lead);
width--;
}
//and the value
Serial.print(value);
}
@AWOL, although a post of text would have been easier he included a image just fine...