1#include "RTClib.h"
2#include <Servo.h>
3#include <Adafruit_NeoPixel.h>
4#ifdef __AVR__
5 #include <avr/power.h>
6#endif
7
8
9#define LOOP_DELAY 10000
10#define FEEDING_DELAY 1400
11#define BUTTON_PIN 3
12#define LED_COUNT 1
13#define LED_PIN 2
14#define BRIGHTNESS 100
15#define SERVO_MOVEMENT_SPEED 2400
16#define SERVO_PIN 8
17
18
19Servo myservo;
20Adafruit_NeoPixel led(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
21RTC_DS1307 rtc;
22
23
24int nextDayForFeeding =-1;
25bool foodWasGiven = false;
26const int HOUR_TO_FEED = 10;
27
28
29void setup () {
30 while (!Serial);
31 pinMode(LED_PIN, OUTPUT);
32 pinMode(BUTTON_PIN, INPUT);
33 led.begin();
34 led.setBrightness(BRIGHTNESS);
35 setLed(255,255,255);
36 Serial.begin(57600);
37 if (! rtc.begin()) {
38 Serial.println("Couldn't find RTC");
39 setLed(255,0,0);
40 while (1);
41 }
42 if (! rtc.isrunning()) {
43 Serial.println("RTC is NOT running!");
44 setLed(255,0,0);
45 rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
46 }
47 versionView();
48}
49
50
51void versionView(){
52 setLed(255,255,255);
53 delay(1000);
54 setLed(255,0,255);
55 delay(1000);
56 setLed(0,255,255);
57 delay(1000);
58}
59
60void setLed(int r, int g, int b){
61 led.clear();
62 led.setPixelColor(0, led.Color(r, g, b));
63 led.show();
64}
65
66
67void feedTheFish(int currDay){
68 setLed(0,0,255);
69 myservo.attach(SERVO_PIN);
70 myservo.writeMicroseconds(SERVO_MOVEMENT_SPEED);
71 delay(FEEDING_DELAY);
72 myservo.detach();
73 foodWasGiven = true;
74 nextDayForFeeding = nextDayForFeeding+1;
75}
76
77void loop () {
78 setLed(0,255,0);
79 int buttonState = digitalRead(BUTTON_PIN);
80
81 DateTime now = rtc.now();
82
83 if(nextDayForFeeding == -1 || (nextDayForFeeding > 6 && now.dayOfTheWeek() == 0) || nextDayForFeeding < now.dayOfTheWeek()){
84 nextDayForFeeding = now.dayOfTheWeek();
85 }
86
87 if(nextDayForFeeding == now.dayOfTheWeek()){
88 foodWasGiven = false;
89 }
90
91 if(HOUR_TO_FEED == now.hour() && foodWasGiven == false){
92 feedTheFish(now.dayOfTheWeek());
93 }
94
95 if (buttonState == HIGH) {
96 nextDayForFeeding = now.dayOfTheWeek();
97 feedTheFish(now.dayOfTheWeek());
98 }
99 delay(LOOP_DELAY);
100}