I am new with coding, and I was wondering how I could run something once and define it as a constant. Here is my code:

int sensorValue1 = analogRead(A0);  
int sensorValue2 = analogRead(A1);  
int g0 = 9.81*9.8;  
int h1 = 48;  
int R = 287.06;  
int a = -0.0065;  

void setup() {   
    int p1 = (1.09512*sensorValue2-22.105)*133.3225;  
    int t1 = (-0.08126*sensorValue1+65.6597)*274.15;  

void loop() {
    String t = "Temperature:    ";  
    String tt = " Celsius";  
    String p = "Preassure:    ";  
    String pp = " Pascal";
    float celsius = -0.08126*sensorValue1+65.6597;     
    float kelvin = celsius*274.15;
    float pascal = (1.09512*sensorValue2-22.105)*133.3225;  
    float height = (t1/a)*((pascal/p1)^((a*R)/g0))-1)+h1;  
    Serial.println( );  
  • What do you need to run something once? Put in inside "setup" method (it gets executed once the Arduino powers up or gets reset)
    – fabrosell
    Nov 19 '15 at 14:53

You can't, and there's other things wrong with your code.

int sensorValue1 = analogRead(A0);  
int sensorValue2 = analogRead(A1);  

Calling functions from the global scope is an incredibly bad idea. Those are run before the board has been properly initialized, and the results you may get from it are not guaranteed in any way, shape, or form. Instead you must call functions like analogRead() from within another function, like setup(), or loop().

As for const - a const is just that - a constant value that can never change. You can only assign a value to it at the moment of creation.

And that means that you can only use it within the scope it was defined within. As you can't do that in the global scope (see above) then you cannot use a constant.

So just use a normal global variable. It will only change when you tell it to change. Using a const only makes sense when you have a numeric literal that you want to refer to by name. Using const doesn't protect the variable from being changed at run-time by memory leaks, etc, it just protects it from attempts to modify it at compile time - and if possible replace it with the numeric literal that it refers to.

So in summary just use:

int sensorValue1;
int sensorValue2;
int p1;
int t1;

void setup() {
    sensorValue1 = analogRead(A0);
    sensorValue2 = analogRead(A1);
    p1 = (1.09512*sensorValue2-22.105)*133.3225;
    t1 = (-0.08126*sensorValue1+65.6597)*274.15;
... etc ...

HOWEVER: Your program methodology is also completely flawed. You read the analog values once, and do calculations with them once, then repeatedly display the same values over and over again.

You must perform a new analogRead() of your sensors every iteration of loop() in order to update the values in your variables.


You can define Constants using the "const" keyword. Ex:

    const float pi = 3.14;
    const int foo = 12;


  • However scope rules mean that variables declared in setup() won't be visible in loop() – to make them visible declare them outside of any function.
    – dlu
    Nov 21 '15 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.