having trouble with some math and a format for time

int buttonPin = 4;
int buttonState;
int count = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}

void loop() 
{
  // Checks if the button state has change
  buttonState = digitalRead(buttonPin);
  
  
  

  if (buttonState == HIGH){ 
    Serial.println("on");
    while (buttonState == HIGH){
    
      count += 1;
      Serial.println(count);
      delay(1000);

    
      buttonState = digitalRead(buttonPin);
      if (buttonState == LOW){ 
        Serial.println("off");
    
    float finalCount = count;
    
    
    float hours = 0;
    float minutes = 0;
    float seconds = 0;

    float sixtyCount = .60;
    
    hours = finalCount / 3600;
    Serial.println(hours);
    
    int getHours = ExtractDecimalPart(hours, 8);
    Serial.println(getHours);

    minutes = getHours * .3600;
    Serial.println(minutes);
    
    int getMinutes = ExtractDecimalPart(minutes, 2);
    Serial.println(getMinutes);
//    
//    seconds = getMinutes / sixtyCount;
//    
//    
//    int getSeconds = ExtractDecimalPart(minutes, 2);
//    Serial.println(getSeconds);

    


    String hourString = String("Hour: ") + String(hours);
    Serial.println(hourString);
    
//    String minuteString = String("Minutes: ") + String(minutes);
//    Serial.println(minuteString);
//    
//    String secondString = String("Seconds: ") + String(seconds);
//    Serial.println(secondString);
//
//    String theTime = hourString + minuteString + secondString;
    
        break;
        
      }
    }
  }
  count = 0;
  delay(1000);
}   

int ExtractDecimalPart(float Value, int numberOfDecimals)
{
  float temp = Value - (long)(Value); 
  long p = 1;
  for (int i=0; i< numberOfDecimals; i++) p*=10;
  long DecimalPart = p * temp;
  Serial.println (DecimalPart);
  return DecimalPart;
}

so here is my code so far but i feel like im going down a rabbit hole any advice would be good.

basically i have a loop that counts every second why it is on im trying to figure out a way to convert the count into hours mins and seconds

example lets say the count is 3665 the output would be “hours: 1 minutes: 1 seconds: 5”

Most people would simply count total seconds as an unsigned long integer and avoid inaccurate floating point calculations. You will need unsigned long to keep track of time in seconds for more than 18 hours.

It is very simple to derive exact hours and minutes from seconds using integer divide.

void setup() {
  Serial.begin(9600);
  while (!Serial);
  unsigned long seconds = 3665;
  int hours = seconds / 3600UL;
  int minutes = (seconds - hours * 3600UL) / 60UL;
  int seconds_remaining = (seconds - hours * 3600UL - minutes * 60UL);
  Serial.print(hours);
  Serial.print(":");
  Serial.print(minutes);
  Serial.print(":");
  Serial.println(seconds_remaining);
}

void loop() {
}

thank you so much this was a huge help

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.