Execute function call from Main loop once.

Discussion in 'Programmer's Corner' started by ajitnayak, Mar 5, 2014.

  1. ajitnayak

    Thread Starter Member

    Feb 22, 2013
    36
    0
    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

    Code ( (Unknown Language)):
    1.  
    2. void loop()
    3. {
    4.   int button;
    5.   while(( button = read_LCD_buttons()) != btnLEFT)
    6.   {
    7.     lcd_call();
    8.   }
    9. }
    10.  
    11. void lcd_call()
    12. {
    13.  
    14.   lcdClear();
    15.   lcd.print("MENU DISPLAY:");
    16.   // button = read_LCD_buttons();
    17.   while (menuOption <= menuOptions)
    18.   {
    19.     /*if (button == btnRIGHT)
    20.      {
    21.      //timedBeep(shortBeep,1);  
    22.      menuOption=menuOption+1;
    23.      Serial.print("menuOption:");
    24.      Serial.println(menuOption);
    25.      }
    26.      else
    27.      {
    28.      menuOption=1;
    29.      Serial.print("menuOption:");
    30.      Serial.println(menuOption);
    31.      }*/
    32.  
    33.     //button = read_LCD_buttons();
    34.     switch(menuOption)
    35.     {
    36.  
    37.     case 1:
    38.       lcdClear();
    39.       lcd.print("DISPLAY PARAMETER");
    40.       timedBeep(beep,1);
    41.       delay(300);
    42.       //button = read_LCD_buttons();
    43.       if((button = read_LCD_buttons()) != btnRIGHT)
    44.       {
    45.         goto LABEL1;
    46.       }
    47.       else
    48.       {
    49.         goto SETPARAMETER;
    50.       }
    51.  
    52. LABEL1:  
    53.       while((button = read_LCD_buttons()) != btnRIGHT)
    54.  
    55.       {
    56.  
    57.         int DS_Counter;
    58.         Serial.println(button);
    59.         DS_Counter=Display_selection();
    60.         Serial.print("DS_Counter:");
    61.         Serial.println(DS_Counter);
    62.         switch(DS_Counter)
    63.         {
    64.         case 1:  
    65.           lcd.setCursor(0,0);
    66.           digitalClockDisplay();
    67.           timedBeep(shortBeep,1);  
    68.           break;
    69.         case 2:
    70.           lcd.setCursor(0,0);
    71.           Display_angle();
    72.           timedBeep(shortBeep,1);
    73.           break;
    74.         case 3:
    75.           lcd.setCursor(0,0);
    76.           Display_coordinate();
    77.           timedBeep(shortBeep,1);
    78.           break;
    79.         case 4:
    80.           lcd.setCursor(0,0);
    81.           Display_Wind();
    82.           timedBeep(shortBeep,1);
    83.           break;
    84.         case 5:
    85.           DS_Counter=1;
    86.           break;  
    87.         }
    88.         // button = read_LCD_buttons();
    89.  
    90.       }
    91.  
    92.  
    93.     case 2:  
    94.       lcdClear();
    95.       lcd.print("SET PARAMETER");
    96.       timedBeep(beep,1);  
    97.       delay(150);
    98.       if((button = read_LCD_buttons()) != btnRIGHT)
    99.       {
    100.         // delay(1000);
    101.         goto SETPARAMETER;
    102.       }
    103.       else
    104.       {
    105.         goto LABEL;
    106.       }
    107. SETPARAMETER:    
    108.       // while ( (button = read_LCD_buttons()) != btnSELECT)
    109.       while((button = read_LCD_buttons()) != btnLEFT)
    110.       {  
    111.         // int Set_Counter=0;
    112.         Serial.println(button);      
    113.         Set_Counter=Select_section();
    114.         Serial.print("Set_Counter:");
    115.         Serial.println(Set_Counter);
    116.         switch(Set_Counter)
    117.         {
    118.         case 1:          
    119.           if(Set_lat_Flag==0)
    120.           {
    121.             set_latitude();
    122.             timedBeep(shortBeep,1);
    123.             //goto COUNTER;
    124.           }
    125.           else
    126.           {
    127.             break;
    128.           }
    129.         case 2:              
    130.           if(Set_long_Flag==0 )
    131.           {
    132.             set_longitude();
    133.             timedBeep(shortBeep,1);
    134.             //goto COUNTER;
    135.           }
    136.           else
    137.           {
    138.             break;
    139.           }
    140.         case 3:              
    141.           if(Set_Track_flag==0)
    142.           {
    143.             set_trackertime();
    144.             timedBeep(shortBeep,1);
    145.           }
    146.           else
    147.           {
    148.             // goto COUNTER;
    149.             break;
    150.           }
    151.  
    152.         case 4:            
    153.           if( Set_DT_Flag==0)
    154.           {
    155.             Serial.print("Set_DT_Flag:");
    156.             Serial.println(Set_DT_Flag);
    157.             setDateTime();
    158.             timedBeep(shortBeep,1);
    159.           }
    160.           else
    161.           {
    162.             Set_Counter=0;
    163.             Print_Date();
    164.             Print_Time();
    165.             //goto COUNTER;
    166.             break;
    167.           }
    168.  
    169.         case 5:            
    170.           Set_Counter=0;
    171.           Set_flag=0;
    172.           Set_DT_Flag=0;
    173.           Set_lat_Flag=0;
    174.           Set_long_Flag=0;
    175.           Set_Track_flag=0;
    176.           goto LABEL;
    177.           break;
    178.  
    179.  
    180.         }
    181.         button = read_LCD_buttons();
    182.         goto LABEL;
    183.         //Set_Counter=Select_section();  
    184.         //menuOption++;    
    185.       }
    186.       //menuOption=Menu_counter();  //break;//menuOption=Menu_counter();break;
    187. LABEL:    
    188.     case 3:
    189.       menuOption=0;
    190.       lcd.begin(16,2);
    191.       lcd.setCursor(0,0);
    192.       lcd.print("EXIT WINDOW");
    193.       timedBeep(beep,1);
    194.       //delay(300);
    195.       if((button = read_LCD_buttons()) != btnRIGHT)
    196.       {
    197.         //delay(1000);
    198.         //goto SETPARAMETER;
    199.       }
    200.       else
    201.       {
    202.         goto LABEL1;
    203.       }
    204.  
    205.       while((button = read_LCD_buttons()) != btnLEFT)
    206.       {
    207.         menuOption=0;
    208.         break;
    209.       }
    210.  
    211.     }
    212.  
    213.  
    214.     button = read_LCD_buttons();
    215.     if (button == btnRIGHT)
    216.     {
    217.       timedBeep(shortBeep,1);  
    218.       menuOption=menuOption+1;
    219.       Serial.print("menuOption:");
    220.       Serial.println(menuOption);
    221.     }
    222.     else
    223.     {
    224.       menuOption;
    225.       Serial.print("menuOption:");
    226.       Serial.println(menuOption);
    227.     }
    228.  
    229.   }
    230.  
    231.   //return 1;
    232. }
    233.  
     
  2. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
  3. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  4. Gibson486

    Member

    Jul 20, 2012
    199
    12
    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.
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,451
    3,370
    I have moved your thread from "General Electronics Chat" to "Programmer's Corner".
     
  6. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,398
    497
    +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.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
    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;
    }
    } }
     
  8. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,398
    497
    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.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    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
     
Loading...