uint16_t variable problem

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there guys,

would someone know why this works

 

uint32_t time_int = 0;

time=actual_time = 98000;

lcd_write(time_int);

 

 

 

also this works

time_int=actual_time = 15*19600;

lcd_write(time_int);

 

 

 

but this does NOT!!!

time_int=actual_time = (((hour_temp1*60)*60)+(minute_temp1*60))+second_temp1;

 

this also not working

time_int=actual_time = ((hour_temp1*60)*60)+(minute_temp1*60)+second_temp1;

 

this output only 11822

 

have been about this all day...

 

here is the all code

uint8_t hour_temp1,minute_temp1,second_temp1=0;
	uint8_t hour_temp2,minute_temp2,second_temp2=0;
	uint32_t time1,time2,actual_time=0;
	

	//Here I was just trying playing arroud by trying to make separate the variables
	hour_temp1 = data.get_hour();
	minute_temp1 = data.get_minute();
	second_temp1 = data.get_second();
	
	uint32_t h = (data.get_hour()*60)*60;
	uint16_t m = (data.get_minute()*60);
	
	
	actual_time = h+m+data.get_second();

if (data.get_humidifier_function_in_use()){
        if (data.get_humidifier_timer_on()){

            data.get_humidifier_time_from(hour_temp,minute_temp,second_temp);
            time1 = (((hour_temp*60)*60)+(minute_temp*60))+second_temp;

            data.get_humidifier_time_to(hour_temp,minute_temp,second_temp);
            time2 = (((hour_temp*60)*60)+(minute_temp*60))+second_temp;

            actual_time = (((hour_temp*60)*60)+(minute_temp*60))+second_temp;
            if((actual_time > time1)&&(actual_time<time2)){
                if (data.get_humidifier1() < data.get_room_humidity() ){
                    data.set_humidifier_on(true);
                }else{
                    data.set_humidifier_on(false);
                }
            }else{data.set_humidifier_on(false);}
        }else
        {
            if (data.get_humidifier1() < data.get_room_humidity() ){
                data.set_humidifier_on(true);
            }else{
                data.set_humidifier_on(false);
            }
        }
    }else{data.set_humidifier_on(false);}

 

thank you very much

This topic has a solution.
Last Edited: Sat. Mar 18, 2017 - 09:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hlinskoelektrikar wrote:
also this works

time_int=actual_time = 15*19600;

Hard to believe.

 

hlinskoelektrikar wrote:
time_int=actual_time = ((hour_temp1*60)*60)+(minute_temp1*60)+second_temp1;

this output only 11822

With what values of hour_temp1, minute_temp1, and second_temp1?

Most likely an overflow. Keep in mind: the type of the variable on the left side of an assignment does not influence the way the evaluation is done on the right side. In this case the whole right side is calculated in int.

 

 

Stefan Ernst

Last Edited: Fri. Mar 17, 2017 - 09:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Integer overflow.
'C' will do integer calculations as 'int' by default, (or wider if there is a 'wider' value in the calculation).
Change hour_temp1 minute_temp1 second_temp1 to uint32_t (or use typecasts in the formula)

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also suffix the constants with ul. Eg 60ul will coax them to be 32 bits. This may also force the compiler to do 32bit calcs rather than 16. As mike hints, it is about C's promotion rules.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi yes this on

time_int=actual_time = 15*19600;

just my mistake it was this way of course :-)

time_int = 15*19600;

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mikech wrote:

Integer overflow.
'C' will do integer calculations as 'int' by default, (or wider if there is a 'wider' value in the calculation).
Change hour_temp1 minute_temp1 second_temp1 to uint32_t (or use typecasts in the formula)

Your solution sound ok but when I am trying this, I get problem that compiler says no  function get_heating_time_from to called with uint32 as parameters of this functions are uit8

even if I try typecast.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Also suffix the constants with ul. Eg 60ul will coax them to be 32 bits. This may also force the compiler to do 32bit calcs rather than 16. As mike hints, it is about C's promotion rules.

 

But hold on!!! This looks write its numbers are now arroung 70200 Iam going to test it more, will let you know....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Also suffix the constants with ul. Eg 60ul will coax them to be 32 bits. This may also force the compiler to do 32bit calcs rather than 16. As mike hints, it is about C's promotion rules.

 

COOOOOOL THATS IT!!!!! Thank you :-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are only 24*60*60 = 86400 seconds in any earth day. So what is this 15*19600 = 294000 seconds thing?

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:

There are only 24*60*60 = 86400 seconds in any earth day. So what is this 15*19600 = 294000 seconds thing?

 

 

I just tested if its calculating right way and if I get right value but this value was have not do anything with time...