Skip to content

Instantly share code, notes, and snippets.

@ulitiy
Last active October 5, 2023 17:30
Show Gist options
  • Save ulitiy/25d675591af73f347926b7a4e6921797 to your computer and use it in GitHub Desktop.
Save ulitiy/25d675591af73f347926b7a4e6921797 to your computer and use it in GitHub Desktop.
FatPetFeeder every 12 hours
#include <Servo.h>
#define second 1000
#define minute 60000
#define hour 3600000
Servo servo;
const int servoPin = 9;
const int buttonDispensePin = 7;
const int buttonMinusPin = 6;
const int buttonPlusPin = 8;
const int minMoveTime = 130;
const int minServing = 300;
const int ledPin = 13;
int servingTime = 1400; // 1000 – 9g, 2000 – 18g, 3000 – 27g, 3500 – 31g, 4000 - 36g
int servingDelta = 150;
int jerkPeriod = 700; // 1000/1000 – 9g, 1000/500 – 7g, 1400/700 – 12g
unsigned long lastFeeding=0;
void setup ()
{
Serial.begin(57600);
Serial.println();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(buttonDispensePin, INPUT_PULLUP);
pinMode(buttonMinusPin, INPUT_PULLUP);
pinMode(buttonPlusPin, INPUT_PULLUP);
buzz();
}
void loop ()
{
int dispenseVal = digitalRead(buttonDispensePin);
if(dispenseVal == LOW)
dispense(servingTime);
int plusVal = digitalRead(buttonPlusPin);
if(plusVal == LOW){
delay(1000);
int plusVal = digitalRead(buttonPlusPin);
if(plusVal == LOW){
servingTime+=servingDelta;
buzz();
}
}
int minusVal = digitalRead(buttonMinusPin);
if(minusVal == LOW){
delay(1000);
int minusVal = digitalRead(buttonMinusPin);
if(minusVal == LOW){
servingTime-=servingDelta;
servingTime = servingTime<minServing?minServing:servingTime;
buzz();
}
}
if(millis() - lastFeeding>12*hour)//
dispense(servingTime);
delay(10);
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void dispense(int dispenseTime)
{
lastFeeding = millis();
servo.attach(servoPin);
for(;dispenseTime>0;dispenseTime-=jerkPeriod){
servo.write(100);
delay(dispenseTime>=jerkPeriod ? jerkPeriod : dispenseTime%1000);
jerk();
}
stop();
}
void jerk()
{
servo.write(0);
delay(minMoveTime);
servo.write(180);
delay(minMoveTime);
}
void stop()
{
servo.write(90);
delay(100);
servo.detach();
}
void buzz()
{
servo.attach(servoPin);
servo.write(0);
delay(50);
jerk();
jerk();
stop();
}
@ulitiy
Copy link
Author

ulitiy commented Jun 28, 2021

@AdvocateAnarchy

I can't figure the code

Please be specific with your question, what issues do you have?
If you don't know how to code, you have to learn or just use the code as is if it suits your needs.

https://gist.github.com/ulitiy/a2b55c8d6ff86dca5956551208b76921

I bet this code will be much easier to understand.

@AdvocateAnarchy
Copy link

This code you linked is much more noob-friendly :P I'll study it up. Thank you.

@AtlasKaiser
Copy link

Hi, i m very much interested in this! I would like to make the esp connected to wifi and control the machine remotely via my mobile device! let s say I'm in a park and wanna use my phone to fed! please sempai teach me!

@Hacker-log4j2
Copy link

well @AtlasKaiser\ this is how you would make it for example with apple home kit use this link https://github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266

then open the folder and open the project " Example06_makesmart_lock"
this is in the examples
then paste the code from here to control the servo:

#include <Servo.h>

Servo myservo;

const int servoPin = 9;
const int ledPin = 13;

void setup() {
myservo.attach(servoPin);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
myservo.write(180);
delay(1000);
myservo.detach();
}

void loop() {
myservo.attach(servoPin);
myservo.write(30);
delay(575);
myservo.write(180);
delay(1500);
myservo.detach();
delay(3600000);
}

and there you go just enter your wifi username and password in the given space and pair. it is pretty simple to open the home app on an ios device then add accessory then select don't have the setup code and enter 12345678 it will take some time so keep your device close and you should have a fully working IOT cat feeder

make sure you use esp8266
I assume you know how to set it up with Arduino IDE
other things you should know if you unplug it then you will have to pair it one more I IDK why tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment