Werte übergeben

Hallo,

hab soeben mein erstes Arduino Projekt gestartet. Ich möchte mit eine automatische Temperaturregelung machen, hab auch ein LCD Keypadshield, mit dem ich die Solltemperatur einstellen kann.

Das Funktioniert ja alles schon mal recht gut.

Mein Problem ist nun, ich bekomme die aktuelle Temperatur nicht auf das Display.
Ich denke dass ich irgend einen Fehler bei der Variablenübergabe habe.. .bin mir aber nicht ganz sicher was.

Ich wollte es hier "void tempHumidityMeasuring(float actuall_temp)" abrufen und übergeben... schaff es aber nicht... was mach ich flasch?

Wäre super wenn mir jemand helfen kann.

/******************************************************************
* Clock and clock adjust demo by "jurs" for German Arduino Forum *
* Hardware required: keypad shield
* Additional library required: Time library must be installed
******************************************************************/

#include <LiquidCrystal.h>
#include <Time.h> 
#include "DHT.h"

#define DHTPIN 26     
#define DHTTYPE DHT22 //DHT11, DHT21, DHT22

int heaterPin   = 22;
int summerPin   = 24;
float actuall_temp = 0;
float set_temp = 0;
float actuall_humidity = 0;
float set_humidity = 0;

// Select the pins used on the LCD panel:
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

DHT dht(DHTPIN, DHTTYPE);

// Define some values used by the LCD panel and buttons:
enum keypadButtons {btnNONE, btnSELECT, btnLEFT, btnRIGHT, btnDOWN, btnUP };
enum menuStates {menuNONE, menuTEMP, menuHUMIDITY, menuTIME};

int readPadButtons()
// read the buttons from analog interface:
// returns the key currently pressed
// do not call this function directly from the sketch!
{
  int adc_key_in = analogRead(0);      // read the value from the sensor
  // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  // we add approx 50 to those values and check to see if we are close
  if (adc_key_in > 1000) return btnNONE;
  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT; 
  return btnNONE;  // when all others fail, return this...
}


#define DEBOUNCETIME 10

int buttonPressed()
// this function returns true only once per single button pressed
// use this function in your program sketch
{
  static boolean buttonFired;
  static long buttonDownSince;
  int State=readPadButtons();
  if (State==btnNONE) 
  { // this button is not pressed at this time
    buttonFired=false;
    buttonDownSince=0;
  }
  else if (buttonDownSince==0)
  { // button is pressed but DEBOUNCETIME did not yet started counting
    buttonFired=false;
    buttonDownSince=millis();
  }
  if (millis()-buttonDownSince>=DEBOUNCETIME && buttonFired==false)
  { // button is pressed and DEBOUNCETIME passed, so fire the button
    buttonFired=true;
    return(State); // return button
  } 
  return(btnNONE); // no button fired this time
}


void setup() 
{
  lcd.begin(16, 2);             // starts the LCD display (2 lines by 16 chars)
  setTime(0);                   // set some initial time
  pinMode(heaterPin, OUTPUT);
  pinMode(summerPin, OUTPUT);
  set_temp = 38.0;
  set_humidity = 80;
  dht.begin();
}


int menuState; // global variable to hold the menu status

void  updateDisplay()
// this function updates the display (time and menu status)
{
  if (menuState==menuTEMP){
    lcd.clear();
    lcd.setCursor(0,0);         
    lcd.print("Isttemp. : ");
    lcd.setCursor(11,0);
    lcd.println(actuall_temp, 1);
    lcd.setCursor(15,0);
    lcd.print("C");
    lcd.setCursor(0,1);
    lcd.print("Solltemp.: ");
    lcd.setCursor(11,1);
    lcd.println(set_temp, 1);
    lcd.setCursor(15,1);
    lcd.print("C");}
  else if (menuState==menuHUMIDITY)
  {
    lcd.clear();
    lcd.setCursor(0,0);         
    lcd.print("Istluftf. : ");
    lcd.setCursor(11,0);
    lcd.println(actuall_humidity, 1);
    lcd.setCursor(15,0);
    lcd.write(B00100101);         // Prozentzeichen

    lcd.setCursor(0,1);
    lcd.print("Sollluftf.: ");
    lcd.setCursor(11,1);
    lcd.println(set_humidity, 1);
    lcd.setCursor(15,1);
    lcd.write(B00100101);         // Prozentzeichen
  } 
  else if (menuState==menuTIME)
  {
  lcd.clear();
  char lcdline[17];
  sprintf(lcdline,"Tag %02d %02d:%02d:%02d", day(),hour(),minute(),second());
  lcd.setCursor(0,0);
  lcd.print(lcdline);
  lcd.setCursor(0,1);
  lcd.print("Gesamtbrutzeit");
  } 
  else 
    {
    lcd.clear();
    lcd.print("T :  ");
    lcd.setCursor(4,0); 
    lcd.println(actuall_temp,1);
    lcd.setCursor(8,0);
    lcd.println("/");
    lcd.setCursor(9,0);
    lcd.println(set_temp,1);
    lcd.setCursor(13,0);
    lcd.write(B11011111);      // ° Zeichen einfügen
    lcd.print("C");

    lcd.setCursor(0,1);
    lcd.print("LF:  ");
    lcd.setCursor(4,1); 
    lcd.print(actuall_humidity);
    lcd.setCursor(8,1);
    lcd.println("/");
    lcd.setCursor(9,1);
    lcd.print(set_humidity);
    lcd.setCursor(13,1);
    lcd.print(" ");
    lcd.write(B00100101);         // Prozentzeichen
    }  
}


