Shop OBEX P1 Docs P2 Docs Learn Events
Execute function call from Main loop once. — Parallax Forums

Execute function call from Main loop once.

ajit.nayak87ajit.nayak87 Posts: 76
edited 2014-03-05 23:11 in General Discussion
http://paste.ubuntu.com/7037458/

Dear all,

I have below piece of code here. i wanted to execute lcd_call function once from main loop. the problem i am facing here is i could able to stay in lcd_call(),every time it wait for 5 second and execute lcd_call function , IS there any way of doing it.

Comments

  • Hal AlbachHal Albach Posts: 747
    edited 2014-03-05 05:44
    Where is main() ?
  • mindrobotsmindrobots Posts: 6,506
    edited 2014-03-05 06:17
    It appears to be Arduino, so there won't be a main(), just init() and loop()

    If you put something inside loop(), it will do that , it will call it continuously. If you want something to be done just once, you put it in init().

    If you want somehting just done once from inside loop(), the you need to set a flag or something and wrap whatever you want done once in an if statement or a while loop.
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2014-03-05 07:36
    For the Arduino that would be setup() and loop(). But yes, setup() is for things you want done once, and at the start of the sketch.
  • mindrobotsmindrobots Posts: 6,506
    edited 2014-03-05 17:37
    Sorry, yes it's setup() - it's been a while since I Arduino'd.

    Thanks, Gordon.
  • ajit.nayak87ajit.nayak87 Posts: 76
    edited 2014-03-05 20:19
    I tried it already , But it still coming back to main MENU
  • ajit.nayak87ajit.nayak87 Posts: 76
    edited 2014-03-05 20:28
    SInce it is arduino there is no main() it called by loop(). I tried with keeping in Setup(), but executing same.
    void Setup()
    {
    lcd_call();
    Serial.begin(9600);
    }
    void loop()
    {
    }
    
    void lcd_call()
    {
     
      lcdClear();
      lcd.print("MENU DISPLAY:"); 
      // button = read_LCD_buttons();
      while ( (button = read_LCD_buttons())!=btnSELECT)
      {
        /*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);
               
              }
              else
              {
                break;
              }
            case 2:              
              if(Set_long_Flag==0 )
              {
                set_longitude();
                timedBeep(shortBeep,1);
              
              }
              else
              {
                break;
              }
            case 3:              
              if(Set_Track_flag==0)
              {
                set_trackertime();
                timedBeep(shortBeep,1);
              }
              else
              {
               
                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;
           
          }
          
    LABEL:    
        case 3:
          menuOption=0;
          lcd.begin(16,2);
          lcd.setCursor(0,0);
          lcd.print("EXIT WINDOW");
          timedBeep(beep,1);
         
          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);
        }
    
      }
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  • ajit.nayak87ajit.nayak87 Posts: 76
    edited 2014-03-05 23:11
    this what i am trying to do: i have 5 keys:
    right/up/down/left/.select
    Initially default screen would be time
    now if right key pressed once change Display to Display parameter
    MENU1:In Display_parameter:
    1. date-time
    2. coordinate
    3. wind speed
    4. desired angle
    5. exit or back to Display parameter screen
    NOTE: up/down key for navigating display screen LEFT key to exit and Display date/time RIGHT key to back to menu
    MENU2: Set parameter
    1. set date and time
    2. set coordinate
    3. set windlimit
    4. set timer
    up/down key for increment / decrement value , LEFT key to exit, right key to change menu, Select key Set /Change value

    From main i will call function name LCD call. If called and right key pressed it show Display parameter unless any other key being pressed once up key pressed it goes inside menu where we can use up/down key to navigate.in Display parameter it must stay in window example coordinate unless left key pressed or display menu being changed, SO similarly does with Set parameter.Let me know What change made in above code.
Sign In or Register to comment.