How to Put Arduino Code into a Function

preview_player
Показать описание
Simplify your Arduino program by putting blocks of code into functions that are called in the loop. This way your Arduino sketch stays more organized and is easier to use.

0:00 How to wrap Arduino code in a function
0:24 Examine existing Arduino Sketch 031 add card
1:17 Cut code to put in function
1:40 Add function name to loop();
2:22 Add new function
2:44 Paste code in function
3:24 Make second function call
3:55 Copy existing code
4:30 Add second new function wrapper
4:49 Paste code into function

How to Create Functions in Arduino

In this Arduino tutorial, I will show you how to create simple functions to organize your code and make it easier to read and maintain.

First, we will create a function to control a servo motor. The function will take two parameters: the pin number of the servo motor and the desired position of the servo motor.

Next, we will create a function to blink an LED. The function will take one parameter: the pin number of the LED.

Finally, we will call the functions from the loop() function.

By creating functions, we can make our code more modular and easier to understand. We can also reuse functions in other projects.

Here are the steps on how to create functions in Arduino:

1. Define the function. The function definition starts with the keyword `void` followed by the name of the function. The name of the function should be descriptive and easy to remember.
2. Define the parameters. The parameters are the values that are passed to the function. The parameters are enclosed in parentheses after the name of the function.
3. Define the body of the function. The body of the function is enclosed in curly braces. The body of the function contains the code that is executed when the function is called.
4. Call the function. The function is called by using the name of the function followed by a pair of parentheses. The values of the parameters are enclosed in the parentheses.

I hope this tutorial has helped you learn how to create functions in Arduino.

#arduino #arduinoproject #electronicarts

Become a member

👍 Support on Patreon

Tip Jar to Support the Channel

Paypal: @pxqx
Venmo: @jimmykuehnle

🟥 Subscribe

Links

🌐 What? Make Art Online

👕🎽Looking for What? Make Art merch?

📷 What? Make Art on Instagram:

🐦 What? Make Art on Twitter:

My Work:

▶️Videos:

#arduino #arduinoproject #code
Рекомендации по теме
Комментарии
Автор

Post any Arduino function questions in the comments

WhatMakeArt
Автор

For big programs it is essential to use functions as most compilers have a limit to how much code you can put in the loop and it really helps readability. And remember document your code!

icarossavvides
Автор

Do you have a part two where you use parameter?

CyFi
Автор

Thank you for posting this informative and educational video which I was able to put to use in my learning Arduino coding. I've combine using your previous "How to Combine Two Arduino Sketches" the "For Loop Iteration" and the "Fading" sketch. And in concert with your How to Remove Delay video, have both sketches working; I thank you for your video guidance. I have a question as to how to Fade the LED 5 times, stop and begin the "For Loop Iteration". I came across this statement: void fade5times(){ for (int i=0; i<5; i=i+1){ } and from what I've read and understand this statement is to be placed in/or just bellow the "VOID LOOP". Any help or direction you could provided would be a great help as I'm a 70 yo neophyte to coding.

salc
Автор

If you wanted to use those custom functions in another sketch, how would you go about that?

trazz