Hilfe beim Ändern der LED auf weiß, wenn die Feuchtigkeit eine Grenze erreicht!

Hallo liebe Community,

ich benötige Hilfe bei meinem Sketch. Ein Bodenfeuchtigkeitssensor übermittelt, ob der Boden feucht genug ist. Wenn dies zutrifft, leuchtet die LED grün. Bei mittlerem Stand leuchtet sie gelb und wenn es zu trocken ist, leuchtet sie rot. Nun lasse ich mir noch die Luftfeuchtigkeit und Temperatur auf eine LCD-Bildschirm anzeigen. Auch hier soll bei zu hoher/niedriger Luftfeuchtigkeit die LED von blau auf weiß springen. Wenn der Sensor kaputt ist, sollen beide LEDs blinken. Was mache ich falsch?

#include <DHT.h>
#include <DHT_U.h>

#include <LiquidCrystal.h>

#define DHTPIN 13
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define PIN_SENSOR A0

#define PIN_LED_GREEN 8
#define PIN_LED_YELLOW 9
#define PIN_LED_RED 10
#define PIN_LED_WHITE A1
#define PIN_LED_BLUE A2

unsigned int sensorValue = 0;
byte currentLed = 0;
byte previousLed = 0;

void setup() {
  Serial.begin(9600);
  pinMode(PIN_LED_GREEN, OUTPUT);
  pinMode(PIN_LED_YELLOW, OUTPUT);
  pinMode(PIN_LED_RED, OUTPUT);
  pinMode(PIN_LED_WHITE, OUTPUT);
  pinMode(PIN_LED_BLUE, OUTPUT);
  dht.begin();
  lcd.begin(16, 2);
}

void loop() {
  sensorValue = analogRead(PIN_SENSOR);
  Serial.println(sensorValue);

  if (sensorValue > 550 && currentLed != PIN_LED_GREEN) {
    currentLed = PIN_LED_GREEN;

  } else if (sensorValue > 450 && currentLed != PIN_LED_YELLOW) {
    currentLed = PIN_LED_YELLOW;

  } else {
    currentLed = PIN_LED_RED;
  }

  if (previousLed != currentLed) {
    previousLed = currentLed;

    digitalWrite(PIN_LED_GREEN, LOW);
    digitalWrite(PIN_LED_YELLOW, LOW);
    digitalWrite(PIN_LED_RED, LOW);
    digitalWrite(currentLed, HIGH);
  }

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (h <= 80 && h >= 50 && currentLed != PIN_LED_BLUE) {
    currentLed = PIN_LED_BLUE;

  } else if (h > 80 && currentLed == PIN_LED_BLUE && currentLed != PIN_LED_WHITE) {
    currentLed != PIN_LED_WHITE;

  } else {
    currentLed != PIN_LED_WHITE;
  }

  if (previousLed != currentLed) {
    previousLed = currentLed;

    digitalWrite(PIN_LED_BLUE, LOW);
    digitalWrite(PIN_LED_WHITE, LOW);
    digitalWrite(currentLed, HIGH);
  }

  if (isnan(t) || isnan(h)) {
    lcd.setCursor(0, 0);
    lcd.print("Fehler beim");
    lcd.print("Auslesen!");
    digitalWrite(PIN_LED_WHITE && PIN_LED_BLUE, HIGH);

  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Feuchte: ");
    lcd.print(h);
    lcd.print("%");
    lcd.setCursor(0, 1);
    lcd.print("Temperatur: ");
    lcd.print(t);
  }

   delay(1000);
}

axeon:
Was mache ich falsch?

delay() z.B.

Ich hab Deinen Code noch nicht durch.
Aber wenn Du willst, das sich hier.