void checkForNewSecond()
// function will update the display once per second
{
  static long lastsecond;
  if (lastsecond!=now())
  {
    updateDisplay();
    lastsecond=now();
  } 
}


void handleMenuTempButton(int button)
// we are in time setting menu and react on pressed button
// by changing the time
{
  switch (button)
  {
    case btnUP:
    {
      set_temp += 0.5;
      digitalWrite(summerPin, HIGH);
      delay(50);
      digitalWrite(summerPin, LOW);
      break;
    }
    case btnDOWN:
    {
      set_temp -= 0.5;
      digitalWrite(summerPin, HIGH);
      delay(50);
      digitalWrite(summerPin, LOW);
      break;
    }
  }
}

void handleMenuHumidityButton(int button)
// we are in time setting menu and react on pressed button
// by changing the time
{
  switch (button)
  {
    case btnUP:
    {
      set_humidity += 0.5;
      digitalWrite(summerPin, HIGH);
      delay(50);
      digitalWrite(summerPin, LOW);
      break;
    }
    case btnDOWN:
    {
      set_humidity -= 0.5;
      digitalWrite(summerPin, HIGH);
      delay(50);
      digitalWrite(summerPin, LOW);
      break;
    }
  }
}

void handleMenuTimeButton(int button)
// we are in time setting menu and react on pressed button
// by changing the time
{
  // nothing yet
}


void handleNormalButton(int button)
{
  // nothing yet
}


void handleButtons()
// Abfrage ob eine Taste gedrückt wurde
// Umschaltung des Menüstatus
// Falls im Zeiteinstellmenü, Funktion zur Behandlung der Tasten im Einstellmenü aufrufen
// Falls im Normalbetrieb, Funktion zur Behandlung der Tasten im Normalbetrieb aufrufen
{
  int curButton;
  curButton=buttonPressed();
  if (curButton==btnNONE) return; // nothing to do if no button is pressed
  else if (curButton==btnSELECT) // switch menu status
  {
    digitalWrite(summerPin, HIGH);
    delay(50);
    digitalWrite(summerPin, LOW);
    if (menuState==menuNONE) menuState=menuTEMP; 
    else if (menuState==menuTEMP) menuState=menuHUMIDITY; 
    else if (menuState==menuHUMIDITY) menuState=menuTIME;
    else menuState=menuNONE;
  }
  else if (curButton!=btnNONE) // falls eine Taste gedrückt wurde
  {
    if (menuState==menuTEMP) handleMenuTempButton(curButton);  // Temperatur einstellen
    else if (menuState==menuHUMIDITY) handleMenuHumidityButton(curButton);  // Luftfeuchte einstellen
    else if (menuState==menuTIME) handleMenuTimeButton(curButton);  // Zeit einstellen (noch nichts vorgesehen)
    else handleNormalButton(curButton); // gedrückte Tasten sonst behandeln
  }
  updateDisplay(); 
}

void heatingperiod()
{ 
   if (set_temp > actuall_temp)
   {
    digitalWrite(heaterPin, LOW);
   }
   else
   {
     digitalWrite(heaterPin, HIGH);
   }  
}


void tempHumidityMeasuring(float actuall_temp)

{

    float h = dht.readHumidity();     //Luftfeuchte auslesen
    float t = dht.readTemperature();  //Temperatur auslesen

    t = actuall_temp;
    h = actuall_humidity;
}
  

void loop()
{
  checkForNewSecond();      // einmal pro Sekunde das Display updaten
  tempHumidityMeasuring(actuall_temp);
  handleButtons();
  heatingperiod();          // Heizungssteuerung

}
void tempHumidityMeasuring(float actuall_temp)
{

    float h = dht.readHumidity();     //Luftfeuchte auslesen
    float t = dht.readTemperature();  //Temperatur auslesen

    t = actuall_temp;
    h = actuall_humidity;
}

Richtig, hier ist ein Bock:

linkeSeite = rechteSeite;

