Snip of code not working. But alone it does.

Hey there,
I'm coding a sensor controlled pot. I got almost everything working except the pump activation after the sensor detects that the soil is to dry. The Code for the pump works if it runs on its own. But not in the main code later. The only thing i changed are the numbers with variables. I can't find the problem. Maybe someone of you can help me?

Main Code:

/*

*/

//Zeiten Pumpe
#define pump 13
unsigned long Zeit = 0;
unsigned long VorherigeMillis = 0;
const long interval1 = 3000; //Aktive Zeit
const long interval2 = 10000; //Warten Zeit

//Smoothed
#include <Smoothed.h>
#define SENSOR_PIN A1
Smoothed <int> mySensor;

//Display
#include <Arduino.h>
#include <U8x8lib.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

//Constats
const int Hygrometer = A1;
const int Sonne = A0;
const int buttonPin1 = 2;    // the pin that the pushbutton is attached to
const int buttonPin2 = 3;    // the pin that the pushbutton is attached to
//const int pump = 13;

//Variables
int Feuchti = 0;
int FeuchtiMin = 800;
int FeuchtiMax = 445;
int Licht = 0;
int FeuchtiAvg;
int Zahl = 0;
int buttonPushCounter = 50;   // counter for the number of button presses
int buttonState1 = 0;         // current state of the button
int lastButtonState1 = 0;     // previous state of the button
int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button
int statePump = 0;


void setup() {

  Serial.begin(9600);
  pinMode(pump, OUTPUT); // Pumpe
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  u8g2.begin(); //Diplay
  mySensor.begin(SMOOTHED_AVERAGE, 10);
  mySensor.clear();

}


void loop() {

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  Feuchti = analogRead(Hygrometer); //Ließt wert von "Hygrometer", speichert unter "Feuchti"
  mySensor.add(Feuchti);
  FeuchtiAvg = mySensor.get();
  FeuchtiAvg = map(FeuchtiAvg, FeuchtiMin, FeuchtiMax, 0, 100);
  FeuchtiAvg = constrain(FeuchtiAvg, 0, 100);
  Serial.println("Feuchtigkeit: ");
  Serial.print(FeuchtiAvg);
  Serial.println("%");
  Serial.println("Sonne: ");

  Licht = analogRead(Sonne); //Ließt wert von "Photoresistor/Sonne", speichert unter "Licht"

  // compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1) {
    // if the state has changed, increment the counter
    if (buttonState1 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter = buttonPushCounter + 5;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(10);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState1 = buttonState1;

  // compare the buttonState to its previous state
  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter = buttonPushCounter - 5;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(10);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState2 = buttonState2;

  //Display
  u8g2.clearBuffer();
  u8g2.home();
  u8g2.setFont(u8g2_font_8x13O_mr);
  u8g2.setCursor(0, 10);
  u8g2.println("AutoTopf_v4");
  u8g2.setFont(u8g2_font_Born2bSportyV2_te);
  u8g2.setCursor(0, 25);
  u8g2.print("Feuchtigkeit: ");
  u8g2.print(FeuchtiAvg);
  u8g2.print("%");
  u8g2.setCursor(0, 37);
  u8g2.print("Sonne: ");
  if (Licht <= 450) {
    u8g2.setFont(u8g2_font_unifont_t_symbols);
    u8g2.setFontDirection(1);
    u8g2.drawGlyph(42, 28, 0x2600);
    u8g2.setFontDirection(0);
  }
  u8g2.setFont(u8g2_font_Born2bSportyV2_te);
  u8g2.setCursor(0, 49);
  u8g2.print("Set Feuchti: ");
  u8g2.print(buttonPushCounter);
  u8g2.print("%");
  u8g2.sendBuffer();
  
  statePump = digitalRead(pump);

  if (FeuchtiAvg <=  buttonPushCounter && Licht <= 450) {
    Zeit = millis();
    if ( statePump == LOW ) {
      if (Zeit - VorherigeMillis >= interval2) {
        VorherigeMillis = Zeit;
        digitalWrite(pump, HIGH);
      }
    }
    else {
      if (Zeit - VorherigeMillis >= interval1) {
        VorherigeMillis = Zeit;
        digitalWrite(pump, LOW);
      }
    }
  }
  if (buttonState1 == HIGH && buttonState2 == HIGH) {
    digitalWrite(pump, HIGH);
  }
  else{
    digitalWrite(pump, LOW);
  }

  if (statePump == HIGH) {
    u8g2.setCursor(0, 61);
    u8g2.print("Pumpe an!");
  }
  
  delay(50);
}

Working pump code only:

/*

*/

#define pumpe 13
unsigned long Zeit = 0;
unsigned long VorherigeMillis = 0;
const long interval1 = 3000;
const long interval2 = 10000;



void setup() {
     pinMode(pumpe, OUTPUT);
}

void loop() {
    if (500 <= 700 && 300 <= 450) {
    Zeit = millis();
    if ( digitalRead(pumpe) == LOW ) {
      if (Zeit - VorherigeMillis >= interval2) {
        VorherigeMillis = Zeit;
        digitalWrite(pumpe, HIGH);
      }
    }
    else {
      if (Zeit - VorherigeMillis >= interval1) {
        VorherigeMillis = Zeit;
        digitalWrite(pumpe, LOW);
      }
    }
  }
}

Please tell me why its not working...

Thanks in advance.

The code is a little difficult to follow and without knowing what your sensor is reading there is not a good way to tell what is going on.

Can you augment your code with more serial output for debugging and post the new sketch and the output from a run that doesn't work?

What is this code for? You don't have it in your short version - perhaps it is overriding the earlier code?

  if (buttonState1 == HIGH && buttonState2 == HIGH) {
    digitalWrite(pump, HIGH);
  }
  else{
    digitalWrite(pump, LOW);
  }

...R

Robin2:
What is this code for? You don't have it in your short version - perhaps it is overriding the earlier code?

  if (buttonState1 == HIGH && buttonState2 == HIGH) {

digitalWrite(pump, HIGH);
  }
  else{
    digitalWrite(pump, LOW);
  }




...R

Looks like a way to turn the pump on maybe to test it. You would have to hold both buttons down at the same time.

Hmmm, I'm almost certain this will always return true:

if (500 <= 700 && 300 <= 450)

ToddL1962:
Looks like a way to turn the pump on maybe to test it. You would have to hold both buttons down at the same time.

Indeed - but I had been hoping only the OP would answer after having given the matter a little thought.

...R

@Robin2
The code is for if you press both buttons you can activete the pump manuly if you want to.

JCA34F:
Hmmm, I'm almost certain this will always return true:

if (500 <= 700 && 300 <= 450)

Yes. It's for testing to see if it actually works. In the main code I only changed to numbers to variables from the sensor.

HyronEZ:
The code is for if you press both buttons you can activete the pump manuly if you want to.

Have you noticed that the ELSE clause always turns the pump off

You need to think about the logic more carefully if you want to implement a manual override. One way is to use the manual button to toggle a variable rather than controlling the pump directly. Then check the variable to see if the pump should be ON even if the sensor says it should be OFF

...R