Go Down

Topic: LCD Menu (Read 766 times) previous topic - next topic

aaron0818

Im having a problem creating a menu for the code below.

My code works but if I want to change any variables I have to connect the arduino nano to the computer and then upload an updated sketch.
Ive tried many menu sites but either dont know where to put my code, how to structure it, or how the code works at all

Any help would be great.

Code: [Select]

#include <Wire.h>
#include <Chronodot.h>
#include <TimeAlarms.h>
#include <Time.h>
#define motor 3
                         
#define feedTimes 3           //Feeding Times Per Day

#define bHour 6               //Breakfast Hour
#define bMin 30               //Breakfast Min

#define lHour 12              //Lunch Hour
#define lMin 00               //Lunch Min

#define dHour 18              //Dinner Hour
#define dMin 30               //Dinner Min

#define bfastDelay 1000       //Breakfast Motor Run Time
#define lunchDelay 1000       //Lunch Motor Run Time
#define dinDelay 1000         //Dinner Motor Run Time

Chronodot RTC;

void setup() {
   Serial.begin(9600);
   digitalWrite(motor, LOW);
   Wire.begin();
   RTC.begin();
   if (! RTC.isrunning()) {
   Serial.println("RTC is NOT running!");}

   DateTime now = RTC.now();
   setTime(hour(), minute(), second(), day(), month(), year());
   setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year()); // set time & date
     
   // Alarms
   Alarm.alarmRepeat(bHour,bMin,0, Breakfast);       // 06:30am every day
   Alarm.alarmRepeat(lHour,lMin,0, Lunch);           // 12:00am every day
   Alarm.alarmRepeat(dHour,dMin,0, Dinner);          // 06:30pm every day
   Alarm.alarmRepeat(01,00,0, CorrectTime);          // Corrects the Time
}

void loop() {
   digitalClockDisplay();
   Alarm.delay(1000);} // wait one second between clock display

void digitalClockDisplay() {
   //digital clock display of the time
   Serial.print(hour());
   printDigits(minute());
   printDigits(second());
   Serial.println();}

void printDigits(int digits) {
   //utility function for digital clock display: prints preceding colon and leading 0
   Serial.print(":");
   if(digits < 10)
   Serial.print('0');
   Serial.print(digits);}

void Breakfast() {
   if (feedTimes >= 2) BreakfastTrue;
   else BreakfastFalse;}
   
void Lunch() {
   if (feedTimes == 1 || feedTimes == 3) LunchTrue;
   else LunchFalse;} 

void Dinner() {
   if (feedTimes >= 2) DinnerTrue;
   else DinnerFalse;}
 
void BreakfastTrue() {
   digitalWrite(motor, HIGH);
   Alarm.delay(bfastDelay);
   digitalWrite(motor, LOW);}

void BreakfastFalse() {
   Serial.print(" Breakfast Skipped");}

void LunchTrue() {
   digitalWrite(motor, HIGH);
   Alarm.delay(lunchDelay);
   digitalWrite(motor, LOW);}

void LunchFalse(){
   Serial.print("Lunch Skipped");}

void DinnerTrue(){
   digitalWrite(motor, HIGH);
   Alarm.delay(dinDelay);
   digitalWrite(motor, LOW);}

void DinnerFalse(){
   Serial.print("Dinner Skipped");}

void CorrectTime(){
   DateTime now = RTC.now();
   setTime(hour(), minute(), second(), day(), month(), year());
   setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year());} // set time & date

Nick Gammon

Without looking at the rest of your code, this is wrong:

Code: [Select]

  if (feedTimes == 1 || feedTimes == 3) LunchTrue;
   else LunchFalse;} 


To call a function you have to use brackets, even if there are no arguments, eg.

Code: [Select]

  if (feedTimes == 1 || feedTimes == 3)
     LunchTrue ();
   else
     LunchFalse ();
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

aaron0818

OK Ill fix that
I got the code from here http://arduino.cc/en/Reference/If.  I had brackets in there but It wouldnt compile (I might have had them in the wrong places).  After changing the code to what I posted I dont get any errors compiling.

Specifically this:

The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement.


if (x > 120) digitalWrite(LEDpin, HIGH);

