Execute function call from Main loop once.

Thread Starter

ajitnayak

Joined Feb 22, 2013
49
http://paste.ubuntu.com/7037458/

Dear all,
I have simple code ,I wanted to execute LCD_call function once from main loop.Here i am trying to set menu. i have 1 windows label1: Display and Set_parameter window. up and down button to navigte menu under each screen , right to change MEnu window. LEFT key to exit.

problem : When LCD _call is called , Inside loop it execute the display paramter and all. As per me it must stay in wherever the displayed window stopped unless any key changed.But here after every 6s display menu changes to loop(). Is there any way to execute once

Rich (BB code):
void loop()
{
  int button;
  while(( button = read_LCD_buttons()) != btnLEFT)
  {
    lcd_call();
  }
}

void lcd_call()
{

  lcdClear();
  lcd.print("MENU DISPLAY:"); 
  // button = read_LCD_buttons();
  while (menuOption <= menuOptions)
  {
    /*if (button == btnRIGHT)
     {
     //timedBeep(shortBeep,1);   
     menuOption=menuOption+1;
     Serial.print("menuOption:");
     Serial.println(menuOption);
     }
     else
     {
     menuOption=1;
     Serial.print("menuOption:");
     Serial.println(menuOption);
     }*/

    //button = read_LCD_buttons();
    switch(menuOption)
    {

    case 1: 
      lcdClear();
      lcd.print("DISPLAY PARAMETER");
      timedBeep(beep,1);
      delay(300);
      //button = read_LCD_buttons();
      if((button = read_LCD_buttons()) != btnRIGHT)
      {
        goto LABEL1;
      }
      else
      {
        goto SETPARAMETER;
      }

LABEL1:   
      while((button = read_LCD_buttons()) != btnRIGHT)

      {

        int DS_Counter;
        Serial.println(button);
        DS_Counter=Display_selection();
        Serial.print("DS_Counter:");
        Serial.println(DS_Counter);
        switch(DS_Counter)
        {
        case 1:  
          lcd.setCursor(0,0); 
          digitalClockDisplay();
          timedBeep(shortBeep,1);  
          break;
        case 2: 
          lcd.setCursor(0,0);
          Display_angle();
          timedBeep(shortBeep,1); 
          break;
        case 3: 
          lcd.setCursor(0,0);
          Display_coordinate();
          timedBeep(shortBeep,1);
          break;
        case 4:
          lcd.setCursor(0,0);
          Display_Wind();
          timedBeep(shortBeep,1);
          break;
        case 5:
          DS_Counter=1; 
          break;   
        }
        // button = read_LCD_buttons();

      } 


    case 2:  
      lcdClear();
      lcd.print("SET PARAMETER");
      timedBeep(beep,1);  
      delay(150); 
      if((button = read_LCD_buttons()) != btnRIGHT)
      {
        // delay(1000);
        goto SETPARAMETER; 
      }
      else
      {
        goto LABEL;
      }
SETPARAMETER:    
      // while ( (button = read_LCD_buttons()) != btnSELECT)
      while((button = read_LCD_buttons()) != btnLEFT)
      {  
        // int Set_Counter=0;
        Serial.println(button);      
        Set_Counter=Select_section();
        Serial.print("Set_Counter:");
        Serial.println(Set_Counter);
        switch(Set_Counter)
        {
        case 1:           
          if(Set_lat_Flag==0)
          {
            set_latitude();
            timedBeep(shortBeep,1);
            //goto COUNTER;
          }
          else
          {
            break;
          }
        case 2:              
          if(Set_long_Flag==0 )
          {
            set_longitude();
            timedBeep(shortBeep,1);
            //goto COUNTER;
          }
          else
          {
            break;
          }
        case 3:              
          if(Set_Track_flag==0)
          {
            set_trackertime();
            timedBeep(shortBeep,1);
          }
          else
          {
            // goto COUNTER;
            break;
          }

        case 4:             
          if( Set_DT_Flag==0)
          {
            Serial.print("Set_DT_Flag:");
            Serial.println(Set_DT_Flag);
            setDateTime();
            timedBeep(shortBeep,1);
          }
          else
          {
            Set_Counter=0;
            Print_Date();
            Print_Time();
            //goto COUNTER;
            break;
          }

        case 5:             
          Set_Counter=0;
          Set_flag=0;
          Set_DT_Flag=0;
          Set_lat_Flag=0;
          Set_long_Flag=0;
          Set_Track_flag=0;
          goto LABEL;
          break; 


        }
        button = read_LCD_buttons();
        goto LABEL;
        //Set_Counter=Select_section();  
        //menuOption++;     
      }
      //menuOption=Menu_counter();  //break;//menuOption=Menu_counter();break;
LABEL:    
    case 3:
      menuOption=0;
      lcd.begin(16,2);
      lcd.setCursor(0,0);
      lcd.print("EXIT WINDOW");
      timedBeep(beep,1);
      //delay(300);
      if((button = read_LCD_buttons()) != btnRIGHT)
      {
        //delay(1000);
        //goto SETPARAMETER; 
      }
      else
      {
        goto LABEL1;
      }

      while((button = read_LCD_buttons()) != btnLEFT)
      {
        menuOption=0; 
        break; 
      }

    }


    button = read_LCD_buttons();
    if (button == btnRIGHT)
    {
      timedBeep(shortBeep,1);   
      menuOption=menuOption+1;
      Serial.print("menuOption:"); 
      Serial.println(menuOption);
    }
    else
    {
      menuOption;
      Serial.print("menuOption:"); 
      Serial.println(menuOption);
    }

  }

  //return 1;
}
 

