You are on page 1of 9

#include <Arduino.

h>

#include <LiquidCrystal.h>

/* Library for the LCD */

#include <Wire.h>

#include "RTClib.h"

#include <Servo.h>

/* Initialize the library with interface pins numbers: */

LiquidCrystal lcd( 8 , 9 , 4 , 5 , 6 ,

7 );

int val;

/* Speaker (buzzer): */

int speakerPin =

13 ;

/* Real time clock: */

RTC_DS3231 rtc;

/* Servo: */

Servo myservo;

int pos = 0 ; /* Variable to store the servo position */

int servoPin = 10 ;

DateTime now;

byte hour, minute;

String h, m;

int feedHour = 14 ;

int feedMin = 0 ;

boolean foodTime = true;


void setup() {

/* Set speaker pin as an output pin: */

pinMode(speakerPin, OUTPUT);

/* Set up LCD's number of columns and rows: */

lcd.begin( 16 ,

2 );

/* Start RTC: */

rtc.begin(); }

void loop() {

now = rtc.now();

printTime(now);

/* LCD buttons are all connected to A0 pin with this values:

* 0-50: UP

* 50-150: RIGHT

* 150-300: DOWN

* 300-500: LEFT

* 500-750: SELECT

*/

val = analogRead(A0);

if ((val >=

500 ) && (val <=

750 )) { // SELECT

setFeedHour();

delay( 1000 );

} now = rtc.now();
printTime(now); delay( 2000 );

if ((feedHour == now.hour()) && (feedMin == now.minute()) && foodTime) {

feed();

/* We use 'foodTime' so food doesn't get served twice

* if the feed routine ends in less than a minute. */

if (feedMin != now.minute()) {

foodTime = true;

}}

void feed() {

lcd.clear();

lcd.setCursor( 0 , 0 );

lcd.print( "Sirviendo comida" );

/* Food gets served: */

/* Attach servo on pin 10 (We attach servo only when

* we need it and detach it afterwards to stop it

* from shaking) */

myservo.attach(servoPin);

/* Servo moves to open lid for food to fall through.

* 'pos' goes from 0 to 180 degrees in steps of 1 degree

* and then gets written to servo*/

for (pos = 0 ; pos <= 120 ; pos += 1 ) {

myservo.write(pos);

/* Wait 15ms for the servo to reach the position */

delay( 15 ); }
delay( 1000 );

/* Go back from 180 to 0 degrees to close the lid: */

for (pos = 120 ; pos >= 0 ; pos -=

1){

myservo.write(pos);

delay( 15 );

delay( 3000 );

/* Detach servo: */

myservo.detach();

bell(); foodTime = false;

void bell() {

for ( int i= 0 ; i< 6 ; i++) {

/* Bell sound: */

tone(speakerPin, 1915 );

delay( 700 );

tone(speakerPin, 1700 );

delay( 700 );

tone(speakerPin, 1519 );

delay( 700 );

noTone(speakerPin); lcd.clear();

lcd.setCursor( 0 , 0 );

lcd.print( "Hora de la" );

lcd.setCursor( 0 , 1 );
lcd.print( "comida!!!!" ); delay( 5000 );

now = rtc.now();

printTime(now);

delay( 5000 );

}}

/* Prints actual time and feed time.

* If hour or minute is just one digit, a zero

* will be added for formatting purposes */

void printTime(DateTime t) {

/* Actual time */

lcd.setCursor( 0 , 0 );

lcd.print( "H. actual: " );

hour = t.hour();

if (hour < 10 ) { h = "0" + String(hour); } else { h = String(hour); }

lcd.print(h);

lcd.print( ':' );

minute = t.minute();

if (minute <

10 ) { m = "0" + String(minute); } else { m = String(minute);

lcd.print(m);

/* Feed hour */

lcd.setCursor( 0 , 1 );

lcd.print( "H. comida: " );

if (feedHour <
10 ) {

h = "0" + String(feedHour);

} else {

h = String(feedHour); }

lcd.print(h);

lcd.print( ':' );

if (feedMin <

10 ) { m = "0" + String(feedMin); } else { m = String(feedMin); }

lcd.print(m);

return ;

void setFeedHour() { lcd.clear();

lcd.setCursor( 0 , 0 );

lcd.print( "Cambiar hora de" );

lcd.setCursor( 0 , 1 );

lcd.print( "comida: " );

int hourAux = feedHour;

while (true) { lcd.setCursor( 8 , 1 );

if (hourAux <

10 ) { lcd.print( "0" );

lcd.print(hourAux);

} else { lcd.print(hourAux); }

lcd.setCursor( 10 , 1 );

lcd.print( ':' );

lcd.setCursor( 11 , 1 );
if (feedMin <

10 ) {

m = "0" + String(feedMin); } else { m = String(feedMin); }

lcd.print(m);

delay( 200 );

/* We check which button is being pressed */

val=analogRead(A0);

if ((val>= 50 ) && (val<= 150 )) { /* UP + 1 hour */

hourAux = (hourAux + 1 ) %

24 ; } else if ((val>= 150 ) && (val<= 300 )) { /* DOWN - 1 hour */

hourAux = hourAux - 1 ;

if (hourAux < 0 ) {

hourAux =

23 ;

else if ((val>= 300 )&&(val<= 500 )) { /* LEFT cancel */

break ;

if ((val>= 0 )&&(val<= 50 )) { /* RIGHT save */

feedHour = hourAux;

setFeedMin();

break ;

}}

return ; }
void setFeedMin() {

lcd.clear();

lcd.setCursor( 0 , 0 );

lcd.print( "Cambiar min. de" );

lcd.setCursor( 0 , 1 );

lcd.print( "comida: " );

int minAux = feedMin;

while (true) {

lcd.setCursor( 8 , 1 );

if (feedHour < 10 ) { h = "0" + String(feedHour); } else { h = String(feedHour);

lcd.print(h);

lcd.setCursor( 10 , 1 );

lcd.print( ':' );

lcd.setCursor( 11 , 1 );

if (minAux <

10 ) {

m = "0" + String(minAux); } else {

m = String(minAux); }

lcd.print(m); delay( 200 );

/* We check which button is being pressed */

val = analogRead(A0);

if ((val>= 50 ) && (val<= 150 )) { /* UP + 1 min */

minAux = (minAux + 1 ) %

60 ; } else if
((val>= 150 ) && (val<= 300 )) { /* DOWN - 1 min */

minAux = minAux - 1 ;

if (minAux < 0 ) {

minAux =

59 ;

} else if

((val>= 300 ) && (val<= 500 )) { /* LEFT cancel */

break ;

} else if

((val>= 500 ) &&( val<= 750 )) { /* SELECT save */

feedMin = minAux;

break ;

}}

return ; }

You might also like