• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Maker Hacks

Maker Hacks

Ideas, news & tutorials for makers and hackers – Arduino/Raspberry Pi, 3D printing, robotics, laser cutting, and more

  • Home
  • About
  • 3D Printing
  • Laser Cutting
  • YouTube
  • Free Arduino Course
  • Recommendations
  • Contact

Day 5: Lesson 01 – Arduino/C Functions and Serial Input

You are here: Home / 30 Days to Arduino Programming Course / 30 Days to Arduino Lessons / Day 5: Lesson 01 – Arduino/C Functions and Serial Input

A critical part of learning any language is the ability to break apart your program into “subroutines”.

In C, and therefore Arduino, we use Functions.

We have already used a few – Setup and Loop are functions, and PrintLn is a function (method) of the Serial object.

Creating Arduino/C Functions

Functions are built in the form:

ReturnType functionName(InputType variableName)
{
 // do stuff
 return "data";
}
Eg. 
String hey(String name)
{
 Serial.println("Hey " + name);
 return "Done!";
}

Don’t Repeat Yourself

One of the best habits to get into when coding is to create small, reusable elements, rather than rely on copy and paste.

It’s easier to understand when reading back (for others, as well as yourself at a later date), and easier to debug.

As a rule of thumb, each function should exist for just one purpose.

Serial Input Demonstration

To demonstrate how functions work, let’s create a sketch that asks the user to input something into the serial monitor input box.

Did you notice the serial monitor allows input as well as showing output?

Serial Monitor Input Box

Serial Input Code

  • 3D Printing
  • CNC
  • Hardware
  • Laser Cutting
  • News
  • Retro Computing
  • Software

amiga amos arduino basic programming budget buying cad cnc commodore corexy c programming creality crypto doctor who emulation esp32 esp8266 filament fpga glowforge Hacks Ideas kids lighting linux Makes making Monoprice Parts pc props prusa python raspberry pi ratrig resin Reviews robots software sounds technology tips upgrades wanhao xtool

.

Maker Hacks Blog Copyright © 2023 · All Rights Reserved · Powered by Mai Theme