sirch2

Joined Jan 21, 2013
1,037
I'm not sure I fully understand what you want to do but if, as you say you "wanted to execute LCD_call function once" then don't call it from within the while loop.

Just having a quick scan through your code I think I would check the button status in the main loop and then have separate functions for each button action. I would also avoid using goto's especially in the middle of a case statement.
 

takao21203

Joined Apr 28, 2012
3,702
It's extermly bad code- normally GOTO is used to avoid to use switch but mixing both?

You should not program like that.

Write a small interpreter, and put sequences of LCD commands into tables. Because when done with C, it results in extremly bad code.

Also it is weird you are able to construct a complicate switching structure with GOTOs, and then you are not able to determine or to control how many times something is executed.

Well, remembering SWITCH CASE from my high school days (BASIC), yes it looks like it dictates the way of the program flow, and only certain combinations are possible.

SWITCH is a straightjacket, don't use it at all.

For creating the interpreter, write on a sheet of paper all possible actions, and the timing, and determine the granularity of the timing.

And remove the buttons input from that by all means, handle seperately.

I'd know how to do it, but it's hard to line out each indivisual step on the internet. Hope above helps.
 

Gibson486

Joined Jul 20, 2012
355
Probably a dumb way to do it...and I am not 100% sure I understand your question...

I would just add a variable that monitors when you enter that function. When you enter it, make the variable 1 or some other number. In order to enter that function, that variable has to be 0 or some other number you tell it to be. Or, you could allow it to enter the function, but pass a value that tells the function to make changes or keep everything the same.
 

shteii01

Joined Feb 19, 2010
4,644
Probably a dumb way to do it...and I am not 100% sure I understand your question...

I would just add a variable that monitors when you enter that function. When you enter it, make the variable 1 or some other number. In order to enter that function, that variable has to be 0 or some other number you tell it to be. Or, you could allow it to enter the function, but pass a value that tells the function to make changes or keep everything the same.
+1 to what Gibson said.

Basically attach a counter to the function. Then just check the counter.

When the counter is 0, execute the function, increment the counter, so counter is now 1.

When counter is 1, skip the code that calls the function.
 

spinnaker

Joined Oct 29, 2009
7,830
First off your coding style is horrible (no offense it just is). There is rarely reason to use a goto.

I am confused at what you want to do. You say you want to execute lcd_call just once. Why not just put it outside the loop? Or just set a flag.

But I think you are going to have to make yourself more clear.

void loop() { int button;
char flag = 0;
while(( button = read_LCD_buttons()) != btnLEFT) { if (!flag)
{
lcd_call();
flag = 1;
}
} }
 

shteii01

Joined Feb 19, 2010
4,644
First off your coding style is horrible (no offense it just is). There is rarely reason to use a goto.

I am confused at what you want to do. You say you want to execute lcd_call just once. Why not just put it outside the loop? Or just set a flag.

But I think you are going to have to make yourself more clear.

void loop() { int button;
char flag = 0;
while(( button = read_LCD_buttons()) != btnLEFT) { if (!flag)
{
lcd_call();
flag = 1;
}
} }
My WAG is that we are looking at Arduino code. If it is... then they should put the function into the setup() section. This way the function is executed once.
 

ErnieM

Joined Apr 24, 2011
8,377
As per me it must stay in wherever the displayed window stopped unless any key changed.But here after every 6s display menu changes to loop().Is there any way to execute once
Plan your work, then work your plan:

1) write out these steps as you want them. Start in very general terms, then add specifics.

2) write the code to perform the outline you made in #1

As a beginning programmer you may have problems with these two simple steps, so feel free to post your efforts at #1 so we can help you.


My menu code follows a pattern like this:
- draw menu
- wait for key release (from button that got us here)
loop:
- wait for key press
- change display based on key pressed
- return to loop or exit based on key pressed
 
Top