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.