Skip to content

Instantly share code, notes, and snippets.

@macbury
Created November 24, 2012 17:24
Show Gist options
  • Save macbury/4140611 to your computer and use it in GitHub Desktop.
Save macbury/4140611 to your computer and use it in GitHub Desktop.
#include <Servo.h>
#include <Bounce.h>
Servo silnik;
byte LED_R = 9;
byte LED_G = 11;
byte LED_B = 10;
byte SILNIK_PIN = 2;
byte RESET_BUTTON_PIN = 4;
unsigned int REFRESH_EVERY_SECONDS = 6 * 60 * 60;
Bounce resetBouncer = Bounce( RESET_BUTTON_PIN, 25 );
unsigned int SERVO_START_POSITION = 450;
unsigned int SERVO_END_POSITION = 2450;
void setup() {
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
pinMode(RESET_BUTTON_PIN, INPUT);
silnik.attach(SILNIK_PIN);
Serial.begin(9600);
}
int state = 0;
int seconds = 0;
void led_color_RGB(byte r, byte g, byte b) {
analogWrite(LED_R, r);
analogWrite(LED_G, g);
analogWrite(LED_B, b);
}
void state_initial() {
Serial.print("Initial state\n");
led_color_RGB(255, 0, 0);
silnik.writeMicroseconds(SERVO_START_POSITION);
led_color_RGB(255, 0, 0);
seconds = 0;
state = 1;
}
void process_reset_button() {
resetBouncer.update ();
if(resetBouncer.read() == HIGH) {
Serial.print("Pressed red button\n");
state = 3;
}
}
void waiting_mode() {
if (seconds >= REFRESH_EVERY_SECONDS) {
state = 3;
} else {
blink_led();
}
}
void blink_led() {
if(seconds % 2 == 0) {
led_color_RGB(0, 0, 255);
} else {
led_color_RGB(0, 0, 0);
}
Serial.print("Seconds: \n");
Serial.print(seconds);
Serial.print("\n");
seconds++;
delay(1000);
}
void feed_cat() {
Serial.print("Feeding cat\n");
seconds = 0;
led_color_RGB(0, 255, 0);
delay(1000);
silnik.writeMicroseconds(SERVO_END_POSITION);
delay(5000);
silnik.writeMicroseconds(SERVO_START_POSITION);
delay(1000);
led_color_RGB(255, 255, 255);
delay(2000);
state = 2;
}
void loop() {
switch(state) {
case 0:
state_initial();
break;
case 1: // waiting for reset button
process_reset_button();
break;
case 2:
process_reset_button();
waiting_mode();
break;
case 3:
feed_cat();
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment