#include <Servo.h>
#include <ezButton.h>
#define fsrPin A0
constexpr uint8_t openPin = 7; // open door
constexpr uint8_t closePin = 8; // force close
constexpr uint8_t servoPin = 9;
const int BUTTON_PIN = 7;
ezButton button(BUTTON_PIN);
int fsrReading;
// make your own servo class
class SlowServo {
protected:
uint16_t target = 110; // target angle
uint16_t current = 110; // current angle
uint8_t interval = 30; // delay time
uint32_t previousMillis = 0;
public:
Servo servo;
//-----------------------------------------------------------------------
void begin(byte pin)
{
servo.attach(pin);
}
//-----------------------------------------------------------------------
void setSpeed(uint8_t newSpeed)
{
interval = newSpeed;
}
//-----------------------------------------------------------------------
void set(uint16_t newTarget)
{
target = newTarget;
}
//-----------------------------------------------------------------------
void update()
{
if (millis() - previousMillis > interval)
{
previousMillis = millis();
if (target < current)
{
current--;
servo.write(current);
}
else if (target > current)
{
current++;
servo.write(current);
}
}
}
};
//-----------------------------------------------------------------------
SlowServo myservo; // create a servo object
//-----------------------------------------------------------------------
void setup() {
Serial.begin(9600);
myservo.begin(servoPin); // start the servo object
pinMode(openPin, INPUT_PULLUP);
pinMode(closePin, INPUT_PULLUP);
button.setDebounceTime(100);
Serial.println("digital cat feeder");
doorClose();
}
//-----------------------------------------------------------------------
void doorOpen()
{
myservo.set(90);
}
//-----------------------------------------------------------------------
void doorClose()
{
myservo.set(0);
}
//-----------------------------------------------------------------------
void loop() {
button.loop();
fsrReading = analogRead(fsrPin);
//Serial.println ("Analog Reading = ");
//Serial.println (fsrReading);
if (digitalRead(openPin) == LOW) {
Serial.println(F("open"));
doorOpen();
}
if (digitalRead(closePin) == LOW) {
Serial.println(F("close"));
doorClose();
}
if (digitalRead(openPin) == HIGH && digitalRead(closePin) == HIGH )
{
if (fsrReading < 700) {
doorClose();
}
if (fsrReading > 700 && fsrReading < 900) {
Serial.println("snowy - open cover");
doorOpen ();
}
if (fsrReading > 900) {
Serial.println("zelda - close cover");
doorClose();
}
}
myservo.update(); // call the update method in loop
}