Can I store the millis in float like done in this code?

#include<Servo.h>

Servo mark1;
float dur = 2.30, del_dur = 0 ;
float offtime = 1, sys_strt = (millis()/1000), curr_tim = (millis()/1000), del_strt = 0;
float inc_val;
const int relay_on = 8;

//2, output = 9;

void setup()

 {
          digitalWrite(8, HIGH);
          mark1.attach(9);
          mark1.write(110);//servo on
          Serial.begin(9600);
        //  pinMode(switch_on, INPUT);
         // pinMode(output, OUTPUT);
}


void loop()
 {
    if(Serial.available() > 0)
       { 
        inc_val = Serial.read();
        Serial.print("\n");
        
      if(inc_val == 'D') // D = DURATION
       {
        delay(2000); 
        dur = Serial.read();
        sys_strt = (millis()/1000);
         //mark1.write(on);
        }
        
     else if(inc_val == 'd') // d = delay_duration_for_future_turn_on
       {
        mark1.write(158);// servo off
        delay(2000);
        del_dur = Serial.read();
        Serial.print("\n");
        delay (5000);
        dur = (Serial.read() + del_dur);
        del_strt = (curr_tim + del_dur);
        sys_strt = del_strt;
        //curr_tim = (millis()/1000);
            
        } 
 }  
     if((millis()/60000) >= 5)
       {digitalWrite( 8, LOW);}
         
     if(((millis()/1000) - 'del_strt') >= 0 && ((millis()/1000) - 'del_strt')    <=10 )    // statement for delay duration process
           {
            mark1.write(110);// servo on
            }
         
        
      if(((millis()/1000)-sys_strt) >= 'dur')
           {
            mark1.write(158);//servo off
            }    
           
          }







    //int x = ((millis() / 1000) - off_timer_start) / 60;
 // if (digitalRead(switch_on) == HIGH) 
    //{
   // off_timer_start = (millis() / 1000);
    //digitalWrite(output, HIGH);
  //} 
   //else if (x >= offtime && digitalRead(output == HIGH)) {
    //digitalWrite(output, LOW);
 //}
  //delay(1000);

I can't read gibberish. Please edit your post and put the code in code tags.

1 Like

Can I store the millis in float like done in this code?

Is there a reason you feel the need to do this? Millis is an integer.

... an unsigned integer

4 byte unsigned long integer.

... unsigned long integer

1 Like

I saw your original purpose - to explain that it is not a mathematical "real number", but a mathematical integer.

1 Like

And a float only has 24 bits of significance.

Good point. That disqualifies it right off the bat.

Actually, to answer the only question that was actually asked, yes you can store millis() in a float like

float now = millis();

... it's just that you can't use it for any reliable normal timing really..

This post does not belong in "Installation and Troubleshooting", as well.

Note that when automatically converted to integer everything to the right of the decimal point is truncated (thrown away) so 2.3 becomes 2 and 2.9999 also becomes 2.

You can use round() and/or if you are summing floating point numbers the extra resolution might be useful before rounding/truncating.

You can use micros() for more precision, but you can get "fooled" because every program-step takes some amount of time. And although micros is counting in microseconds, it might not actually be getting updated every-single microsecond. ...If you write a program that counts microseconds in a loop, there will be numbers missing as it takes time to run & loop.

...and the maximum resolution of micros() is 4 us.

I'm new here....can't post my code correctly...:sweat_smile::sweat_smile:. Anyway. Yeah. I'm trying to convert millis into hour by dividing (1000*3600) then trying to store this data to a float. Will it work if the millis return a large number?

You certainly can, if you read the forum instructions. They were put there specifically for new people.

You can not SWAG in a programming language. Single quotes can not enclose a string. There is no logical expression there (i.e. comparison). Actually, it's hard to guess what you are up to in this line. When I see something like this, I stop reading because I expect to see numerous additional problems.

I suggest you abandon this program, and begin with some simpler sketches so you have some solid skill for writing larger programs. The current way, you have created a pile of problems to solve, which all have the potential to interact. Not an easy troubleshooting task.

2 Likes

I think that you have a more peaceful life if you convert your hour to milliseconds than the other way around.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

1 Like

Another thing, it is always helpful to see how other people handle common problems. I think it would be a good idea for you to look at some time keeping examples. There are many on the web... have a look at the DateTime library too.

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