Skip to content
Snippets Groups Projects
Commit 1939a37f authored by Andreas Dominic Zahnd's avatar Andreas Dominic Zahnd
Browse files

uebung oop gelöst

parent 03708d30
Branches main
No related tags found
No related merge requests found
......@@ -4,55 +4,58 @@
#include "Flowerpot.h"
// TODO: Implement the constructor
Flowerpot::Flowerpot(int threshold, uint8_t displayAddress, int valvePin)
: threshold{threshold}, displayAddress{displayAddress}, valvePin{valvePin}, moistureSensor{displayAddress}
{}
// TODO: Uncomment the following code. begin(), loop(), observe(), and watering() are already implemented
// void Flowerpot::begin()
// {
// moistureSensor.begin();
// valve.attach(valvePin, 1000, 2000);
// valve.write(valvePos);
// }
void Flowerpot::begin()
{
moistureSensor.begin();
valve.attach(valvePin, 1000, 2000);
valve.write(valvePos);
}
// void Flowerpot::loop()
// {
// switch (state)
// {
// case S_OBSERVE:
// observe();
// if (threshold > moisture) {
// state = T_ENTERWATERING;
// }
// break;
// case T_ENTERWATERING:
// valve.write(valvePos++);
// if (valvePos > 90) {
// state = S_WATERING;
// }
// break;
// case S_WATERING:
// watering();
// tickCount++;
// if (tickCount>=100){
// tickCount = 0;
// state = T_ENTEROBSERVE;
// }
// break;
// case T_ENTEROBSERVE:
// valve.write(valvePos--);
// if (valvePos < 0) {
// state = S_OBSERVE;
// }
// break;
// }
// }
void Flowerpot::loop()
{
switch (state)
{
case S_OBSERVE:
observe();
if (threshold > moisture) {
state = T_ENTERWATERING;
}
break;
case T_ENTERWATERING:
valve.write(valvePos++);
if (valvePos > 90) {
state = S_WATERING;
}
break;
case S_WATERING:
watering();
tickCount++;
if (tickCount>=100){
tickCount = 0;
state = T_ENTEROBSERVE;
}
break;
case T_ENTEROBSERVE:
valve.write(valvePos--);
if (valvePos < 0) {
state = S_OBSERVE;
}
break;
}
}
// void Flowerpot::observe()
// {
// moisture = moistureSensor.getSoilMoisture();
// }
void Flowerpot::observe()
{
moisture = moistureSensor.getSoilMoisture();
}
// void Flowerpot::watering()
// {
// moisture = moistureSensor.watering();
// }
void Flowerpot::watering()
{
moisture = moistureSensor.watering();
}
// Flowerpot.h
// TODO: Declare the Flowerpot class according to the class diagram here
#pragma once
#include "MoistureSensor.h"
#include <Servo.h>
class Flowerpot {
public:
Flowerpot(int threshold, uint8_t displayAddress, int valvePin);
void begin();
void loop();
private:
enum ValveStates{S_OBSERVE,T_ENTERWATERING,S_WATERING,T_ENTEROBSERVE};
int threshold;
int valvePin;
int moisture=0;
int tickCount=0;
int valvePos=0;
uint8_t displayAddress;
ValveStates state;
Servo valve;
MoistureSensor moistureSensor;
void observe();
void watering();
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment