I have been working on a pet feeder project based off of Automatic Pet Feeder. I have his code working but now i am tweaking it to my needs and trying to learn new things along the way. I want to add a settings menu that allows a user to use the rotary encoder to set the time and a few other variables. As of right now i have the rotary encoder working for the root layer of my menu. When i select a menu it should launch the function and use the encoder to configuration the numbers. i can not get the encoder to work inside the set time function.
the menu code is based on a cocktail maker code, i haven't cleaned up all those references yet.
Menu Code:
/* Cocktail Production - Menu Test Sketch
* Created by VulkanDesign.com
* Free to use -> but: pls pay attention to the copyrights of the included libraries
* Granted by Dipl.-Ing. Raimund P. Trierscheid
*/
/* Rotary encoder handler for arduino.
Copyright 2011 Ben Buxton. Licenced under the GNU GPL Version 3.
Contact: bb@cactii.net
Quick implementation of rotary encoder routine.
More info: http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html
*/
// Include the library code
#include <Wire.h> // needed for I2C communication
#include <LiquidCrystal.h> // LCD with I2C adapter
#include <rotary.h> // rotary handler
#include <Time.h>
#include <TimeLib.h>
#include <DS1307RTC.h> // Real Time Clock Library
/* maybe for later enhancements
#include <EEPROMex.h>
#include <EEPROMVar.h>
#include <OneButton.h>
*/
// Initialize the Rotary object
// Rotary(Encoder Pin 1, Encoder Pin 2, Button Pin) Attach center to ground
Rotary r = Rotary(2, 3, 4); // there is no must for using interrupt pins !!
// Half-step mode
#define HALF_STEP
// Define I2C_LCD
LiquidCrystal lcd(12, 11, 5, 8, 7, 6);
// Define your cocktail information
int maxNumber = 5;
int hourmaxNumber = 23;
int minmaxNumber = 59;
char* myCocktails[] = {
"Set Time",
"First Meal",
"Second Meal",
"Loola QTY",
"Dooly QTY",
//"Mai Tai",
//"Bloody Mary",
//"Mojito",
//"Cuba Libre",
//"Tequila Sunrise",
//"Caipirinha",
//"Wodka Lemon",
};
int x = 0;
void setup() {
//initialize serial and wait for port to open :
Serial.begin(9600); // only for debugging, comment out later
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Initialized");
Serial.println();
lcd.begin(16, 2);
lcd.clear (); // go home
lcd.print ("David S");
lcd.setCursor(0, 1);
lcd.print("Pet Feeder");
delay(2000);
//lcd.backlight(); // only needed for 0x3F I2C Adapter
// ------- Quick 3 blinks of backlight -------------
//blinkLCD();
//char lcdline1[13];
// sprintf (lcdline1, "Cocktail #: %02i", x + 1); // index starts at 0
lcd.clear();
lcd.setCursor(0, 0);
//lcd.print (lcdline1);
lcd.print("Settings");
lcd.setCursor(0, 1);
lcd.print(" "); // erase previous content
lcd.setCursor(0, 1);
lcd.print(myCocktails[x]);
}
void loop() {
volatile unsigned char result = r.process();
if (result) {
result == DIR_CCW ? x = x - 1 : x = x + 1;
if (x < 0) { // no values < 0; later: use unsigned int
//blinkLCD();
x = maxNumber - 1; // roll over
}
if (x > maxNumber - 1) { // no more strings
// ------- Quick 3 blinks of backlight -------------
//blinkLCD();
x = 0; // roll over
}
//char lcdline1[13];
//sprintf (lcdline1, "Cocktail #: %02i", x + 1);
lcd.clear();
lcd.setCursor(0, 0);
//lcd.print (lcdline1);
lcd.print("Settings");
lcd.setCursor(0, 1);
lcd.print(" "); // erase previous content
lcd.setCursor(0, 1);
lcd.print(myCocktails[x]);
}
if (r.buttonPressedReleased(25)) {
switch (x) {
case 0:
// lcdScreen1();
SetTime(); // Start der Produktion
break;
case 1:
// lcdScreen1();
Cocktail_2(); // Start der Produktion
break;
case 2:
//lcdScreen1();
Cocktail_3(); // Start der Produktion
break;
case 3:
// lcdScreen1();
Cocktail_4(); // Start der Produktion
break;
case 4:
//lcdScreen1();
Cocktail_5(); // Start der Produktion
break;
}
//blinkLCD();
lcd.clear (); // go home
lcd.setCursor(0, 1);
x = 0; // reset to start position
lcd.clear();
char lcdline1[13];
lcd.print ("Settings");
lcd.setCursor(0, 1);
lcd.print(myCocktails[x]);
}
}
// #################### Functions ###################
void SetTime() {
// produce the requested cocktail -> stepper motor actions
tmElements_t tm; // This sectionm reads the time from the RTC, sets it in tmElements tm (nice to work with), then displays it.
RTC.read(tm);
volatile unsigned char Hour = r.process();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SET TIME HERE");
lcd.setCursor(3,1);
lcd.print(":");
lcd.blink();
x = Hour;
if (!(r.buttonPressedReleased(25))) {
do {
lcd.setCursor(1,1);
delay(500);
if (Hour) {
Hour == DIR_CCW ? x = x - 1 : x = x + 1;
if (x < 0) { // no values < 0; later: use unsigned int
x = hourmaxNumber - 1; // roll over
}
if (x > hourmaxNumber - 1) { // no more strings
x = 0; // roll over
}
}
tm.Hour = x;
RTC.write(tm);
lcd.setCursor(1,1);
printDigits(tm.Hour);
} while ((r.buttonPressedReleased(25)));
lcd.noBlink();
delay(1000);
}
}
void Cocktail_2() {
// produce the requested cocktail -> stepper motor actions
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set First Meal");
lcd.setCursor(0,1);
lcd.print("Time XX:XX");
lcd.blink();
lcd.setCursor(5,1);
delay(5000);
lcd.noBlink();
}
void Cocktail_3() {
// produce the requested cocktail -> stepper motor actions
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Second Meal");
lcd.setCursor(0,1);
lcd.print("Time XX:XX");
lcd.blink();
lcd.setCursor(5,1);
delay(5000);
lcd.noBlink();
}
void Cocktail_4() {
// produce the requested cocktail -> stepper motor actions
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Loola Feed");
lcd.setCursor(0,1);
lcd.print("QTY: XX");
lcd.blink();
lcd.setCursor(5,1);
delay(5000);
lcd.noBlink();
}
void Cocktail_5() {
// produce the requested cocktail -> stepper motor actions
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Dooly Feed");
lcd.setCursor(0,1);
lcd.print("QTY: XX");
lcd.blink();
lcd.setCursor(5,1);
delay(5000);
lcd.noBlink();
}
void printDigits(int digits){ // utility function for digital clock display: prints leading 0
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
Any help with getting this sorted would be great. Or even just a topic to research to help me figure it out on my own. Thank you.