Das Ergebnis der Berechnung rechteSeite wird der Variablen linkeSeite zugewiesen.

Du liest also Temp und Feuchte in zwei lokale Variable t und h, und überschreibst dann die Werte durch actuall_temp und actauall_humidity.
Danach ist die Funktion zu Ende und die lokalen Variablen sind futsch.
actuall_temp und actuall_humidity werden nicht verändert.

Was hältst du hiervon:

// Globale Variable:
float actuall_temp;
float actuall_humidity;
...
void tempHumidityMeasuring() {
     actuall_humidity = dht.readHumidity();   //Luftfeuchte auslesen
     actuall_temp = dht.readTemperature();  //Temperatur auslesen
}
...

P.S. Code Tags ( der Button </> links oben ) macht das ganze lesbarer.

Ah...perfekt danke!
So ein blöder Fehler.. habs jetzt umgeschrieben. Funktioniert... Super

P.S ich habs gesucht für den Code aber nicht gefunden :wink:

/******************************************************************
* Clock and clock adjust demo by "jurs" for German Arduino Forum *
* Hardware required: keypad shield
* Additional library required: Time library must be installed
******************************************************************/

#include <LiquidCrystal.h>
#include <Time.h> 
#include "DHT.h"

#define DHTPIN 26     
#define DHTTYPE DHT22 //DHT11, DHT21, DHT22

int heaterPin   = 22;
int summerPin   = 24;
float actuall_temp = 0;
float set_temp = 0;
float actuall_humidity = 0;
float set_humidity = 0;

// Select the pins used on the LCD panel:
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

DHT dht(DHTPIN, DHTTYPE);

// Define some values used by the LCD panel and buttons:
enum keypadButtons {btnNONE, btnSELECT, btnLEFT, btnRIGHT, btnDOWN, btnUP };
enum menuStates {menuNONE, menuTEMP, menuHUMIDITY, menuTIME};

int readPadButtons()
// read the buttons from analog interface:
// returns the key currently pressed
// do not call this function directly from the sketch!
{
  int adc_key_in = analogRead(0);      // read the value from the sensor
  // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  // we add approx 50 to those values and check to see if we are close
  if (adc_key_in > 1000) return btnNONE;
  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT; 
  return btnNONE;  // when all others fail, return this...
}


#define DEBOUNCETIME 10

int buttonPressed()
// this function returns true only once per single button pressed
// use this function in your program sketch
{
  static boolean buttonFired;
  static long buttonDownSince;
  int State=readPadButtons();
  if (State==btnNONE) 
  { // this button is not pressed at this time
    buttonFired=false;
    buttonDownSince=0;
  }
  else if (buttonDownSince==0)
  { // button is pressed but DEBOUNCETIME did not yet started counting
    buttonFired=false;
    buttonDownSince=millis();
  }
  if (millis()-buttonDownSince>=DEBOUNCETIME && buttonFired==false)
  { // button is pressed and DEBOUNCETIME passed, so fire the button
    buttonFired=true;
    return(State); // return button
  } 
  return(btnNONE); // no button fired this time
}


void setup() 
{
  lcd.begin(16, 2);             // starts the LCD display (2 lines by 16 chars)
  setTime(0);                   // set some initial time
  pinMode(heaterPin, OUTPUT);
  pinMode(summerPin, OUTPUT);
  set_temp = 38.0;
  set_humidity = 80;
  dht.begin();
}


int menuState; // global variable to hold the menu status

void  updateDisplay()
// this function updates the display (time and menu status)
{
  if (menuState==menuTEMP){
    lcd.clear();
    lcd.setCursor(0,0);         
    lcd.print("Isttemp. : ");
    lcd.setCursor(11,0);
    lcd.println(actuall_temp, 1);
    lcd.setCursor(15,0);
    lcd.print("C");
    lcd.setCursor(0,1);
    lcd.print("Solltemp.: ");
    lcd.setCursor(11,1);
    lcd.println(set_temp, 1);
    lcd.setCursor(15,1);
    lcd.print("C");}
  else if (menuState==menuHUMIDITY)
  {
    lcd.clear();
    lcd.setCursor(0,0);         
    lcd.print("Istluftf. : ");
    lcd.setCursor(11,0);
    lcd.println(actuall_humidity, 1);
    lcd.setCursor(15,0);
    lcd.write(B00100101);         // Prozentzeichen

    lcd.setCursor(0,1);
    lcd.print("Sollluftf.: ");
    lcd.setCursor(11,1);
    lcd.println(set_humidity, 1);
    lcd.setCursor(15,1);
    lcd.write(B00100101);         // Prozentzeichen
  } 
  else if (menuState==menuTIME)
  {
  lcd.clear();
  char lcdline[17];
  sprintf(lcdline,"Tag %02d %02d:%02d:%02d", day(),hour(),minute(),second());
  lcd.setCursor(0,0);
  lcd.print(lcdline);
  lcd.setCursor(0,1);
  lcd.print("Gesamtbrutzeit");
  } 
  else 
    {
    lcd.clear();
    lcd.print("T :  ");
    lcd.setCursor(4,0); 
    lcd.println(actuall_temp,1);
    lcd.setCursor(8,0);
    lcd.println("/");
    lcd.setCursor(9,0);
    lcd.println(set_temp,1);
    lcd.setCursor(13,0);
    lcd.write(B11011111);      // ° Zeichen einfügen
    lcd.print("C");

    lcd.setCursor(0,1);
    lcd.print("LF:  ");
    lcd.setCursor(4,1); 
    lcd.print(actuall_humidity);
    lcd.setCursor(8,1);
    lcd.println("/");
    lcd.setCursor(9,1);
    lcd.print(set_humidity);
    lcd.setCursor(13,1);
    lcd.print(" ");
    lcd.write(B00100101);         // Prozentzeichen
    }  
}


