Void functions within void functions

I was wondering if you could use declared void functions in another void function, like the one below.

ArduinoFAQ.ino (343 Bytes)

You can certainly call functions from inside another function if that’s what you’re asking. You don’t call them like this though:

  function1; //calling upon function1

You need brackets:

  function1(); //calling upon function1

I know, my bad, I just tried to put in an example code quickly, I mean like this tho,

void function1()
{
  //dosomething
}

void function2()
{
  //do something else
}

void controlling_function()
{
  function1(); //calling upon function1
  delay(200);
  function2(); //calling upon function2
  delay(200);
}

void setup()
{
  //*Setup*
}

void loop()
{
  function1();
  delay(200)
  function2;
  delay(200);
  controlling_function();
}

WillemBataillie: I mean like this tho,

What happens when you try?

Compiling works, but since my Arduino among the other things I need are at school, I have no way of verifying it. It's supposed to be for controlling a little robot;

I have a void function for controlling each motor both in forward an backwards direction.

I would like to create a function that just makes them go both forward, backward, or counter to each other (for turning) within a new function.

These are the 2 parts of code I want to implement inside the third function.

void L_forward(int Lspeed) //left motor forward with Lspeed in %
{
digitalWrite(motorVL, HIGH);
digitalWrite(motorAL, LOW);
digitalWrite(enablemotorL, HIGH);
delayMicroseconds(Lspeed);
digitalWrite(enablemotorL, LOW);
delayMicroseconds(100-Lspeed);
}

void R_forward(int Rspeed) //right motor forward with Rspeed in %
{
digitalWrite(motorVR, HIGH);
digitalWrite(motorAR, LOW);
digitalWrite(enablemotorR, HIGH);
delayMicroseconds(Rspeed);
digitalWrite(enablemotorR, LOW);
delayMicroseconds(100-Rspeed);
}

void forward(int drivespeed) //both going forwar
{
  L_forward(drivespeed);
  R_forward(drivespeed);
}
void L_forward(int Lspeed) //left motor forward with Lspeed in %
{
digitalWrite(motorVL, HIGH);
digitalWrite(motorAL, LOW);
digitalWrite(enablemotorL, HIGH);
delayMicroseconds(Lspeed);
digitalWrite(enablemotorL, LOW);
delayMicroseconds(100-Lspeed);
}

Lines 3 through eight each call functions from within your L_forward() function.

I have a void function

Let's just say you have a function...

Yeah but those are just other names for the pins I use, I'm using an L293 motor driver IC wich connects to pins 7 -> 13, it's just easier knowing wich what it controls than rather knowing all the pins by heart.

Should have implemented these

#define enablemotorL 13
#define motorVL 12
#define motorAL 11
#define enablemotorR 10
#define motorVR 9
#define motorAR 8
[code]

my point was that you are already calling functions from functions, so the question has been asked and answered.

If you are asking if you can define a function within a function, the answer is no.

(deleted)

Welp at school the teacher just taught us differently, so now I see it as "Void functions" and that was the thing that confused me so much on this matter.

Thanks for telling it the correct way :)

or as you incorrectly call them, void functions

IMO there is nothing wrong with calling them void functions because that is what they are. It might be unusual to refer to them as such but not actually wrong.

What is wrong, and is seen here frequently, is to refer to them as "the void loop()" and "the void setup()" as this implies that they are "voids".

I suspect that there may also be an element of differences in languages at play here. I don't know what Willem's native language is but in French, for instance adjectives follow the noun, hence "maillot jaune" instead of the English "yellow jersey"

I, and many others, would refer to loop() and setup() as "the loop() function" and "the setup() function" leaving out the return data type which will, of course, be in the code itself.