# Rounding down measured value in Arduino

Gold Member
I am making a circuit with arduino that will read AC voltage supplied to the mains, and based on that value, control a solid state relay. As per my theory till date, if the AC is >=205 or <= 230V, the relay will be on, otherwise it will be off.

The circuits have been borrowed from here:
1. AC voltmeter project with arduino
2. SSR control with arduino (but without the fan)
I am not giving the code here, because I shall post a separate thread with the whole circuit in the DIY section.

Now, I have been measuring AC supplied to our mains for some time, and it seems that I have some trouble. (In India, we get AC RMS at 220V to 230V, 50Hz, but goes down to around 200V when all the air-conditioners of the neighbouring houses are working). The voltage does remain strictly at some value; it is continuously varying by ##\pm##1V or 2V in a very short period, say 1s. And that is the difficulty. For example, if the voltage varies between 204V and 206V continuously, then the circuit will keep switching on and off the relay, and that will create some trouble for the appliance being controlled by the relay.

What I want to do is to simply round off the calculated voltage to the nearest lowest integer. In that way, I can easily get around with this problem.

Since I am not conversant in C programming, can anyone tell whether the floor() function will work in Arduino? What libraries do I need and how do I download them?

Borek
Mentor
Have you tried #include <math.h>? Did it work?

mfb
Mentor
What I want to do is to simply round off the calculated voltage to the nearest lowest integer. In that way, I can easily get around with this problem.
Why? If the voltage varies between 205.5 and 204.5 V you can still get switching processes. In fact you still get the same range for the voltages as before. Rounding shouldn't make any difference for a >= comparison (and it just shifts the range by 1 V for <= comparisons).
You could work with integers in C, then you have automatic rounding. But I don't think that is what you want.

What you probably want is hysteresis: If the device switches on when the voltage goes above 205 V, it shouldn't switch off if the voltage drops to e.g. 204.9 V. Keep it on unless the voltage drops a bit more (1-3 V? Whatever works for you).
Alternatively, add a timer: After a switching process there cannot be additional switches within x seconds. While this makes sure there are not too many switching processes it can leave your device connected with a very low/high voltage if the voltage changes rapidly.

Asymptotic and anorlunda
Gold Member
Have you tried #include <math.h>? Did it work?
That worked. Thanks.

Gold Member
Why? If the voltage varies between 205.5 and 204.5 V you can still get switching processes. In fact you still get the same range for the voltages as before. Rounding shouldn't make any difference for a >= comparison (and it just shifts the range by 1 V for <= comparisons).
You could work with integers in C, then you have automatic rounding. But I don't think that is what you want.

What you probably want is hysteresis: If the device switches on when the voltage goes above 205 V, it shouldn't switch off if the voltage drops to e.g. 204.9 V. Keep it on unless the voltage drops a bit more (1-3 V? Whatever works for you).
Alternatively, add a timer: After a switching process there cannot be additional switches within x seconds. While this makes sure there are not too many switching processes it can leave your device connected with a very low/high voltage if the voltage changes rapidly.
I just realised that the problem is not only about decimals, but it is a bit complex. I will explain briefly. Let's forget decimals for a moment to keep things simple. What I am finding is that, the voltage shifts from 204V to 205V in about 1s, and again comes back to 204V, and this goes on for a long time. This is where the actual problem lies. If I write my code such that it will switch the relay on in a range >= 205V, and provide a delay of 1s or so, even then it will keep switching the relay on and off continuously, as the voltage fluctuates. The problem doesn't arise when voltage fluctuates between 206V to 207V, but occurs at the limit points. There is a chance that the Arduino will continually switch on and off the relay, thereby damaging the appliance (an air-conditioner).

Now comes the bottleneck: If I provide a long time delay of about 5s or 10s, there is a chance that the voltage will drop to a very low value and the appliance will still work, as the Arduino will be sleeping then.

I need to do something such that the fluctuations at the edge can be removed. Either by coding, or by changing circuit, as my current circuit doesn't take this into account. I cannot use voltage regulators, as they'll provide me with constant DC and I don't want that. Perhaps some IC, but I have to research a bit. I might need something like a Schimitt trigger, but I have to see.