recalling function values

my friend an I are trying to make project where we use a temperature sensor.

But the in de serial monitor the temperature keeps giving us zeros
we think it has to do with recalling the function values

int tempPin = A0;
int ledGroen = 7;
int ledRood = 8; // LED connected to digital pin 13
int maxTemp = 23; // maximale temperatuurwaarde van de omgeving
int reading = 0;
float voltage = 0;
float tempC = 0;

void analogeWaarde (int reading, float voltage, float tempC)
{
reading = analogRead(tempPin); //leest de waarde van temperatuur sensor
voltage = reading*5.0; //gegeven uit de spreetsheet van de temperatuur sensor
voltage /= 1024.0; // zet de analoge nummer 0-1023 om naar volts/digitale waarde
tempC = (voltage - 0.5)*100; // voltage zet je om in graden celsius
}

void setup()
{
pinMode(ledGroen, OUTPUT);// configureerd de pin mode voor uigaande(out) singale of ingaande (in)
pinMode(ledRood, OUTPUT);// configureerd de pin mode voor uigaande(out) singale of ingaande (in)
pinMode(tempPin, INPUT);// configureerd de pin mode voor uigaande(out) singale of ingaande (in)
Serial.begin(9600); // Start de connectie met de computer, druk ctrl+shift+M
}

void loop()
{
analogeWaarde(reading, voltage, tempC);

if(tempC < maxTemp) // als TempC kleiner is dan MaxTemp dan worden de regels hier onder uitgevoerd
{
digitalWrite(ledRood,LOW);//zet led uit
digitalWrite(ledGroen,HIGH);//zet led aan
}

else // als de tempc groter dan maxTemp is word dit gedeelte uitgevoerd
{
digitalWrite(ledRood,HIGH);//zet led aan
digitalWrite(ledGroen,LOW);//zet led uit
}

Serial.print("volts = ");
Serial.print(voltage); // geeft aantal volts aan in serial monitor
Serial.print("v ");
Serial.print("Temprature (C) = ");
Serial.println(tempC); // geeft aantal graden aan in serial montitor
delay(1000); // de tijd tussen 2 metingen in Milliseconde

eindelijk_af_goed.ino (1.88 KB)

Please modify your Post and use the code button </> so your code looks like this and is easy to copy to a text editor.

I am not an expert onf C/C++ and I wonder if this line

void analogeWaarde (int reading, float voltage, float tempC)

creates a local variable called tempC. If so that would mean that the value in the identically named global variable is not updated.

The simple solution would be to change the code to

void analogeWaarde ()

and let the function use the global variables

...R

You may also use references to your variables within the function call in order to have them changed.

void analogeWaarde (int* reading, float* voltage, float* tempC)
{
  reading = analogRead(tempPin); //leest de waarde van temperatuur sensor
  *voltage = reading*5.0; //gegeven uit de spreetsheet van de temperatuur sensor
  *voltage /= 1024.0; // zet de analoge nummer 0-1023 om naar volts/digitale waarde
  *tempC = (*voltage - 0.5)*100; // voltage zet je om in graden celsius
}
  • edited.
void analogeWaarde (int reading, float voltage, float tempC)
{
  reading = analogRead(tempPin); //leest de waarde van temperatuur sensor
  voltage = reading*5.0; //gegeven uit de spreetsheet van de temperatuur sensor
  voltage /= 1024.0; // zet de analoge nummer 0-1023 om naar volts/digitale waarde
  tempC = (voltage - 0.5)*100; // voltage zet je om in graden celsius
}

Using argument names that are the same as global variables is a VERY bad idea. This function does not need any arguments. As it is defined, the local variable is affected by what the function does. The global variable of the same name is not affected. This function does NOT return data in the arguments, so changes to the argument variables are lost when the function ends.

One more note besides your parameter problem:

Do not use pinMode for analog pins, if you want to use them as analog inputs,
it is reconfiguring the digital side of that pin, which will not hurt if INPUT, but any other value will.