Call menu in program

Hi,

I've made my arduino program and built a setting menu.

But I don't know how call the menu.

The program start and when I press one touch, nothing's matter

Thanks in advance

Sorry for mistakes.

Only one mistake that I can see. You forgot to post your code. :wink:

My code can be optimize...

//example use of LCD4Bit_mod library

#define DS1307_I2C_ADDRESS 0x68
#include <LCD4Bit_mod.h>
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>
#include <stdio.h>

//create object to control an LCD.

LCD4Bit_mod lcd = LCD4Bit_mod(2);

// definition menu

// premiere ligne menu – description of menu level
char sequenceMenu[4][17] = {"Reglages ", "Mode manuel ", "Debut programme", " Reglage heure "};

// seconde ligne menu – choix module
char sequenceChoix[7][17] = {"T5 1 & 2","T5 3 & 4","T5 5 & 6","Mod Led UV","Mod Led 1","Mod Led 2"};

// troisième ligne menu – options réglages
char sequenceOptions[2][17] = {" OFF : ", " ON : "};

// troisième bis ligne menu – mode manuel
char sequenceManuel[2][17] = {" Eteindre ", " Allumer "};

char affich[6];

int adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

#define rightKey 0
#define upKey 1
#define downKey 2
#define leftKey 3
#define selectKey 4

// définition variables menu
int program = 0;
int General = 0;
int Menu = 0;
int Choix = 0;

int Options = 0;
int Manuel = 0;
int Reglage = 0;
int Settime = 0;

// définition variables état éclairage

int L [4] = {0,0,0,0};
int T5_mod1 = 1;
int T5_mod2 = 2;
int T5_mod3 = 3;
int Led_UV = 4;
int Led_mod1 = 5;
int Led_mod2 = 6;

byte pwmmod1 = 0;
byte pwmmod2 = 0;

// Array of pwm values for 15 minute sections
byte Ledmod1[96] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-03
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 03-06
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 06-09
0, 0, 0, 0, 20, 50, 80, 110, 140, 170, 200, 230, // 09-12
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 12-15
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 15-18
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 18-21
230, 200, 170, 140, 110, 80, 50, 30, 0, 0, 0, 0 // 21-24
};

byte Ledmod2[96] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-03
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 03-06
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 06-09
0, 0, 0, 0, 20, 50, 80, 110, 140, 170, 200, 230, // 09-12
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 12-15
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 15-18
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 18-21
230, 200, 170, 140, 110, 80, 50, 30, 0, 0, 0, 0 // 21-24
};

byte Ledmod1MaxCurrent = 100; // Percentage
byte Ledmod2MaxCurrent = 100; // Percentage

int SECS_PER_HOUR = 3600;

#define SS 15 //Sector size

int min_cnt ;

// définition Heures / minutes éclairage

char Jour[][8]= {"Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim"};
char Mois[][13] = {"Janv", "Fevr", "Mars", " Avr", " Mai", "Juin", "Juil", "Aout", "Sept", " Oct", " Nov", " Dec"};

int second, minute, hour, day, date, month, year;
int HH = 12;
int MM=0;

int Hallum [4] = {0,0,0,0};
int Hextin [4] = {0,0,0,0};
int Mallum [4] = {0,0,0,0};
int Mextin [4] = {0,0,0,0};

void setup() {

lcd.init();
//optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
//lcd.commandWrite(0x0F);//cursor on, display on, blink on. (nasty!)
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn(" Reef-light 1.0 ");
delay(3000);
clearLine(1);
lcd.cursorTo(2,0);
lcd.printIn("-> Reglage date ");
delay(500);
Wire.begin();
Serial.begin(19200);
Set();
setDateDs1307(second, minute, hour, day, date, month, year);
Settime=0;
delay(500);
clearLine(1);
clearLine(2);
lcd.cursorTo(2,0);
lcd.printIn(" saved ");
delay(500);
clearLine(2);
}

void loop()
{
affichage_heure();
for (int i=0; i<4;i++)
{
if ( hour == Hallum && minute == Mallum*)*
* {*
* digitalWrite(i+1, HIGH);*
* }*
if (hour==Hextin && minute==Mextin*)*
* {*
* digitalWrite(i+1, LOW);*
* }*
* }*
* LED_levels_output();
_ menu();
}*_

// Convert ADC value to key number
int get_key(unsigned int input)
{
* int k;*

* for (k = 0; k < NUM_KEYS; k++)
_ {_
if (input < adc_key_val[k])
_ {
return k;
}
}*_

* if (k >= NUM_KEYS)
_ {k = -1; // No valid key pressed*
* return k;}
}
// ===================================================================
// Lcd tools*