if (isnan(t) || isnan(h)) {
    lcd.setCursor(0, 0);
    lcd.print("Fehler beim");
    lcd.print("Auslesen!");
    digitalWrite(PIN_LED_WHITE && PIN_LED_BLUE, HIGH);

was blinkt, dann sollte das anders aussehen.

    digitalWrite(PIN_LED_WHITE(!digitalRead(PIN_LED_WHITE)));
    digitalWrite(PIN_LED_BLUE(!digitalRead(PIN_LED_BLUE)));

wäre eine Variante

Hallo
auch ich habe mal deinen Sketch überflogen, deine LED-Logik habe ich nicht verstanden :slight_smile:

Aber dafür habe ich den Sketch so angepasst das bei NAN-Bedingung die weisse LED blinkt. Dazu habe ich das delay durch einen einfachen 1sec Zeitgber ersetzt - ALLES UNGETESTET:

#include <DHT.h>
#include <DHT_U.h>

#include <LiquidCrystal.h>

#define DHTPIN 13
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define PIN_SENSOR A0

#define PIN_LED_GREEN 8
#define PIN_LED_YELLOW 9
#define PIN_LED_RED 10
#define PIN_LED_WHITE A1
#define PIN_LED_BLUE A2

unsigned int sensorValue = 0;
byte currentLed = 0;
byte previousLed = 0;

void setup() {
  Serial.begin(9600);
  pinMode(PIN_LED_GREEN, OUTPUT);
  pinMode(PIN_LED_YELLOW, OUTPUT);
  pinMode(PIN_LED_RED, OUTPUT);
  pinMode(PIN_LED_WHITE, OUTPUT);
  pinMode(PIN_LED_BLUE, OUTPUT);
  dht.begin();
  lcd.begin(16, 2);
}

void loop() {
  static unsigned long stempel = 0;
  static unsigned long pause = 1000;

  if (millis() - stempel >= pause ) {
    stempel = millis();
    sensorValue = analogRead(PIN_SENSOR);
    Serial.println(sensorValue);

    if (sensorValue > 550 && currentLed != PIN_LED_GREEN) {
      currentLed = PIN_LED_GREEN;

    } else if (sensorValue > 450 && currentLed != PIN_LED_YELLOW) {
      currentLed = PIN_LED_YELLOW;

    } else {
      currentLed = PIN_LED_RED;
    }

    if (previousLed != currentLed) {
      previousLed = currentLed;

      digitalWrite(PIN_LED_GREEN, LOW);
      digitalWrite(PIN_LED_YELLOW, LOW);
      digitalWrite(PIN_LED_RED, LOW);
      digitalWrite(currentLed, HIGH);
    }

    float h = dht.readHumidity();
    float t = dht.readTemperature();

    if (h <= 80 && h >= 50 && currentLed != PIN_LED_BLUE) {
      currentLed = PIN_LED_BLUE;

    } else if (h > 80 && currentLed == PIN_LED_BLUE && currentLed != PIN_LED_WHITE) {
      currentLed != PIN_LED_WHITE;

    } else {
      currentLed != PIN_LED_WHITE;
    }

    if (previousLed != currentLed) {
      previousLed = currentLed;

      digitalWrite(PIN_LED_BLUE, LOW);
      digitalWrite(PIN_LED_WHITE, LOW);
      digitalWrite(currentLed, HIGH);
    }
    if (isnan(t) || isnan(h)) {
      lcd.setCursor(0, 0);
      lcd.print("Fehler beim");
      lcd.print("Auslesen!");
      digitalWrite(PIN_LED_WHITE, not digitalRead(PIN_LED_WHITE)) // && PIN_LED_BLUE, HIGH);

    } else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Feuchte: ");
      lcd.print(h);
      lcd.print("%");
      lcd.setCursor(0, 1);
      lcd.print("Temperatur: ");
      lcd.print(t);
    }
  }
  /////delay(1000);
}

paulpaulson:
auch ich habe mal deinen Sketch überflogen, deine LED-Logik habe ich nicht verstanden :slight_smile:

Ich kann da nicht drüber grinsen - aber nu…
Ja, ich hab die Logik schon - der TO sollte aber wenigstens was von sich geben und nicht drauf warten, das ihm sein Sketch geschrieben wird.

Aber dafür habe ich den Sketch so angepasst das bei NAN-Bedingung die weisse LED blinkt. Dazu habe ich das delay durch einen einfachen 1sec Zeitgber ersetzt - ALLES UNGETESTET:

#include <DHT.h>

#include <DHT_U.h>

#include <LiquidCrystal.h>

#define DHTPIN 13
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define PIN_SENSOR A0

#define PIN_LED_GREEN 8
#define PIN_LED_YELLOW 9
#define PIN_LED_RED 10
#define PIN_LED_WHITE A1
#define PIN_LED_BLUE A2

unsigned int sensorValue = 0;
byte currentLed = 0;
byte previousLed = 0;

void setup() {
  Serial.begin(9600);
  pinMode(PIN_LED_GREEN, OUTPUT);
  pinMode(PIN_LED_YELLOW, OUTPUT);
  pinMode(PIN_LED_RED, OUTPUT);
  pinMode(PIN_LED_WHITE, OUTPUT);
  pinMode(PIN_LED_BLUE, OUTPUT);
  dht.begin();
  lcd.begin(16, 2);
}

void loop() {
  static unsigned long stempel = 0;
  static unsigned long pause = 1000;

if (millis() - stempel >= pause ) {
    stempel = millis();
    sensorValue = analogRead(PIN_SENSOR);
    Serial.println(sensorValue);

if (sensorValue > 550 && currentLed != PIN_LED_GREEN) {
      currentLed = PIN_LED_GREEN;

} else if (sensorValue > 450 && currentLed != PIN_LED_YELLOW) {
      currentLed = PIN_LED_YELLOW;

} else {
      currentLed = PIN_LED_RED;
    }

if (previousLed != currentLed) {
      previousLed = currentLed;

digitalWrite(PIN_LED_GREEN, LOW);
      digitalWrite(PIN_LED_YELLOW, LOW);
      digitalWrite(PIN_LED_RED, LOW);
      digitalWrite(currentLed, HIGH);
    }

float h = dht.readHumidity();
    float t = dht.readTemperature();

if (h <= 80 && h >= 50 && currentLed != PIN_LED_BLUE) {
      currentLed = PIN_LED_BLUE;

} else if (h > 80 && currentLed == PIN_LED_BLUE && currentLed != PIN_LED_WHITE) {
      currentLed != PIN_LED_WHITE;

} else {
      currentLed != PIN_LED_WHITE;
    }

if (previousLed != currentLed) {
      previousLed = currentLed;

digitalWrite(PIN_LED_BLUE, LOW);
      digitalWrite(PIN_LED_WHITE, LOW);
      digitalWrite(currentLed, HIGH);
    }
    if (isnan(t) || isnan(h)) {
      lcd.setCursor(0, 0);
      lcd.print(“Fehler beim”);
      lcd.print(“Auslesen!”);
      digitalWrite(PIN_LED_WHITE, not digitalRead(PIN_LED_WHITE)) // && PIN_LED_BLUE, HIGH);

} else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(“Feuchte: “);
      lcd.print(h);
      lcd.print(”%”);
      lcd.setCursor(0, 1);
      lcd.print("Temperatur: ");
      lcd.print(t);
    }
  }
  /////delay(1000);
}

Nein, ist es nicht.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.