void checkForNewSecond()
// function will update the display once per second
{
  static long lastsecond;
  if (lastsecond!=now())
  {
    updateDisplay();
    lastsecond=now();
  } 
}


void handleMenuTempButton(int button)
// we are in time setting menu and react on pressed button
// by changing the time
{
  switch (button)
  {
    case btnUP:
    {
      set_temp += 0.5;
      digitalWrite(summerPin, HIGH);
      delay(50);
      digitalWrite(summerPin, LOW);
      break;
    }
    case btnDOWN:
    {
      set_temp -= 0.5;
      digitalWrite(summerPin, HIGH);
      delay(50);
      digitalWrite(summerPin, LOW);
      break;
    }
  }
}

void handleMenuHumidityButton(int button)
// we are in time setting menu and react on pressed button
// by changing the time
{
  switch (button)
  {
    case btnUP:
    {
      set_humidity += 0.5;
      digitalWrite(summerPin, HIGH);
      delay(50);
      digitalWrite(summerPin, LOW);
      break;
    }
    case btnDOWN:
    {
      set_humidity -= 0.5;
      digitalWrite(summerPin, HIGH);
      delay(50);
      digitalWrite(summerPin, LOW);
      break;
    }
  }
}

void handleMenuTimeButton(int button)
// we are in time setting menu and react on pressed button
// by changing the time
{
  // nothing yet
}


void handleNormalButton(int button)
{
  // nothing yet
}


void handleButtons()
// Abfrage ob eine Taste gedrückt wurde
// Umschaltung des Menüstatus
// Falls im Zeiteinstellmenü, Funktion zur Behandlung der Tasten im Einstellmenü aufrufen
// Falls im Normalbetrieb, Funktion zur Behandlung der Tasten im Normalbetrieb aufrufen
{
  int curButton;
  curButton=buttonPressed();
  if (curButton==btnNONE) return; // nothing to do if no button is pressed
  else if (curButton==btnSELECT) // switch menu status
  {
    digitalWrite(summerPin, HIGH);
    delay(50);
    digitalWrite(summerPin, LOW);
    if (menuState==menuNONE) menuState=menuTEMP; 
    else if (menuState==menuTEMP) menuState=menuHUMIDITY; 
    else if (menuState==menuHUMIDITY) menuState=menuTIME;
    else menuState=menuNONE;
  }
  else if (curButton!=btnNONE) // falls eine Taste gedrückt wurde
  {
    if (menuState==menuTEMP) handleMenuTempButton(curButton);  // Temperatur einstellen
    else if (menuState==menuHUMIDITY) handleMenuHumidityButton(curButton);  // Luftfeuchte einstellen
    else if (menuState==menuTIME) handleMenuTimeButton(curButton);  // Zeit einstellen (noch nichts vorgesehen)
    else handleNormalButton(curButton); // gedrückte Tasten sonst behandeln
  }
  updateDisplay(); 
}

void heatingperiod()
{ 
   if (set_temp > actuall_temp)
   {
    digitalWrite(heaterPin, LOW);
   }
   else
   {
     digitalWrite(heaterPin, HIGH);
   }  
}


void tempHumidityMeasuring()

{

    float h = dht.readHumidity();     //Luftfeuchte auslesen
    float t = dht.readTemperature();  //Temperatur auslesen

    actuall_temp = t;
    actuall_humidity = h;
}
  

void loop()
{
  checkForNewSecond();      // einmal pro Sekunde das Display updaten
  tempHumidityMeasuring();
  handleButtons();
  heatingperiod();          // Heizungssteuerung

}

Dann lösche bitte den Code aus dem ersten Post raus, das wird übersichtlicher. Und ein kleiner Hinweis "Code folgt".

Danke.