4
0
Fork 0
mirror of https://github.com/zzzzDev4/IAS-Better-Tea.git synced 2025-04-21 07:31:20 +02:00

Replaced servo with continuous servo and implemented utils for simple control

This commit is contained in:
zzzz 2024-04-30 00:56:50 +02:00
parent b61c79f4d5
commit 1fe3ec02a3
5 changed files with 78 additions and 22 deletions

40
src/ContinuousServo.cpp Normal file
View file

@ -0,0 +1,40 @@
#include "ContinuousServo.hpp"
// 95 -> neutral, 0 or 180 -> full speed
ContinuousServo::ContinuousServo(int pin)
{
myservo.attach(pin);
myservo.write(NEUTRAL_POS);
}
void ContinuousServo::moveServoForMillisClockwise(unsigned long duration)
{
if (m_isTurning)
{
return;
}
myservo.write(NEUTRAL_POS + 50);
m_endTurningTime = millis() + duration;
m_isTurning = true;
}
void ContinuousServo::moveServoForMillisCounterClockwise(unsigned long duration)
{
if (m_isTurning)
{
return;
}
myservo.write(NEUTRAL_POS - 50);
m_endTurningTime = millis() + duration;
m_isTurning = true;
}
void ContinuousServo::tick()
{
if (millis() >= m_endTurningTime && m_isTurning)
{
// stop servo when time limit reached
myservo.write(NEUTRAL_POS);
m_isTurning = false;
}
}

18
src/ContinuousServo.hpp Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include <Servo.h>
class ContinuousServo
{
public:
ContinuousServo(int pin);
void moveServoForMillisClockwise(unsigned long duration);
void moveServoForMillisCounterClockwise(unsigned long duration);
void tick();
bool isTurning() { return m_isTurning; }
private:
Servo myservo;
bool m_isTurning = false;
unsigned long m_endTurningTime;
const int NEUTRAL_POS = 95;
};

View file

@ -1,6 +1,6 @@
#include "TeaTimer.hpp" #include "TeaTimer.hpp"
void TeaTimer::init(SmartServo *t_servo) void TeaTimer::init(ContinuousServo *t_servo)
{ {
m_servo = t_servo; m_servo = t_servo;
} }
@ -10,7 +10,7 @@ void TeaTimer::beginSteeping(unsigned long t_steepingDuration)
{ {
if (m_isSteeping) if (m_isSteeping)
return; return;
m_servo->moveServoTo180(); m_servo->moveServoForMillisClockwise(2000);
m_endSteepingTime = millis() + t_steepingDuration; m_endSteepingTime = millis() + t_steepingDuration;
m_isSteeping = true; m_isSteeping = true;
} }
@ -18,14 +18,15 @@ void TeaTimer::beginSteeping(unsigned long t_steepingDuration)
// Move servo up, stop timer immediately. // Move servo up, stop timer immediately.
void TeaTimer::end_steeping() void TeaTimer::end_steeping()
{ {
m_servo->moveServoToZero(); m_servo->moveServoForMillisCounterClockwise(2000);
m_isSteeping = false; m_isSteeping = false;
} }
// Called at start of update. // Called at start of update.
void TeaTimer::tick() void TeaTimer::tick()
{ {
if (millis() >= m_endSteepingTime) m_servo->tick();
if (millis() >= m_endSteepingTime && m_isSteeping)
{ {
end_steeping(); end_steeping();
} }

View file

@ -1,11 +1,12 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include "SmartServo.hpp" #include "SmartServo.hpp"
#include "ContinuousServo.hpp"
class TeaTimer class TeaTimer
{ {
public: public:
void init(SmartServo *t_servo); void init(ContinuousServo *t_servo);
// Move servo down, start timer. // Move servo down, start timer.
void beginSteeping(unsigned long t_steepingDuration); void beginSteeping(unsigned long t_steepingDuration);
// Move servo up, stop timer immediately. // Move servo up, stop timer immediately.
@ -13,14 +14,11 @@ public:
// Called at start of update. // Called at start of update.
void tick(); void tick();
unsigned long remainingSteepTimeSeconds(); unsigned long remainingSteepTimeSeconds();
bool isSteeping() bool isSteeping() { return m_isSteeping; };
{
return m_isSteeping;
};
private: private:
bool m_isSteeping = false; bool m_isSteeping = false;
// The time in millis when steeping is done. // The time in millis when steeping is done.
unsigned long m_endSteepingTime; unsigned long m_endSteepingTime;
SmartServo *m_servo; ContinuousServo *m_servo;
}; };

View file

@ -6,6 +6,7 @@
#include "TeaTimer.hpp" #include "TeaTimer.hpp"
#include "SmartServo.hpp" #include "SmartServo.hpp"
#include "ContinuousServo.hpp"
#define WIRE Wire #define WIRE Wire
@ -15,14 +16,14 @@
auto display = Adafruit_SSD1306(128, 32, &WIRE); auto display = Adafruit_SSD1306(128, 32, &WIRE);
SmartServo smartServo(SERVO_PIN); ContinuousServo contServo(SERVO_PIN);
TeaTimer teaTimer; TeaTimer teaTimer;
void setup() void setup()
{ {
// Serial.begin(9600); // Serial.begin(9600);
teaTimer.init(&smartServo); teaTimer.init(&contServo);
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
pinMode(TOUCH_BTN_PIN, INPUT); pinMode(TOUCH_BTN_PIN, INPUT);
@ -33,8 +34,6 @@ void setup()
// internally, this will display the splashscreen. // internally, this will display the splashscreen.
display.display(); display.display();
delay(1000); delay(1000);
smartServo.moveServoToZero();
} }
void printMsg(const String &s) void printMsg(const String &s)
@ -58,27 +57,27 @@ void printMsg(unsigned long l)
bool isBtnPressed() bool isBtnPressed()
{ {
if (millis() < 10000) return false; if (millis() < 10000)
return false;
return digitalRead(TOUCH_BTN_PIN) == HIGH; return digitalRead(TOUCH_BTN_PIN) == HIGH;
} }
void loop() void loop()
{ {
teaTimer.tick(); teaTimer.tick();
if (isBtnPressed()) if (isBtnPressed() && !teaTimer.isSteeping())
{ {
printMsg("BUTTON");
teaTimer.beginSteeping(5000); teaTimer.beginSteeping(5000);
} }
else if (!teaTimer.isSteeping()) if (teaTimer.isSteeping())
{
printMsg("Default");
}
else
{ {
auto r = teaTimer.remainingSteepTimeSeconds(); auto r = teaTimer.remainingSteepTimeSeconds();
printMsg(r); printMsg(r);
} }
else
{
printMsg("Default");
}
} }
void toggle_led() void toggle_led()