Das ist “Tank.h”
#pragma once
#include <Arduino.h>
class Tank {
public:
Tank();
~Tank();
void init(byte pinTankFull, byte pinTankEmpty,byte pinMotorOpenValve, byte pinMotorCloseValve);
bool getTankFull();
bool getTankEmpty();
void openValve();
void closeValve();
private:
bool _tankSensorFull;
bool _tankSensorEmpty;
byte _pinTankFull;
byte _pinTankEmpty;
byte _pinMotorOpenValve;
byte _pinMotorCloseValve;
};
Das ist “Tank.cpp”
#include <Arduino.h>
#include <Tank.h>
void Tank::init(byte pinTankFull, byte pinTankEmpty, byte pinMotorOpenValve, byte pinMotorCloseValve)
{
_pinTankFull = pinTankFull; // Assign the pin numbers to your Arduino Uno, Nano, Mega, ESP32, ESP8266 pins
_pinTankEmpty = pinTankEmpty;
_pinMotorOpenValve = pinMotorOpenValve;
_pinMotorCloseValve = pinMotorCloseValve;
// I don't know how your sensors work.
// You might need to change it to pinMode(_pinTankFull, INPUT_PULLUP)
// if your sensors are normally closed and you want to use the internal pull-up resistors.
pinMode(_pinTankFull, INPUT); // Set the pin mode for the tank full sensor
pinMode(_pinTankEmpty, INPUT); // Set the pin mode for the tank empty sensor
pinMode(_pinMotorOpenValve, OUTPUT); // Set the pin mode for the motor open valve
pinMode(_pinMotorCloseValve, OUTPUT); // Set the pin mode for the motor close valve
digitalWrite(_pinMotorOpenValve, LOW); // Initialize the motor open valve to LOW
digitalWrite(_pinMotorCloseValve, LOW); // Initialize the motor close valve to LOW
}
bool Tank::getTankFull()
{
// You need to adjust it:
// Depending on whether the sensor is normally closed or normally open.
// For example, if the sensor is normally closed, you might want to invert the reading:
// _tankSensorFull = !digitalRead(_pinTankFull);
_tankSensorFull = digitalRead(_pinTankFull);
return _tankSensorFull;
}
bool Tank::getTankEmpty()
{
_tankSensorEmpty = digitalRead(_pinTankEmpty);
// You need to adjust it,
// depending on whether the sensor is normally closed or normally open.
// For example, if the sensor is normally closed, you might want to invert the reading:
// _tankSensorEmpty = !digitalRead(_pinTankEmpty);
return _tankSensorEmpty;
}
void Tank::openValve()
{
// Interlock to prevent simultaneous opening and closing operations.
digitalWrite(_pinMotorOpenValve, HIGH);
digitalWrite(_pinMotorCloseValve, LOW);
}
void Tank::closeValve()
{
// Interlock to prevent simultaneous opening and closing operations.
digitalWrite(_pinMotorOpenValve, LOW);
digitalWrite(_pinMotorCloseValve, HIGH);
}
Tank::Tank()
{
// Here, too, you need to specify the normal state of your sensors in the init method.
// For example, if the sensors are normally closed, you might want to initialize them as true:
_tankSensorFull = false;
_tankSensorEmpty = false;
}
Tank::~Tank()
{
}
Das ist dein Arduino-Programm “main.cpp
#include <Arduino.h>
#include <Tank.h>
// das musst du für dein Projekt anpassen, je nachdem, wie du die Pins auf deinem Arduino Uno, Nano, Mega, ESP32, ESP8266 angeschlossen hast.
const byte pinTankFull = 2; // Pin für den "Tank voll" Sensor
const byte pinTankEmpty = 3; // Pin für den "Tank leer" Sensor
const byte pinMotorOpenValve = 4; // Pin für das Öffnen des Ventils
const byte pinMotorCloseValve = 5; // Pin für das Schließen des Ventils
const byte pinTankFull2 = 6; // Pin für den "Tank voll" Sensor
const byte pinTankEmpty2 = 7; // Pin für den "Tank leer" Sensor
const byte pinMotorOpenValve2 = 8; // Pin für das Öffnen des Ventils
const byte pinMotorCloseValve2 = 9; // Pin für das Schließen des Ventils
// Hier kannst du weitere Tanks hinzufügen, indem du weitere Instanzen der Tank-Klasse erstellst und die entsprechenden Pins zuweist.
Tank MyTank1;
Tank MyTank2;
void setup()
{
Serial.begin(115200); // Starte die serielle Kommunikation mit 115200 Baudrate
MyTank1.init(pinTankFull, pinTankEmpty, pinMotorOpenValve, pinMotorCloseValve); // Initialisiere den ersten Tank mit den entsprechenden Pins
MyTank2.init(pinTankFull2, pinTankEmpty2, pinMotorOpenValve2, pinMotorCloseValve2); // Initialisiere den zweiten Tank mit den entsprechenden Pins
// Hier kannst du weitere Tanks initialisieren, indem du die init-Methode für jede Tank-Instanz aufrufst und die entsprechenden Pins übergibst.
}
void loop()
{
// Hier kannst du die Logik implementieren, um die Sensoren zu überwachen und die Ventile entsprechend zu steuern.
// Zum Beispiel:
if (MyTank1.getTankFull())
{
MyTank1.closeValve(); // Schließe das Ventil, wenn der Tank voll ist
}
else if (MyTank1.getTankEmpty())
{
MyTank1.openValve(); // Öffne das Ventil, wenn der Tank leer ist
}
if (MyTank2.getTankFull())
{
MyTank2.closeValve(); // Schließe das Ventil, wenn der Tank voll ist
}
else if (MyTank2.getTankEmpty())
{
MyTank2.openValve(); // Öffne das Ventil, wenn der Tank leer ist
}
// Hier kannst du weitere Logik hinzufügen, um andere Tanks zu überwachen und zu steuern.
delay(1000); // Warte 1 Sekunde bevor die Sensoren erneut überprüft werden
}