# require Arduino math explanation

Been working with some code for an NRF24 receiver, which was posted here.

The particular lines I have a query about are these below.
I've looked at it for quite a while now and although I have modified it and worked out the outcome of it, I'm looking for an explanation of just how it works in a breakdown arrangement so-to-speak to understand it fully.

``````while (digitalRead(triggerHeight) == LOW) // If "set switch" is pressed, the user can set the altitude at which the alarm will trigger.
{
lcd.clear();
var = (1000 - val * 24) / 100; //Switching between a float and int, and playing with the math, to make the "alarm altitude" increase in 50 f increments.
beepingHeight = var * 10 + 500;  // original was *50 for 50ft ...changed to 10 for 10ft
lcd.print("Alarm will sound");
lcd.setCursor(0, 1);
lcd.print("at ");
lcd.print(beepingHeight);
lcd.print(" ft    ");
delay(500);
//lcd.clear();
``````

I can see val as being read from a pot which sets a value where the alarm goes off, and var as being a float but where do the other numbers in the calculations originate?? One can see where I have changed 50 to 10 which gives a result in 10ft steps but I'm a bit lost when it comes to the Op's comments etc.

Hoping it is regarded as a "programming query".

I imagine normal math rules apply in that multiplication, then division then addition etc.

``````val = analogRead(setTriggerHeight);
``````

I dont think you named a pin “setTriggerHeight”

bluejets:
Been working with some code for an NRF24 receiver, which was posted here.

The declarations are important...

``````int val = 0;
float var;
int beepingHeight = 1000; //I set this at 1000 so that the unit does not start beeping as soon as it is turned on.
``````

bluejets:
where do the other numbers in the calculations originate?

Presumably they are used to convert from the analog-to-digital-converter value, which is essentially unitless, to real-world units.

bluejets:
I'm a bit lost when it comes to the Op's comments etc.

Some of the comments are just wrong so you being lost is certainly understandable. The "switching between a float and int" gobbledygook is a good example. The calculation is performed entirely using integer math.

bluejets:
I imagine normal math rules apply in that multiplication, then division then addition etc.

Multiplication and division left-to-right then addition...

demkat1:

``````val = analogRead(setTriggerHeight);
``````

I dont think you named a pin "setTriggerHeight"

yes, it is named in the original I linked to as an int. on pin 15

As far as the Op's pinmode naming arrangement for this "setTriggerHeight" I changed it in my code as A1 as it is a pot input.
I then commented out his line in the setup with pinmode as an input as I believe with analog input this is not necessary.

Thanks Coding Badly, as usual I must have looked at that beepingHeight line more than a dozen times but it simply didn't click.

I'll see how I go from here.

Thanks to both replies.