Nested functions?

Can anyone offer guidance as to why my program wont work. Im new to arduino and I want to call a series of functions within eachother but for some reason i cant get the first function to switch over to the second.

#include <LiquidCrystal.h>
#include <Stepper.h>

//Start the time at 0
int time = 0;

//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // correspond to RS,EN,D4,D5,D6,D7

int stepIN1Pin = 8;
int stepIN2Pin = 9;
int stepIN3Pin = 10;
int stepIN4Pin = 11;

int stepIN1Pin1 = 52;
int stepIN2Pin1 = 53;
int stepIN3Pin1 = 50;
int stepIN4Pin1 = 51;

int stepsPerRevolution = 2048; // amount of steps per revolution

const int button1Pin = 2; // pushbutton 1 pin for clockwise
const int button2Pin = 3; // pushbutton 2 pin for no rotation

Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);
Stepper myStepper1(stepsPerRevolution, stepIN1Pin1, stepIN3Pin1, stepIN2Pin1, stepIN4Pin1);
Stepper myStepper2(stepsPerRevolution, stepIN1Pin2, stepIN3Pin2, stepIN2Pin2, stepIN4Pin2);

void Question1(){
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("function1");

int button1State, button2State;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);

if (button1State == HIGH){ // if we're pushing button 1 OR button 2
delay(1000);
myStepper.step(stepsPerRevolution/8);
Question2();

}

if (button2State == HIGH){
myStepper.step(0);
Question2();
}
}

void Question2(){
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("function2");

int button1State, button2State;

button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);

if (button1State == HIGH){ // if we're pushing button 1 OR button 2
delay(1000);
myStepper1.step(stepsPerRevolution/8);

//Question3();
}
if (button2State == HIGH){
myStepper1.step(0);

//Question3();
}
}

void setup()
{

pinMode(button1Pin, INPUT);
myStepper.setSpeed(15);
myStepper1.setSpeed(15);

Question1();
// myStepper2.setSpeed(15);
// myStepper3.setSpeed(15);
// myStepper4.setSpeed(15);
}

void loop()
{

void Question1();.

}

Not saying it's the cause of the problem but you should more than likely not use lcd.begin every time; use it once in setup().

void loop()
{
  void Question1();
  .
}

This is an example of a function calling a function. What is that full stop doing in the code ?

I note that it does not compile

UKHeliBob:

void loop()

{
  void Question1();
  .
}



This is an example of a function calling a function.

No.

This is a declaration of a function, not the call of a function.

Whandall:
No.

This is a declaration of a function, not the call of a function.

Silly old me. There I was thinking that the Question1() function was being called.

In my program 'question1()' is being called and after a button is pressed, I want it to call 'Question2()' and do the same thing after a button is pressed but for some reason Question1() is the only function that gets called can anyone help??

christianlauder:
In my program 'question1()' is being called and after a button is pressed, I want it to call 'Question2()' and do the same thing after a button is pressed but for some reason Question1() is the only function that gets called can anyone help??

How about following the forum rules and posting code inside code tags, then maybe you will get more help...

I want it to call function 2 inside function 1 but it wont work why is this?

I changed my loop portion to..

void loop(){

Question1();

}

now its calling this function but within this function its supposed to call Question2(); but its not working..

Just to make one thing clear, you can always call a function from inside another function in C/C++.

aarg:
Just to make one thing clear, you can always call a function from inside another function in C/C++.

then why wont my second function be called? is it because i have the first function being called inside the loop?

or does it have something to do with switching between steppers?

christianlauder:
then why wont my second function be called?

What I am saying is, you can be 100% sure that it is being called. If you call it, that is... :slight_smile: Note that it depends on a test of button1state.

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

So I'm trying to call 'Questin2()' within function 1 which is originally called in the loop.

When I click the button the first stepper rotates then question 2 should be called.

After I click the button again the if statement in question2() should cause the second stepper and only the second stepper to rotate.. but instead question1() is being called again i think.

Can anyone help me out??

#include <LiquidCrystal.h>
#include <Stepper.h>

boolean lastButton = LOW; //variable containing the previuos button state.
boolean currentButton = LOW; //variable containing the current button state.

//Start the time at 0
int time = 0;

//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);     // correspond to RS,EN,D4,D5,D6,D7


int stepIN1Pin = 8;         
int stepIN2Pin = 9;
int stepIN3Pin = 10;
int stepIN4Pin = 11;

int stepIN1Pin1 = 52;         
int stepIN2Pin1 = 53;
int stepIN3Pin1 = 50;
int stepIN4Pin1 = 51;

int stepsPerRevolution = 2048; // amount of steps per revolution
  
const int button1Pin = 2;  // pushbutton 1 pin for clockwise 
const int button2Pin = 3;  // pushbutton 2 pin for no rotation 

boolean debounce(boolean last)
{
  boolean current = digitalRead(button1Pin); //read the button state
  if (last != current)              //if its diffent...
  {
    delay(5);                       //wait 5ms
    current = digitalRead(button1Pin);       //read 2 again
  }
  return current;                   //return the current value  
}


Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);
Stepper myStepper1(stepsPerRevolution, stepIN1Pin1, stepIN3Pin1, stepIN2Pin1, stepIN4Pin1);

void Question2(){
  lcd.begin(16, 2);
  lcd.print("Question 2");  
 
   currentButton = debounce(lastButton);                 //read the debounced state
if (lastButton == LOW && currentButton == HIGH)       //if it was pressed...
  {
  delay(1000);
  myStepper1.step(stepsPerRevolution/8);
  }   
  lastButton = currentButton;                       //reset
//  Question3();
}

void Question1(){
  
  lcd.begin(16, 2);
  lcd.print("Question 1");  

  currentButton = debounce(lastButton);                 //read the debounced state
if (lastButton == LOW && currentButton == HIGH)       //if it was pressed...
{
  delay(1000);
  myStepper.step(stepsPerRevolution/8);
  Question2();  //call question2, question1() stops running
  }   
  lastButton = currentButton; //reset button value  
}

void setup()
{
   pinMode(button1Pin, INPUT);
   myStepper.setSpeed(15);
   myStepper1.setSpeed(15);
  
}

void loop()
{
       Question1();
      
}
  Question2();  //call question2, question1() stops running

No, it doesn't stop running. It "calls" the other function and then resumes running on the line immediately below this call.

Both functions are using the global variables currentButton and lastButton. You call Question2 before you set the value of lastButton properly to catch up to the current state of the button. So Question2 gets handed a mix of partly-done stuff. It will have problems detecting the correct state of the button.

Since you've started numbering questions, I expect that you're going to have a lot more of these functions by the time you're finished. It would be much better to have all the questions in an array and then keep a number to record which question you're up to. Then you only need one function called askQuestion() and you might call it by...

  currentQuestion++; //advance the current question number
  askQuestion(currentQuestion);

Alternatively, currentQuestion might be a global variable and the askQuestion() function is allowed to increment it when a question is answered correctly.

@christianlauder, do not cross-post. Threads merged.