In computer science, a subroutine or subprogram (also called procedure, method, function, or routine) is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code.
A function is a way for programmers to reuse code without having to rewrite the code.
This is time saving, and often makes code more readable, if the function names are well selected, that is.
void - does not return a value
arguments - numbers you pass into the function
Here is a sketch that blinks an LED on and off slow and then fast.
void setup()
{
pinMode(10, OUTPUT); // initialize the digital pin as an output
}
void loop()
{
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for 0.2 of a second
digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
delay(200); // wait for 0.2 a second
}
If we wanted to do some Morse code and blink the led
it would take a lot of repeated code and it would get confusing.
Functions make this much easier. Create your own functions AFTER the void loop(), and then you can use them in void loop(). The compiler will find the functions below. In Arduino, you always put your user-created functions AFTER void loop(). In other languages you put them before you use them.
void setup()
{
pinMode(10, OUTPUT); // initialize the digital pin as an output
}
void loop() //void loop is your main program that runs in a continuous loop
{
blinkFast();
blinkSlow();
blinkSlow();
blinkFast();
blinkFast();
blinkSlow();
}
void blinkSlow () //only runs when called from void loop
{
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(1000);
}
void blinkFast () //only runs when called from void loop
{
digitalWrite(10, HIGH);
delay(200);
digitalWrite(10, LOW);
delay(200);
}
Here’s another example of a function using our robots.
// Set up the pins with variable names
// Left motor
const int enable1=3; // const=constant, int=integer
const int input1=4;
const int input2=2;
// Right motor
const int enable2=5;
const int input3=6;
const int input4=7;
1. Declare your global variables (variables used through entire program)
void setup() {
//initialize the input pins as digital outputs
// the enable pins will use analogWrite, so they don't need to be initialized
pinMode (input1, OUTPUT);
pinMode (input2, OUTPUT);
pinMode (input3, OUTPUT);
pinMode (input4, OUTPUT);
delay (4000);
}
2. Setup pins etc. - runs once at beginning.
void loop() //this is your main program that runs in a continuous loop
{
forward();
delay (1000);
backward ();
delay (2000);
right();
delay(500);
backward();
delay(1000);
left()
delay(250);
}
3. This is your program that runs infinitely in a loop until your batteries run out
void forward() // only runs when called by void loop
{
analogWrite(enable1, 255); //left motor full speed
digitalWrite(input1, HIGH); //these two lines turn the left motor in one direction
digitalWrite(input2, LOW); //if it's not forward, swap your red and black motor wires
analogWrite(enable2, 255); //right motor full speed
digitalWrite(input3, HIGH);
digitalWrite(input4, LOW);
}
void backward() // only runs when called by void loop
{
analogWrite(enable1, 255); //left motor full speed
digitalWrite(input1, LOW);
digitalWrite(input2, HIGH);
analogWrite(enable2, 255); //right motor full speed
digitalWrite(input3, LOW);
digitalWrite(input4, HIGH);
}
void right()
{
analogWrite(enable1, 255); //left motor full speed
digitalWrite(input1, LOW);
digitalWrite(input2, HIGH);
analogWrite(enable2, 255); //right motor full speed
digitalWrite(input3, HIGH);
digitalWrite(input4, LOW);
}
4. User-created functions go below your void loop. They ONLY run if they are called from void loop ()
Exercise
Your previous assignment was to have your robot drive in a 10cm square. Rewrite your program creating two functions:
Show me that your robot still moves in a 10cm square and resubmit the code.