Project 4.01 Using Functions

In this project, you'll learn the advantages of organizing your code with the help of functions!
At this point in our programming journey, we need to be using functions in our programs. Functions make programming much easier to read and write. They make doing the same thing repeatedly much easier. This program is actually the exact same as the last but put into a function.

Project Code:

				
					/////////////////////////////////////////
// 4.01 - Using functions

byte piezoPin = 12;

byte SW1 = 1;
bool pressed = LOW;


void setup() {
  pinMode(piezoPin, OUTPUT);
  pinMode(SW1, INPUT);
}

void loop() {
  if (digitalRead(SW1) == pressed) {
    BuzzPiezo();
  }
}

/*
  It is good practice to put a function description here.
  Example:
  This function applies alternating voltage to the Piezo speaker
    at an period of 2ms.
*/
void BuzzPiezo() {
  digitalWrite(piezoPin, HIGH);
  delay(1);
  digitalWrite(piezoPin, LOW);
  delay(1);
}
/////////////////////////////////////////
				
			

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

This time in the loop instead of a bunch of code you’ll only see a few lines. This line calls the function BuzzPiezo. Inside of this function is the code from the loop() function from the last program. The program just calls this function over and over again in the loop() if the SW1 is pressed.

				
					void loop() {
  if (digitalRead(SW1) == pressed) {
    BuzzPiezo();
  }
}
				
			
When making a function it is good practice to put a function description right above it in a comment. This will remind you or tell someone else what the function does. Using the “/* */” format here will allow you to easily write a multi-line comment.
				
					/*
  It is good practice to put a function description here.
  Example:
  This function applies alternating voltage to the Piezo speaker
    at an period of 2ms. 
*/
				
			

Remember a function has a few parts. The return type, function name, parameters if it takes any, and the code inside of the brackets. See the table below for a breakdown of the BuzzPiezo function:

Part Name Part
Return Type Void (It does not return anything)
Function Name BuzzPiezo
Parameters None in this case
Code inside brackets
digitalWrite(piezoPin, HIGH);
delay(1);
digitalWrite(piezoPin, LOW);
delay(1);
                
				
					void BuzzPiezo() {
  digitalWrite(piezoPin, HIGH);
  delay(1);
  digitalWrite(piezoPin, LOW);
  delay(1);
}
				
			

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.