Take a hardware variable and play in a software

Asked

Viewed 81 times

0

I have a potentiometer(hardware) that generates a variable that will range from 0 to 1023. I need to read this variable

void atualizaDados(){
  int valorpress;
  int valortemp;
  valortemp=analogRead(temperaturaPin);
  temperatura=valortemp/5;  
  pressao=analogRead(pressaoPin);
  for ( pressao=0; pressao>1024; pressao++) {
  //valorpress = analogRead(pressaoPin);
  //pressao=map(valorpress,0,50,0,40);
  Serial.println(pressao);  
  }
  vazao = analogRead(vazaoPin); 
}

The code I was using is this, the pressure is giving the value 0 and is not oscillating.

  • 2

    Only a correction in the way of seeing the thing, to facilitate its use and understanding of terminology. The potentiometer does not generate a "variable", nor does the gate generate a variable. You have a function that returns a value (so far there is no variable in the process, in the sense of programming). When you do algumacoisa = função(), is attributing the value returned to a variable.

2 answers

1

whereas the condition of for (pressao>1024) is an error when typing the code here, the pressure value is being printed as 0, because you after receiving the Arduino reading, assign the value 0 for this variable in the loop for, see:

    pressao=analogRead(pressaoPin);
    for (pressao=0; pressao>1024; pressao++) {}

Anyway, this loop seems unnecessary to me. If you just need to show the pressure value within the range [0.1023], just do this:

pressao=analogRead(pressaoPin);
Serial.println(pressao);  

However, it is very common to want to turn the [0.1023] value into pressure, the code below is an example of how to do this (for a specific sensor):

    //realiza a leitura na porta analica, o valor retornado já é o convertido pelo ADC  
    pressao=analogRead(pressaoPin);     

    //converte a leitura acima em tensão (considerando que a tensão de referencia são 5000 mv (ou 5 V).)
    float tensao = (pressao * 5000.0) / 1023.0;

    //transforma a tensão em pressão (kPa)
    float p1 = ((((tensao - 800.0)*100000.0) / 3200.0) / 1000.0);

Note that this is for a specific sensor (code I removed from an old project). I will not go into the merit of the above constants (nor do I remember to tell you the truth), the important thing here is to show how to transform a value read in an analog port into a value that makes sense to your problem (be it pressure, temperature, etc). You will need to read your sensor datasheet for the above conversion.

An excellent response on digital analog conversion (ADC).

0

Nathalia,

I may be clouded, but I believe the mistake is in your for loop.

for ( pressao=0; pressao>1024; pressao++) {
  //valorpress = analogRead(pressaoPin);
  //pressao=map(valorpress,0,50,0,40);
  Serial.println(pressao);  
}

you will be assigning the value 0 to the variable pressao and after that is running the is while pressao > 1024. What never happens because the value of pressao is 0.

maybe you were looking for this:

for ( pressao=0; pressao < 1024; pressao++) {
  //valorpress = analogRead(pressaoPin);
  //pressao=map(valorpress,0,50,0,40);
  Serial.println(pressao);  
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.