sketch seems to skip lines..

Dear Fellow Arduino users,

I am working on a project which in a nutshell, asks the user for an option to use last settings (which would be read form the EEPROM). Or enter new settings (These settings would be limits of excursion of a ram). Once these parameters have been entered, the sketch would then go to the main loop which will execute the ram within the set limits while checking for parameters to stop. I have been trying a few techniques to get this going (I have tried While and Do- While loops to wait for the user input within the setup function). And finally I feel that multiple functions, each for one section of the task is the simplest way. What I also want to do is at each stage, there would be an option to go back to the main setting. My sketch works fine but the problem is that after a few cycles (sometime 4, sometimes 1) the sketch skips straight to the main loop without going through the rest. Please help. The sketch that I have uploaded is the simplified version for discussion purposes. I just started using Arduino or any microcontrollers, So please forgive me if i am asking the obvious… In the example i have given below, there are two buttons pulled down with 10K resistors so that pressing would make the signal go high. I am using Adruino UNO R3 with IDE 1.0…Thank you in advance for all you help.

// Initialize LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd (8,9,10,11,12,13);

// define variables
int yes = A0;
int no = A1;



// setup
void setup(){
  
 pinMode (yes, INPUT); 
 pinMode (no, INPUT); 
lcd.begin (16,2);
start();  // call (start) function
}
void start(){    // start function (introduction screen)
lcd.clear();
lcd.print ("WELCOME");
delay (1000);
lcd.clear();
lcd.print (" 1    2");
while (digitalRead (yes)== LOW && digitalRead (no) ==LOW){  //waits for button to be pressed
 if (digitalRead (yes) == HIGH){   // what to do if yes is pressed
   delay (500);                    // to give enough time to unpressed the button
   
  button_one();                  // to call (button one) function
 }
 
if (digitalRead (no) == HIGH){    // What to do if no button was pressed
  delay (500);                    // to give enough time to unpressed the button
 button_two();
}
}
}

// button one                // button one function
void button_one(){
   lcd.clear ();
lcd.print ("BUTTON ONE");
delay (500);
start();                    // goes back to start
}

// button two

void button_two(){            // button two function
 lcd.clear ();
lcd.print ("BUTTON two");
delay (500);
start();                      // goes back to start
}


                            // main loop
void loop(){
  
 lcd.clear();
lcd.print ("MAIN LOOP");
delay (1000);
 


}
start();  // call (start) function

Really? I'd have never guessed that that was what that code did. Learn something new every day.

start() calls button_one() which calls start(). Can you say "stack overflow"? I knew you could.

Recursion is not the way to go about this. When button_one() ends, it will return to start() without your "help".

Dear PaulS,

Sorry, perhaps my description was not adequate, The actual thing that i am working on would not do this, I only wrote this sketch to ilustrate the problem i am facing. I purposely wanted the sketch to loop over and over again. Logically, based on what I have written, the main loop would never run right? But what actually happens is that it works once twice or three times (it is unpredictable how many times it loops, sometimes it goes straight to the main loop on the first button press). My question is why is that happening when the code just loops around start?. I have a feeling it may be hardware related, but changing the delay (to allow adequate time to take the finger off the switch) does not seem to help. But with this test code, even if there is a short in the switch, it should not go to the loop function, but it does...

But with this test code, even if there is a short in the switch, it should not go to the loop function, but it does...

There are fundamental flaws in that code. You really need to address them, and then see if there is still a problem.

First, Do yourself a favor and use Tools → Auto Format in the IDE to clean up your code’s indentation. It’ll make your code much easier to read.

Second, you’ve defined analog inputs for the buttons, is this what you meant to do?

Third, does this code have the same issue or is this just an example of what you think the code is doing?

Sorry for the formating issue,

Second:[quote author=James C4S link=topic=104677.msg785227#msg785227 date=1336332058]
Second, you’ve defined analog inputs for the buttons, is this what you meant to do?
[/quote]

I used analog input because my actual project needs LCD and motor controls as well as interrupts, thus since I was using most of the other pins for those, I decided to use the analog pin as digital input pins for the switches.

Yes it has the same issues, I wrote this code to experiment with when the actual project code gave me the problem as a trouble shooting measure. with some modification (the actual does not have an infinite loop before reaching the main loop function but one of the button would ask if you want to go back to previous step which would then bring you to the start otherwise proceed to the next step.)

If there is a fundamental flaw, please do point it out, I have been looking at this for some time now, but can’t really notice it. To make things worse, it does work sometimes but rather erratic… Since i am new to MCU programming and electronics as a whole, I hope someone can provide some guidance or suggest some alternatives to try. Thanks . below is the reformatted code…

// Initialize LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd (8,9,10,11,12,13);

// define variables
int yes = A0;
int no = A1;



// setup
void setup(){

  pinMode (yes, INPUT); 
  pinMode (no, INPUT); 
  lcd.begin (16,2);
  start();  // call (start) function
}
void start()
{                                  // start function (introduction screen)
  lcd.clear();
  lcd.print ("WELCOME");
  delay (1000);
  lcd.clear();
  lcd.print (" 1    2");
  while (digitalRead (yes)== LOW && digitalRead (no) ==LOW)
  {                                  //waits for button to be pressed
    if (digitalRead (yes) == HIGH)
    {                                 // what to do if yes is pressed
      delay (500);                    // to give enough time to unpressed the button

      button_one();                  // to call (button one) function
    }

    if (digitalRead (no) == HIGH)
    {                              // What to do if no button was pressed
      delay (500);                    // to give enough time to unpressed the button
      button_two();
    }
  }
}

// button one                // button one function
void button_one()
{
  lcd.clear ();
  lcd.print ("BUTTON ONE");
  delay (500);
  start();                    // goes back to start
}

// button two

void button_two()
{                              // button two function
  lcd.clear ();
  lcd.print ("BUTTON two");
  delay (500);
  start();                      // goes back to start
}


// main loop
void loop()
{

  lcd.clear();
  lcd.print ("MAIN LOOP");
  delay (1000);



}

If there is a fundamental flaw, please do point it out

I already did. start() calls button_one() which calls start(). You must eliminate the recursion.

Dear PaulS,

Thanks for the suggestion, I tried removing the recursions and modified the code further, It is much more stable not, but now the occasional skip to the main loop still happens but much more stable (not sure why is that perhaps the delay is not adequate). Thanks and have a good day.

If you want us to look at the modified code, post it again.

Thanks for the offer, I would take it up. But first I need to leave country for a conference overseas. After that I will work on the code again. Let me try to make it more usable to my needs before posting it up again. Thanks again