I am new to Arduino and am trying to make a dog feeder. Using a UNO R3, 16*2 LCD, and 3 tactile push buttons.
Time.h
TimeAlarms.h
LiquidCrystal.h
It will have clock function that is user adjustable. Two feeding times that are user adjustable. The alarm times are to be displayed at the same time as the clock time.
The LED is for prototyping purposes will later be replaced with a 12v motor to run an auger for the feeding
I have an RTC on order so that will eliminate the need for the LCD clear timer.
I can change through the modes 0-4
however modes 3 and 4 will not work the same code in anything 2 or less will work
Why?
I can change the time, and the time the alarm DISPLAYS however when the clock hits the DISPLAYED alarm time nothing happens. And when the time hits the ORIGINAL alarm time the alarm is triggered.
Why does the ORIGINAL alarm time not update with the DISPLAYED alarm time?
here is the code
#include <Time.h>
#include <TimeAlarms.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const int buttonMin = 5; //assigns BUTTON3 to pin 5
const int buttonHr = 6; //assigns BUTTON4 to pin 6
const int LED = 13; //sets LED to pin 13
const int AlarmPOT1 = A0; // sets POT input to pin A0
unsigned int val1 = 0; //variable int read form pot
const int AlarmPOT2 = A1; // sets POT input to pin A0
unsigned int val2 = 0; //variable int read form pot
int val3 = 8; //morning feed hour var
int val4 = 00; //morning feed min var
int val5 = 20; //evening feed hour var
int val6 = 00; //evening feed min var
const int ModeButton = 2; // ModeButton is connected to pin 2
int ModeVal; // variable for reading the pin status
int ModeVal2; // variable for reading the delayed status
int ModebuttonState; // variable to hold the button state
int SelectMode = 0; // What mode is the light in?
int incrementState = 0;
int lastIncrementState = 0;
int incrementState2 = 0;
int lastIncrementState2 = 0;
int incrementState3 = 0;
int lastIncrementState3 = 0;
int incrementState4 = 0;
int lastIncrementState4 = 0;
void setup()
{
pinMode(buttonHr, INPUT); //sets pin 5 input
pinMode(buttonMin, INPUT); //sets pin 6 input
pinMode(LED, OUTPUT); //LED is OUTPUT
pinMode(AlarmPOT1, INPUT); //POT is INPUT
pinMode(AlarmPOT2, INPUT); //POT is INPUT
digitalWrite(buttonHr, HIGH); // turn on pullup resistor
digitalWrite(buttonMin, HIGH); // turn on pullup resistor
pinMode(ModeButton, INPUT); // Set the switch pin as input
digitalWrite(ModeButton, HIGH); // turn on pullup resistor
ModebuttonState = digitalRead(ModeButton); // read the initial state
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.print("Auto dog v1.27"); // Print a message to the LCD.
delay(750);
lcd.clear();
digitalWrite(LED, LOW); // turn LED OFF
setTime(8,29,00,8,24,13); // set time to Saturday 8:29:00am Aug 24 2013
// create the alarms
Alarm.timerRepeat(60, resetLCD); // timer for every 60 seconds
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
Alarm.alarmRepeat(val3,val4,00, MorningFeeding); // 8:00am every day
Alarm.alarmRepeat(val5,val6,00,EveningFeeding); // 8:00pm every day
val1=analogRead(AlarmPOT1);
val2=analogRead(AlarmPOT2);
}
// functions to be called when an alarm triggers:
void MorningFeeding(){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Morning Feeding");
digitalWrite(LED, HIGH); // turn LED ON
Alarm.delay(val1*59);
digitalWrite(LED, LOW); // turn LED OFF
lcd.clear();
}
void EveningFeeding(){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Night Feeding");
digitalWrite(LED, HIGH); // turn LED ON
Alarm.delay(val2*59);
digitalWrite(LED, LOW); // turn LED OFF
lcd.clear();
}
void resetLCD(){ //clears unused numbers from LCD
lcd.clear();
}
void digitalClockDisplay()
{
// digital clock display of the time
lcd.setCursor(0,0);
lcd.print("TIME");
lcd.print(":");
lcd.print(hour());
lcd.setCursor(7,0);
lcd.print(":");
lcd.print(minute());
lcd.setCursor(10,0);
lcd.print(":");
lcd.print(second());
lcd.setCursor(0,1);
lcd.print("MF");
lcd.setCursor(2,1);
lcd.print(val3);
lcd.setCursor(4,1);
lcd.print(":");
lcd.setCursor(5,1);
lcd.print(val4);
lcd.setCursor(9,1);
lcd.print("AF");
lcd.setCursor(11,1);
lcd.print(val5);
lcd.setCursor(13,1);
lcd.print(":");
lcd.setCursor(14,1);
lcd.print(val6);
lcd.setCursor(15,0);
lcd.print(SelectMode);
{
ModeVal = digitalRead(ModeButton); // read input value and store it in val
ModeVal2 = digitalRead(ModeButton);// read input again to check for bounces
if (ModeVal == ModeVal2) { // make sure got 2 consistant readings!
if (ModeVal != ModebuttonState) { // the button state has changed!
if (ModeVal == LOW) { // check if the button is pressed
if (SelectMode == 0) { //Do nothing MODE
SelectMode = 1; //go to mode 1
lcd.clear(); // clear LCD
}
else {
if (SelectMode == 1) { // Turn Backlight on or off
SelectMode = 2; // go to mode 2
lcd.clear(); // clear LCD
}
else {
if (SelectMode == 2) { //Set time MODE
SelectMode = 3; // go to mode 3
lcd.clear(); // clear LCD
}
else {
if (SelectMode == 3) { //Set Morning Feeding
SelectMode = 4; // go to mode 4
lcd.clear(); // clear LCD
}
else {
if (SelectMode == 4) { // Set Night Feeding
SelectMode = 0; // go to mode 0
lcd.clear(); // clear LCD
}
}
}
}
}
}
ModebuttonState = ModeVal; // save the new state in our variable
}}}
// Now do whatever the lightMode indicates
if (SelectMode == 0) //Do nothing MODE
{ { incrementState3 = digitalRead(buttonHr);
if(incrementState3 != lastIncrementState3) //compare increment button state to its last state
{
if(incrementState3 == LOW)//increment button is pressed
{
val5 = val5 + 1; //increment the counter
Alarm.delay(20); //debounce delay
if(val5 == 24) {val5 = 0; };
lcd.clear();
}}
lastIncrementState3 = incrementState3;
incrementState4 = digitalRead(buttonMin);
if(incrementState4 != lastIncrementState4) //compare increment button state to its last state
{
if(incrementState4 == LOW)//increment button is pressed
{
val6 = val6 + 1; //increment the counter
Alarm.delay(20); //debounce delay
if(val6 == 60) {val6 = 0; };
lcd.clear();
}}
lastIncrementState4 = incrementState4;
}
}
if (SelectMode == 1) // Turn Backlight on or off
{ { incrementState = digitalRead(buttonHr);
if(incrementState != lastIncrementState) //compare increment button state to its last state
{
if(incrementState == LOW)//increment button is pressed
{
val3 = val3 + 1; //increment the counter
Alarm.delay(20); //debounce delay
if(val3 == 24) {val3 = 0; };
lcd.clear();
}}
lastIncrementState = incrementState;
incrementState2 = digitalRead(buttonMin);
if(incrementState2 != lastIncrementState2) //compare increment button state to its last state
{
if(incrementState2 == LOW)//increment button is pressed
{
val4 = val4 + 1; //increment the counter
Alarm.delay(20); //debounce delay
if(val4 == 60) {val4 = 0; };
lcd.clear();
}}
lastIncrementState2 = incrementState2;
}
}
if (SelectMode == 2) //Set time MODE
{
if(digitalRead(buttonMin) == LOW){ //adjust clock time min
adjustTime(+60);
}
//detect button pressing
if(digitalRead(buttonHr) == LOW){ //adjust clock time hr
adjustTime(+3600);
}
if (SelectMode == 3) //Set Morning Feeding
{
}
if (SelectMode == 4) // Set Night Feeding
{
}
}}