Variable types in Arduino c++

Hello Guys, I am trying to calculate the frequency of a sensor that gives a square wave of 3.3V magnitude.

In order to calculate that frequency I attached an interrupt so I can measure the time between rising edges.

My code is :
const byte interruptPin = 2;

volatile float t=0;
void setup() {

pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), RisingEdge, RISING);

Serial.begin(9600);
}

void loop() {
Serial.print("t\n");
Serial.print(t);
Serial.print("\n");
float period = (t/1000000);
Serial.print("period\n");
Serial.print(period);
Serial.print("\n");
float frequency = 1/period;
//Serial.print("Frequency = "); Serial.print(frequency); Serial.println(" Hz");
}

void RisingEdge() {
static float temp;

t = micros() - temp;
temp = micros();
}

When checking the value of Period which is a float I get 0.01, why if it is a float I keep getting the value to 2 decimal places?

I am sorry to bother but I am a newbie and I am stuck

Serial.print(period, 7);

print to 7 decimal places

1 Like

It works, Thanks a lot

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