Skip to content

Instantly share code, notes, and snippets.

@oscarmorrison
Last active November 14, 2021 19:32
Show Gist options
  • Save oscarmorrison/81fa0fad92d8a36e307b26af07fde50b to your computer and use it in GitHub Desktop.
Save oscarmorrison/81fa0fad92d8a36e307b26af07fde50b to your computer and use it in GitHub Desktop.
Python Cat Feeder two servos
#blog.oscarmorrison.com
#Cat feeder (open and close server)
#change the pin, and port here.
from flask import Flask, jsonify
import RPi.GPIO as IO
import time
pinOutOne = 4
#set this two whatever your 2nd servo is in
pinOutTwo = 5
app = Flask(__name__)
@app.route('/')
def index():
return jsonify(response='pinging from the cat feeder!')
@app.route('/open')
def openRouteWithDefaultTime():
time = 0.5
openFeeder(time)
return jsonify(response='feeding the kitty '+str(time))
@app.route('/open/<time>')
def openRoute(time):
time = float(time)
openFeeder(time)
return jsonify(response='feeding the kitty '+str(time))
def openFeeder(wait):
print('Opening cat feeder for ', wait)
IO.setwarnings(False)
IO.setmode (IO.BCM)
IO.setup(pinOutOne,IO.OUT)
IO.setup(pinOutTwo,IO.OUT)
pwmOne = IO.PWM(pinOutOne,50)
pwmTwo = IO.PWM(pinOutTwo,50)
pwmOne.start(2.5)
pwmTwo.start(2.5)
time.sleep(0.5)
pwmOne.ChangeDutyCycle(7.5)
pwmTwo.ChangeDutyCycle(7.5)
time.sleep(wait)
pwmOne.ChangeDutyCycle(2.5)
pwmTwo.ChangeDutyCycle(2.5)
time.sleep(0.5)
pwmOne.stop(2.5)
pwmTwo.stop(2.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment