Skip to content

Instantly share code, notes, and snippets.

@naiaden
Last active October 10, 2017 20:18
Show Gist options
  • Select an option

  • Save naiaden/2632d05e82e195bfde475f91c298622d to your computer and use it in GitHub Desktop.

Select an option

Save naiaden/2632d05e82e195bfde475f91c298622d to your computer and use it in GitHub Desktop.
Kattenvoederautomaat
#include <Adafruit_SoftServo.h> // SoftwareServo (works on non PWM pins)
const int _foodServoPin = 0;
const int _foodButtonPin = 1;
Adafruit_SoftServo foodServo;
int servoAngle = 0;
int foodButtonState = 0;
unsigned long lastServe;
unsigned long lastPress;
bool buttonPressBuffer = false;
void setup()
{
OCR0A = 0xAF;
TIMSK |= _BV(OCIE0A);
pinMode(_foodButtonPin, INPUT);
lastServe = millis();
lastPress = millis();
foodServo.attach(_foodServoPin);
foodServo.write(1);
delay(25);
}
void loop()
{
foodButtonState = digitalRead(_foodButtonPin);
if(foodButtonState == HIGH)
{
buttonPressBuffer = true;
lastPress = millis();
}
if(millis() - lastPress > 2000)
{
buttonPressBuffer = false;
}
if(millis() - lastServe > 5000)
{
foodButtonState = digitalRead(_foodButtonPin);
if(buttonPressBuffer/*foodButtonState == HIGH*/)
{
servoAngle = 180;
foodServo.write(servoAngle);
delay(500);
servoAngle = 1;
foodServo.write(servoAngle);
delay(25);
}
buttonPressBuffer = false;
lastServe = millis();
}
}
// We'll take advantage of the built in millis() timer that goes off
// to keep track of time, and refresh the servo every 20 milliseconds
// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
// Called by the microcontroller every 2 milliseconds
volatile uint8_t counter = 0;
SIGNAL(TIMER0_COMPA_vect) {
// this gets called every 2 milliseconds
counter += 2;
// every 20 milliseconds, refresh the servos!
if (counter >= 20) {
counter = 0;
foodServo.refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment