Calculating with decimals: float or int?

Hi, When I try to calculate with 'float' numbers, it says "invalid operands of types 'float' and 'int' to binary 'operator^' " which I understand. However, when I try to convert the float variables to an 'int' in order to calculate with the data, the decimals are gone. The decimals were the reason to use 'float' in stead of 'int'.

If I can't use 'float' because they can not communicate with the 'int' data, what will be the best way to define variables which will contain decimals and with which you want to calculate?

Underneath my code:

float trig = 0.00;
float time = 0.00;
unsigned long previousTime = 0;
unsigned long x;
int pin = 7;
byte tgl = 1;

void setup()
{
pinMode(pin,INPUT_PULLUP);
Serial.begin(9600);

}

void loop()
{
if(digitalRead(pin) && tgl){
trig = millis();
tgl = 0;
}

if(!digitalRead(pin) && !tgl){
time = (millis() - trig)/1000; // here time becomes a decimal
tgl = 1;

Serial.println(time);

x = (1.225*(time^2)); // here I would like to calculate with the decimal value

}
}

Take trig to unsigned long. and change this:

  if(!digitalRead(pin) && !tgl){
    time = (millis() - trig)/1000; // here time becomes a decimal
    tgl = 1;
  
   Serial.println(time);
}

to

  if(!digitalRead(pin) && !tgl){
    time = (millis() - trig)/1000.0; // here time becomes a decimal
    tgl = 1;
  
   Serial.println(time);
}

I think you want to use pow. ^ is bitwise xor operator :slight_smile:

guix:
I think you want to use pow. ^ is bitwise xor operator :slight_smile:

pow it was, thanks a lot!

For time^2, why not just use time*time?

Pete