void clearLine(int line)
{
* lcd.cursorTo(line,0);
lcd.printIn(" ");
lcd.cursorTo(line,0);
}
void Retour()
{
clearLine(1);
clearLine(2);
Menu = 1;
Choix = 0;
Manuel=0;
General--;*_

* lcd.cursorTo(1, 2);*
* lcd.printIn(sequenceMenu[Menu]);*
* lcd.cursorTo(2, 0);*
* lcd.printIn(">");*
* lcd.cursorTo(2, 2);*
* lcd.printIn(sequenceChoix[Choix]);*
}

void AffichageH(int Heure)
{
* if (Heure < 10)*
* {*
* lcd.cursorTo(2, 11);*
* lcd.printIn ("0");*
* lcd.cursorTo(2, 12);*
* itoa(Heure,affich,10);*
* lcd.printIn (affich);*
* }*
* else*
* {*
* lcd.cursorTo(2, 11);*
* itoa(Heure,affich,10);*
* lcd.printIn (affich);*
* } *
}
void AffichageM(int Minutes)
*{ *
* if (Minutes < 10)*
* {*
* lcd.cursorTo(2, 14);*
* lcd.printIn ("0");*
* lcd.cursorTo(2, 15);*
* itoa(Minutes,affich,10);*
* lcd.printIn (affich);*
* }*
* else*
* {*
* lcd.cursorTo(2, 14);*
* itoa(Minutes,affich,10);*
* lcd.printIn (affich);*
* } *
}
void Affichage(int valeur, int line, int row)
*{ *
* if (valeur < 10)*
* {*
* lcd.cursorTo(line, row);*
* lcd.printIn ("0");*
* lcd.cursorTo(line, row+1);*
* itoa(valeur,affich,10);*
* lcd.printIn (affich);*
* }*
* else*
* {*
* lcd.cursorTo(line, row);*
* itoa(valeur,affich,10);*
* lcd.printIn (affich);*
* } *
}
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
_ return ( (val/1016) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers*

byte bcdToDec(byte val)
{
return ( (val/1610) + (val%16) );
}
// Set date*

void setDateDs1307(int second, int minute, int hour, int day, int date, int month, int year)
{

* Wire.beginTransmission(DS1307_I2C_ADDRESS);*
* Wire.send(0);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock*

* Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set*

* // bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(day));
Wire.send(decToBcd(date));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.send(0x10);
Wire.endTransmission();
}
void getDateDs1307()
{
// Reset the register pointer*

* Wire.beginTransmission(DS1307_I2C_ADDRESS);*
* Wire.send(0);
Wire.endTransmission();_

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
_ // A few of these need masks because certain bits are control bits*
* second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm*

* day = bcdToDec(Wire.receive());
date = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());
}
void Set()
{
clearLine(1);
clearLine(2);
lcd.cursorTo(1,0);
lcd.printIn ("Reglage");
lcd.cursorTo(1,10);
lcd.printIn (":");
lcd.cursorTo(1,13);
lcd.printIn (":");
while (Settime<=7)
{
int key = 0;_

adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press*

* if (key != oldkey) // if keypress is detected*
* {*
* delay(50); // wait for debounce time*
* adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press*

* if (key != oldkey)*
* {*
* oldkey = key;*
* if (key >=0)*
* { *
* switch(key){*
* case upKey:*
* switch( Settime ) {*
* case 0:*
* hour++;*
* if( hour >= 24 ) {hour = 0;}*
* Affichage(hour, 1, 8);*
* break;*
* case 1:*
* minute++;*
* if( minute >= 60 ) {minute = 0; }*
* Affichage(minute, 1, 11);*
* break;*
* case 2:*
* second++;*
* if( second >= 59 ) {second = 0;}*
* Affichage(second, 1, 14);*
* break;*
* case 3:*
* day++;*
* if( day >= 7 ) {day = 0; }*
* lcd.cursorTo(2,0);*
* lcd.printIn (Jour[day]); *
* break;*
* case 4:*
* date++;*
* if( date >= 32 ) {date = 1; }*
* Affichage(date, 2, 4);*
* break;*
* case 5:*
* month++;*
* if( month >= 12 ) {month = 0; }*
* lcd.cursorTo(2,7);*
* lcd.printIn (Mois[month]);*
* break;*
* case 6:*
* year++;*
* if( year >= 21 ) {year = 9;}*
* lcd.cursorTo(2,12);*
* lcd.printIn ("20");*
* Affichage(year, 2, 14);*
* break;*
* }*
* break;*

* case downKey:*
* switch( Settime ) {*
* case 0:*
* hour--;*

The Set function does not wait for a key press. If only does anything if a key is being pressed.

The only place that Set() is called is in setup.

You'd have to be holding a key down when the Arduino is started in order to get into the menu function.

I think you need to add a call to Set() in loop, too.

Setting date is not the problem.

When arduino start, I set it and all is ok but I can't call the menu() function.

There is a call to the menu function in loop. However the code you posted (incorrectly, by the way. Please use the # button on the top row when posting code) got chopped off before the menu function. So, I can't tell what is happening in the menu function.

Here the code.

What's happening : the arduino start, I set the date, date appears on the lcd and refresh all 2 seconds (instead of each second).
When I press key nothing matter, I can't go to the menu.

What's I want : refresh hour each second and go to menu when I press a key.

thanks for your help.

//example use of LCD4Bit_mod library

#define DS1307_I2C_ADDRESS 0x68
#include <LCD4Bit_mod.h>
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>
#include <stdio.h>

//create object to control an LCD.  

LCD4Bit_mod lcd = LCD4Bit_mod(2); 
                
// definition menu

// premiere ligne menu – description of menu level
char sequenceMenu[4][17]  = {"Reglages ", "Mode manuel ", "Debut programme", " Reglage heure "};

// seconde ligne menu – choix module
char sequenceChoix[7][17]   = {"T5 1 & 2","T5 3 & 4","T5 5 & 6","Mod Led UV","Mod Led 1","Mod Led 2"};

// troisième ligne menu – options réglages
char sequenceOptions[2][17] = {" OFF : ", " ON  : "};

// troisième bis ligne menu – mode manuel
char sequenceManuel[2][17] = {" Eteindre ", " Allumer "};
  

char affich[6];

int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

#define rightKey 0
#define upKey 1
#define downKey 2
#define leftKey 3
#define selectKey 4

// définition variables menu
int program = 0;
int General = 0;
int Menu    =  0;
int Choix = 0;

int Options = 0;
int Manuel = 0;
int Reglage = 0;
int Settime = 0;

// définition variables état éclairage

int L [4] = {0,0,0,0};
int T5_mod1 = 1;
int T5_mod2 = 2;
int T5_mod3 = 3;
int Led_UV = 4;
int Led_mod1 = 5;
int Led_mod2 = 6;

byte pwmmod1 = 0;
byte pwmmod2 = 0;

// Array of pwm values for 15 minute sections
byte Ledmod1[96] = {
  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  // 00-03
  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  // 03-06
  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  // 06-09
  0, 0, 0, 0, 20, 50, 80, 110, 140, 170, 200, 230, // 09-12
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  // 12-15
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  // 15-18
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  // 18-21
  230, 200, 170, 140, 110, 80, 50, 30, 0, 0, 0, 0 // 21-24
}; 

byte Ledmod2[96] = {
  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  // 00-03
  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  // 03-06
  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  // 06-09
  0, 0, 0, 0, 20, 50, 80, 110, 140, 170, 200, 230, // 09-12
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  // 12-15
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  // 15-18
  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,  // 18-21
  230, 200, 170, 140, 110, 80, 50, 30, 0, 0, 0, 0 // 21-24
};

byte Ledmod1MaxCurrent = 100; // Percentage
byte Ledmod2MaxCurrent = 100; // Percentage

int SECS_PER_HOUR = 3600;

#define SS 15  //Sector size

int min_cnt ;

// définition Heures / minutes éclairage

char Jour[][8]= {"Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim"};
char Mois[][13] = {"Janv", "Fevr", "Mars", " Avr", " Mai", "Juin", "Juil", "Aout", "Sept", " Oct", " Nov", " Dec"};

int second, minute, hour, day, date, month, year;
int HH = 12;
int MM=0;

int Hallum [4] = {0,0,0,0};
int Hextin [4] = {0,0,0,0};
int Mallum [4] = {0,0,0,0};
int Mextin [4] = {0,0,0,0};

void setup() { 

  lcd.init();
  //optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
  //lcd.commandWrite(0x0F);//cursor on, display on, blink on.  (nasty!)
   lcd.clear();
   lcd.cursorTo(1,0);
   lcd.printIn(" Reef-light 1.0 ");
   delay(3000);
   clearLine(1);
   lcd.cursorTo(2,0);             
   lcd.printIn("-> Reglage date ");
   delay(500);
   Wire.begin();
   Serial.begin(19200);
   Set();
   setDateDs1307(second, minute, hour, day, date, month, year);
   Settime=0;
   delay(500);
   clearLine(1);
   clearLine(2);
   lcd.cursorTo(2,0);             
   lcd.printIn(" saved ");
   delay(500);
   clearLine(2);
}

void loop()
{
  affichage_heure();
  for (int i=0; i<4;i++)
  {
     if ( hour == Hallum[i] && minute == Mallum[i])
     {
     digitalWrite(i+1, HIGH); 
     }
     if (hour==Hextin[i] && minute==Mextin[i])
     {
     digitalWrite(i+1, LOW);
     }
  }
  LED_levels_output();
  menu();   
}

  
// Convert ADC value to key number
int get_key(unsigned int input)
{
      int k;
    
      for (k = 0; k < NUM_KEYS; k++)
      {
            if (input < adc_key_val[k])
            {  
                return k;
                }
      }
    
        if (k >= NUM_KEYS)
        {k = -1;     // No valid key pressed
        return k;}
}
// ===================================================================
// Lcd tools

void clearLine(int line)
{
    lcd.cursorTo(line,0);
    lcd.printIn("                ");
    lcd.cursorTo(line,0);
}

void Retour()
{
  clearLine(1);
  clearLine(2);
  Menu = 1;
  Choix = 0;
  Manuel=0;
  General--;
                        
  lcd.cursorTo(1, 2);
  lcd.printIn(sequenceMenu[Menu]);
  lcd.cursorTo(2, 0);
  lcd.printIn(">");
  lcd.cursorTo(2, 2);
  lcd.printIn(sequenceChoix[Choix]);
}
                                     
void AffichageH(int Heure)
{
    if (Heure < 10)
       {
       lcd.cursorTo(2, 11);
       lcd.printIn ("0");
       lcd.cursorTo(2, 12);
       itoa(Heure,affich,10);
       lcd.printIn (affich);
       }
       else
       {
       lcd.cursorTo(2, 11);
       itoa(Heure,affich,10);
       lcd.printIn (affich);
       }                                                      
}
void AffichageM(int Minutes)
{                                  
    if (Minutes < 10)
       {
       lcd.cursorTo(2, 14);
       lcd.printIn ("0");
       lcd.cursorTo(2, 15);
       itoa(Minutes,affich,10);
       lcd.printIn (affich);
       }
       else
       {
       lcd.cursorTo(2, 14);
       itoa(Minutes,affich,10);
       lcd.printIn (affich);
       }                   
}
void Affichage(int valeur, int line, int row)
{                                  
      if (valeur < 10)
       { 
       lcd.cursorTo(line, row);
       lcd.printIn ("0");
       lcd.cursorTo(line, row+1);
       itoa(valeur,affich,10);
       lcd.printIn (affich);
       }
       else
       {
       lcd.cursorTo(line, row);
       itoa(valeur,affich,10);
       lcd.printIn (affich);
       }            
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// Set date
void setDateDs1307(int second, int minute, int hour, int day, int date, int month, int year)          
{
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.send(0);
   Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
   Wire.send(decToBcd(minute));
   Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   Wire.send(decToBcd(day));
   Wire.send(decToBcd(date));
   Wire.send(decToBcd(month));
   Wire.send(decToBcd(year));
   Wire.send(0x10);
   Wire.endTransmission();
}

void getDateDs1307()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(Wire.receive() & 0x7f);
  minute     = bcdToDec(Wire.receive());
  hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  day        = bcdToDec(Wire.receive());
  date       = bcdToDec(Wire.receive());
  month      = bcdToDec(Wire.receive());
  year       = bcdToDec(Wire.receive());
}

void Set()
{  
  clearLine(1);
  clearLine(2);
  lcd.cursorTo(1,0);
  lcd.printIn ("Reglage");
  lcd.cursorTo(1,10);
  lcd.printIn (":");
  lcd.cursorTo(1,13);
  lcd.printIn (":");  
  while (Settime<=7)
  {
  int key = 0;
    adc_key_in = analogRead(0);                  // read the value from the sensor  
    key = get_key(adc_key_in);                  // convert into key press
    if (key != oldkey)                        // if keypress is detected
    {
        delay(50);                        // wait for debounce time
        adc_key_in = analogRead(0);            // read the value from the sensor  
        key = get_key(adc_key_in);            // convert into key press
        if (key != oldkey)
        {
            oldkey = key;
            if (key >=0)
            {  
        switch(key){
          case upKey:
            switch( Settime ) {
            case 0:
            hour++;
            if( hour >= 24 ) {hour = 0;}
            Affichage(hour, 1, 8);
            break;
            case 1:
            minute++;
            if( minute >= 60 ) {minute = 0; }
            Affichage(minute, 1, 11);
            break;
            case 2:
            second++;
            if( second >= 59 ) {second = 0;}
            Affichage(second, 1, 14);
            break;
            case 3:
            day++;
            if( day >= 7 ) {day = 0; }
            lcd.cursorTo(2,0);
            lcd.printIn (Jour[day]);  
            break;
            case 4:
            date++;
            if( date >= 32 ) {date = 1; }
            Affichage(date, 2, 4);
            break;
            case 5:
            month++;
            if( month >= 12 ) {month = 0; }
            lcd.cursorTo(2,7);
            lcd.printIn (Mois[month]);
            break;
            case 6:
            year++;
            if( year

My prog is too long...

void menu()
{
    int key = 0;
    adc_key_in = analogRead(0);                  // read the value from the sensor  
    key = get_key(adc_key_in);                  // convert into key press
    if (key != oldkey)                        // if keypress is detected
    {
        program = 1;
        delay(50);                        // wait for debounce time
        adc_key_in = analogRead(0);            // read the value from the sensor  
        key = get_key(adc_key_in);            // convert into key press
        if (key != oldkey)
        {
            oldkey = key;
            if (key >=0)
            {
            switch(key){
                 case upKey:
                    switch (General){
                          case 0:             // Menu niveau 1                       
                          clearLine(1);
                          clearLine(2);
                          lcd.cursorTo(1, 0);             
                          lcd.printIn(">");
                          Menu--;
                            if (Menu<0)
                            {Menu = 3;}
                          lcd.cursorTo(1, 2);
                          lcd.printIn(sequenceMenu[Menu]);
                          break;
                          
                          case 1:             // Menu niveau 2
                              switch(Menu) {
                              case 0:          // Menu "reglages"
                                  clearLine(2);
                                  lcd.cursorTo(2, 0);             
                                  lcd.printIn(">");
                                  Choix--;
                                  if (Choix<0)
                                      {Choix = 6;}
                                      lcd.cursorTo(2, 2);             
                                      lcd.printIn(sequenceChoix[Choix]);
                              break;
                              case 1:          // Menu "manuel"
                                  clearLine(2);
                                  lcd.cursorTo(2, 0);             
                                  lcd.printIn(">");            
                                  Choix--;
                                    if (Choix<0)
                                    {Choix = 6;}
                                  lcd.cursorTo(2, 2);             
                                  lcd.printIn(sequenceChoix[Choix]);
                              break;
                              case 2:          // Menu "début programme"
                              break;
                              case 3:          // Menu "Reglage heure"
                              break;
                              }
                          break;
                          case 2:             // Menu niveau 3
                          switch(Menu) {
                                  lcd.cursorTo(1, 1);
                                  lcd.printIn(" ");
                                  lcd.cursorTo(2, 1);
                                  lcd.printIn(" ");
                                  lcd.cursorTo(1, 3);
                                case 0:          // Menu secondaire "Réglages"  
                                  lcd.printIn(sequenceOptions[0]);
                                  lcd.cursorTo(2, 3);
                                  lcd.printIn(sequenceOptions[1]);
                                  Options=0;
                                  lcd.cursorTo(1 , 1);
                                  lcd.printIn(">");
                                break;
                                case 1:          // Menu secondaire "Manuel"
                                  lcd.printIn(sequenceManuel[0]);
                                  lcd.cursorTo(2,3);
                                  lcd.printIn(sequenceManuel[1]);
                                  Manuel=0;
                                  lcd.cursorTo(1 , 1);
                                  lcd.printIn(">");
                                break;
                          }
                          break;
                          case 3:             // Menu niveau 4                     
                              switch (Reglage){
                                  case 0:
                                      HH++;
                                      if (HH > 23)
                                      {HH = 0;}
                                      AffichageH(HH);   
                                  break;
                                  case 1:
                                      MM++;
                                      if (MM > 59)
                                      {MM = 0;}
                                      AffichageM(MM);
                                  break;
                              }
                          break;
                          }
                    break;
                 case downKey:
                    switch (General){
                          case 0:             // Menu niveau 1               
                          clearLine(1);
                          clearLine(2);
                          lcd.cursorTo(1, 0);             
                          lcd.printIn(">");                       
                          Menu++;
                            if (Menu>3)
                            {Menu = 0;}
                          lcd.cursorTo(1, 2);              
                          lcd.printIn(sequenceMenu[Menu]);
                          break;
                          case 1:
                              clearLine(2);
                              switch(Menu) {
                              case 0:          // Menu secondaire "reglages"
                                  lcd.cursorTo(2, 0);
                                  lcd.printIn(">");
                                  Choix++;
                                  if (Choix>6)
                                      {Choix = 0;}
                                  lcd.cursorTo(2, 2);              
                                  lcd.printIn(sequenceChoix[Choix]);
                              break;
                              case 1:          // Menu secondaire "manuel"
                                  lcd.cursorTo(2, 0);
                                  lcd.printIn(">");               
                                  Choix++;
                                    if (Choix>6)
                                    {Choix = 0;}
                                  lcd.cursorTo(2, 2);              
                                  lcd.printIn(sequenceChoix[Choix]);
                              break;
                              case 2:          // Menu secondaire "début programme"
                                  lcd.cursorTo (2,2);
                                  lcd.printIn ("Press select");
                              break;
                              case 3:          // Menu secondaire "Reglage heure"
                                  lcd.cursorTo (2,2);
                                  lcd.printIn ("Press select");
                              break;
                              }
                          break;
                          case 2:
                                  lcd.cursorTo(1, 1);
                                  lcd.printIn(" ");
                                  lcd.cursorTo(2, 1);
                                  lcd.printIn(" ");
                                  lcd.cursorTo(1, 3);
                            switch(Menu) {
                              case 0:          // Menu secondaire "Réglages"
                                  lcd.printIn(sequenceOptions[0]);
                                  lcd.cursorTo(2, 3);
                                  lcd.printIn(sequenceOptions[1]);
                                  Options=1;
                                  lcd.cursorTo(2 , 1);
                                  lcd.printIn(">");
                              break;
                              case 1:          // Menu secondaire "Manuel"
                                  lcd.printIn(sequenceManuel[0]);
                                  lcd.cursorTo(2,3);
                                  lcd.printIn(sequenceManuel[1]);
                                  Manuel=1;
                                  lcd.cursorTo(2 , 1);
                                  lcd.printIn(">");
                              break;
                              }
                          break;
                          case 3:             // Menu niveau 4                     
                              switch (Reglage){
                                  case 0:
                                      HH--;
                                      if (HH < 0)
                                      {HH = 23;}
                                      AffichageH(HH);
                                  break;
                                  case 1:
                                      MM--;
                                      if (MM < 0)
                                      {MM = 59;}
                                      AffichageM(MM);
                                  break;
                                  }
                          break;
                          }
                    break;
                    
                 case rightKey:
                 break;
                
                 case leftKey:
                    switch (General){
                          case 0:             // Menu niveau 1

Nobody can help me ?

Not if you posting like that, no, I shouldn't think so.

Please use the # button when posting code.

I've use # but the code is too long

You'll need to find a web hosting company, then, to host the code, and post a link to the code.

Or, strip the code down to the minimum that illustrates the problem.

My guess is that when you do this, the problem will go away. Figure out what you deleted to make the problem go away, and leave that out.

Here is my code strip to mini.
You can see :

  • void setup () -> I set the date
  • void loop () -> the program start.

I want enter the menu when a key is press.
After that in the menu, I choose "program start" and return main loop after pressing select key.
I think debounce function can help me but I don't know how I can use it.

How I can do that ?

//example use of LCD4Bit_mod library

#define DS1307_I2C_ADDRESS 0x68
#include <LCD4Bit_mod.h>
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>
#include <stdio.h>

//create object to control an LCD.  

LCD4Bit_mod lcd = LCD4Bit_mod(2); 
                
// definition menu

// premiere ligne menu – description of menu level
char sequenceMenu[4][17]  = {"Reglages ", "Mode manuel ", "Debut programme", " Reglage heure "};

// seconde ligne menu – choix module
char sequenceChoix[7][17]   = {"T5 1 & 2","T5 3 & 4","T5 5 & 6","Mod Led UV","Mod Led 1","Mod Led 2"};

// troisième ligne menu – options réglages
char sequenceOptions[2][17] = {" OFF : ", " ON  : "};

// troisième bis ligne menu – mode manuel
char sequenceManuel[2][17] = {" Eteindre ", " Allumer "};
  

char affich[6];

int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

#define rightKey 0
#define upKey 1
#define downKey 2
#define leftKey 3
#define selectKey 4

// définition variables menu
int program = 0;
int General = 0;
int Menu    =  0;
int Choix = 0;

int Options = 0;
int Manuel = 0;
int Reglage = 0;
int Settime = 0;

// définition variables état éclairage

int L [4] = {0,0,0,0};
int T5_mod1 = 1;
int T5_mod2 = 2;
int T5_mod3 = 3;
int Led_UV = 4;
int Led_mod1 = 5;
int Led_mod2 = 6;

byte pwmmod1 = 0;
byte pwmmod2 = 0;

int SECS_PER_HOUR = 3600;

#define SS 15  //Sector size

int min_cnt ;

// définition Heures / minutes éclairage

char Jour[][8]= {"Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim"};
char Mois[][13] = {"Janv", "Fevr", "Mars", " Avr", " Mai", "Juin", "Juil", "Aout", "Sept", " Oct", " Nov", " Dec"};

int second, minute, hour, day, date, month, year;
int HH = 12;
int MM=0;

int Hallum [4] = {0,0,0,0};
int Hextin [4] = {0,0,0,0};
int Mallum [4] = {0,0,0,0};
int Mextin [4] = {0,0,0,0};

void setup() { 

  lcd.init();
  //optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
  //lcd.commandWrite(0x0F);//cursor on, display on, blink on.  (nasty!)
   lcd.clear();
   lcd.cursorTo(1,0);
   lcd.printIn(" Reef-light 1.0 ");
   delay(3000);
   clearLine(1);
   lcd.cursorTo(2,0);             
   lcd.printIn("-> Reglage date ");
   delay(500);
   Wire.begin();
   Serial.begin(19200);
   Set();
   setDateDs1307(second, minute, hour, day, date, month, year);
   Settime=0;
   delay(500);
   clearLine(1);
   clearLine(2);
   lcd.cursorTo(2,0);             
   lcd.printIn(" saved ");
   delay(500);
   clearLine(2);
}

void loop()
{
  affichage_heure();
  for (int i=0; i<4;i++)
  {
     if ( hour == Hallum[i] && minute == Mallum[i])
     {
     digitalWrite(i+1, HIGH); 
     }
     if (hour==Hextin[i] && minute==Mextin[i])
     {
     digitalWrite(i+1, LOW);
     }
  }
  LED_levels_output();
  menu();   
}

void setDateDs1307(int second, int minute, int hour, int day, int date, int month, int year)          
{ }
void getDateDs1307()
{ }
void Set()
{ } 
void LED_levels_output()
{ }
void menu()
{
    int key = 0;
    adc_key_in = analogRead(0);                  // read the value from the sensor  
    key = get_key(adc_key_in);                  // convert into key press
    if (key != oldkey)                        // if keypress is detected
    {
        program = 1;
        delay(50);                        // wait for debounce time
        adc_key_in = analogRead(0);            // read the value from the sensor  
        key = get_key(adc_key_in);            // convert into key press
        if (key != oldkey)
        {
            oldkey = key;
            if (key >=0)
            {
            switch(key){
                 case upKey:
                    switch (General){
                          case 0:             // Menu niveau 1                       
                          clearLine(1);
                          clearLine(2);
                          lcd.cursorTo(1, 0);             
                          lcd.printIn(">");
                          Menu--;
                            if (Menu<0)
                            {Menu = 3;}
                          lcd.cursorTo(1, 2);
                          lcd.printIn(sequenceMenu[Menu]);
                          break;
                          
                          case 1:             // Menu niveau 2
                              switch(Menu) {
                              case 0:          // Menu "reglages"
                                  clearLine(2);
                                  lcd.cursorTo(2, 0);             
                                  lcd.printIn(">");
                                  Choix--;
                                  if (Choix<0)
                                      {Choix = 6;}
                                      lcd.cursorTo(2, 2);             
                                      lcd.printIn(sequenceChoix[Choix]);
                              break;
                              case 1:          // Menu "manuel"
                                  clearLine(2);
                                  lcd.cursorTo(2, 0);             
                                  lcd.printIn(">");            
                                  Choix--;
                                    if (Choix<0)
                                    {Choix = 6;}
                                  lcd.cursorTo(2, 2);             
                                  lcd.printIn(sequenceChoix[Choix]);
                              break;
                              }
                          break;
                          }
                    break;
                    
                 case downKey:
                 break;
                    
                 case rightKey:
                 break;
                
                 case leftKey:   
                 break;
                        
                 case selectKey:
                    switch (General){
                          case 0:             // Menu niveau 1  
                          clearLine (1);
                          lcd.cursorTo(1, 2);              
                          lcd.printIn(sequenceMenu[Menu]);
                          General++;
                          Choix = 0;
                          if (Menu>=2)
                          {}  
                          else
                          {
                          lcd.cursorTo(2, 0);
                          lcd.printIn(">");    
                          lcd.cursorTo(2, 2);
                          lcd.printIn(sequenceChoix[Choix]);
                          }
                          break;
                          
                          case 1:
                              switch(Menu) {
                              case 0:          // Menu secondaire "reglages"
                                clearLine(1);
                                clearLine(2);
                                lcd.cursorTo(1, 1);
                                lcd.printIn(">");
                                lcd.cursorTo(1, 3);
                                lcd.printIn(sequenceOptions[0]);
                                lcd.cursorTo(2, 3);
                                lcd.printIn(sequenceOptions[1]);
                                General++;
                              break;
                              case 1:          // Menu secondaire "manuel"
                                clearLine(1);
                                clearLine(2);
                                lcd.cursorTo(1, 1);
                                lcd.printIn(">");
                                lcd.cursorTo(1, 3);
                                lcd.printIn(sequenceManuel[0]);
                                lcd.cursorTo(2, 3);
                                lcd.printIn(sequenceManuel[1]);
                                General++;
                              break;
                              case 2:          // Menu secondaire "début programme"
                              clearLine(1);
                              clearLine(2);
                              program = 0;
                              break;
                              case 3:          // Menu secondaire "Reglage heure"
                              Set();
                              break;
                              }
                          break;
                          }
                    break;    
           }
          }
}}  
}

A good post has several parts.

It starts with a statement of the problem. I am trying to do this or that...

It contains a description of the expected results. I press these keys, and this appears in the monitor window...

It contains a description of the actual results. Instead, when I press the third key, this happens...

It contains some code. Here is my code:

// Code would be here...

It contains a description of the hardware and how its connected or a picture or schematic. Here is a schematic of my hardware...

Your post contains some code and a description of what you want to have happen.

How does what you want to have happen differ from what actually happens? What hardware do you have, and how is it connected?

Ok, I reply to all yours questions...

It starts with a statement of the problem. I am trying to do this or that...
In the main loop, I want enter the menu when a key is press.
After that in the menu, I choose "program start" and return main loop after pressing select key.

It contains a description of the expected results. I press these keys, and this appears in the monitor window...

The program start, there is hour which refresh on the screen but nothing matter when I press key. If I test the menu without the main loop, all's OK

It contains some code.
The code is in my last reply

It contains a description of the hardware and how its connected or a picture or schematic. Here is a schematic of my hardware...

An arduino card duemilanove + an lcd screen (2x16) with Joystick style Keypad with 5 pushbuttons + a DS1307 for date

There is no connection problem : I've ever test the Keypad and lcdscreen with the menu and test the DS1307 (I set the date and write and refresh it on the screen.

Thanks for your help.

Some more questions, then.

  affichage_heure();

This function is not included in the code you posted. What does it do?

Every time through loop, this code is executed:

  for (int i=0; i<4;i++)
  {
     if ( hour == Hallum[i] && minute == Mallum[i])
     {
     digitalWrite(i+1, HIGH);
     }
     if (hour==Hextin[i] && minute==Mextin[i])
     {
     digitalWrite(i+1, LOW);
     }
  }

What is attached to pins 1, 2, 3, and 4? Since hour and minute are never changed, and Hextin, Mextin, Hallum, and Mallum never change, either, all this is doing is setting pins 1, 2, 3, and 4 HIGH then LOW. The purpose of this is...?

In menu, you check which key is pressed, if any. If the key being pressed (including no key) is not the same as the last key pressed, you wait 50 milliseconds, and check again. If the key being pressed is not the same as the last key pressed, and there is a key being pressed, you execute the switch statement.

Add Serial.print statements before to show the value of oldkey and key, with descriptive text.

            oldkey = key;
                Serial.print("oldkey = ");
                Serial.println(oldkey, DEC);
                Serial.print("key = ");
                Serial.print(key, DEC);
            oldkey = key;

This will tell you whether the program correctly determined that a (different) key was pressed.

In each of the case blocks in the switch statement, based on key), add a Serial.println statement to confirm that the correct case is executed.

I think you'd have better luck seeing what was happening if you created some new functions. The entire switch(key) block should be placed in a function like "void handleKeyPress(int key)".

The handleKeyPress function should call other functions, such as handleUpKey, handleDownKey, handleSelectKey, etc.

Don't include case statements that don't do anything. There is no reason to have case statements for the left and right switches when nothing happens if you press the left or right switches.

In each function, use Serial.println to show when the function was entered and exited.

void handleKeyPress(int key)
{
   Serial.println("handleKeyPress ==>");
   // Do something useful
   Serial.println("handleKeyPress <==");
}

If, after doing this, you still have problems, come on back, and post the revised code AND output. I'm happy to help you get this working. I just don't understand (yet) what the problem is.

If I test the menu without the main loop, all's OK

How do you test the menu without the main loop?

Hello, as I see, this problem is similar, than my post about Fire oven describes.
But sorry, I couldn't help you.... because I'm starting programmer...

affichage_heure();

This function is not included in the code you posted. What does it do?

Well, just guesing, but it displays the time. ;D

"Affichage heure" is just a jonction to display time :wink:

Pin 1, 2, 3 & 4 command four lights.
Hour, minute change, it's refresh with RTC. The idea is to switch on/off the lights at differents time (Hallum & Mallum = hour and minute for switching on, Hextin & Mextin = hour and minute for switching off).

Testing menu : I've download it in the Arduino and I can navigate and choose every options in it. All keypress operate.

If I download all the program, time refresh on screen but I can press any key, It's impossible to go to the menu. That's my problem : calling and closing the menu in the main loop

I'll modify my program and add "serial print".

If you forget about the lights for the moment, your loop function looks like:

void loop()
{
  affichage_heure();
  LED_levels_output();
  menu();
}

LED_levels_output() does nothing, so your down to show the time and navigate the menu.

The first thing that happens in the menu function, if any key is pressed, is that the LCD is cleared, and a prompt shown. But, menu is not a blocking function, so it ends, and loop is called again. The time is shown (whether on one line or 2 I don't know, since that code is top secret). Oops, there goes the prompt and menu item...

Your choices seem to come down to:

  • Use a larger LCD screen (4 lines) so both time and menu can be shown
  • Have a button that enters/exits menu mode. Don't show the time in menu mode
  • Add a second LCD for the menu