if (x > 120)
digitalWrite(LEDpin, HIGH);

if (x > 120){ digitalWrite(LEDpin, HIGH); }

if (x > 120){
  digitalWrite(LEDpin1, HIGH);
  digitalWrite(LEDpin2, HIGH);
}                                 // all are correct

Ill post the code after I make the changes


pYro_65

Nick means the brackets on the function name '(,)' not the curly if brackets '{,}', but yes you are right about only one statement being conditional when the curly brackets are omitted.

Code: [Select]
LunchFalse;// is a function pointer
LunchFalse(); is a function call.

aaron0818

I see

Hows this look

Code: [Select]
#include <Wire.h>
#include <Chronodot.h>
#include <TimeAlarms.h>
#include <Time.h>
#define motor 3
                         
#define feedTimes 3           //Feeding Times Per Day

#define bHour 6               //Breakfast Hour
#define bMin 30               //Breakfast Min

#define lHour 12              //Lunch Hour
#define lMin 00               //Lunch Min

#define dHour 18              //Dinner Hour
#define dMin 30               //Dinner Min

#define bfastDelay 1000       //Breakfast Motor Run Time
#define lunchDelay 1000       //Lunch Motor Run Time
#define dinDelay 1000         //Dinner Motor Run Time

Chronodot RTC;

void setup() {
   Serial.begin(9600);
   digitalWrite(motor, LOW);
   Wire.begin();
   RTC.begin();
   if (! RTC.isrunning()) {
   Serial.println("RTC is NOT running!");}

   DateTime now = RTC.now();
   setTime(hour(), minute(), second(), day(), month(), year());
   setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year()); // set time & date
     
   // Alarms
   Alarm.alarmRepeat(bHour,bMin,0, Breakfast);       // 06:30am every day
   Alarm.alarmRepeat(lHour,lMin,0, Lunch);           // 12:00am every day
   Alarm.alarmRepeat(dHour,dMin,0, Dinner);          // 06:30pm every day
   Alarm.alarmRepeat(01,00,0, CorrectTime);          // Corrects the Time
}

void loop() {
   digitalClockDisplay();
   Alarm.delay(1000);} // wait one second between clock display

void digitalClockDisplay() {
   //digital clock display of the time
   Serial.print(hour());
   printDigits(minute());
   printDigits(second());
   Serial.println();}

void printDigits(int digits) {
   //utility function for digital clock display: prints preceding colon and leading 0
   Serial.print(":");
   if(digits < 10)
   Serial.print('0');
   Serial.print(digits);}

void Breakfast() {
   if (feedTimes >= 2) BreakfastTrue ();
   else BreakfastFalse ();}
   
void Lunch() {
   if (feedTimes == 1 || feedTimes == 3) LunchTrue ();
   else LunchFalse ();} 

void Dinner() {
   if (feedTimes >= 2) DinnerTrue ();
   else DinnerFalse ();}
 
void BreakfastTrue() {
   digitalWrite(motor, HIGH);
   Alarm.delay(bfastDelay);
   digitalWrite(motor, LOW);}

void BreakfastFalse() {
   Serial.print(" Breakfast Skipped");}

void LunchTrue() {
   digitalWrite(motor, HIGH);
   Alarm.delay(lunchDelay);
   digitalWrite(motor, LOW);}

void LunchFalse(){
   Serial.print("Lunch Skipped");}

void DinnerTrue(){
   digitalWrite(motor, HIGH);
   Alarm.delay(dinDelay);
   digitalWrite(motor, LOW);}

void DinnerFalse(){
   Serial.print("Dinner Skipped");}

void CorrectTime(){
   DateTime now = RTC.now();
   setTime(hour(), minute(), second(), day(), month(), year());
   setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year());} // set time & date

pYro_65

You should move away from having the smaller code look:

Code: [Select]
void Breakfast() {
   if (feedTimes >= 2) BreakfastTrue ();
   else BreakfastFalse ();}


This can be laid out so it is easier to read, maybe its an easy example, but squashing up large functions just leads to headaches.

Code: [Select]
void Breakfast( void ){
   if( feedTimes >= 2 )
     BreakfastTrue();
   else
     BreakfastFalse();
}

Go Up