Getting Back Up To Speed With Arduino

Have you ever wanted to code something cool and new with your Arduino but its been a few months and getting back up to speed with coding your Arduino seems like a bit of a monumental task? If so, this blog is for you (and me). Truth is, I have found myself here many times. Now, instead of cracking out my favorite books about programming Arduino, I am writing this blog so I can quickly get back up and running with a cool Arduino development board.

The Official Arduino Reference

First of all, there is the official Arduino Reference. IT describes every function, variable, and structure available in the standard Arduino. Don’t skip this reference if you have not already looked through the Arduino reference.

Also be sure to check out my favorite books and other resources for learning Arduino. Let’s get started by covering some of the most common tags and then we will go into some example code.

#include

If you are using some peripheral like an OLED display or a sensor, you will likely need to pull in a library that allows you to easily interact with the extra device or sensor. Libraries can be downloaded using the library manager found at tools > manage libraries in your menu bar on a Mac or PC. If a library asks to download dependencies, you generally want to download everything the library is asking for.

A few of my favorite libraries are:

You will find libraries that work for you by using boards and chips commonly used for Arduino and looking through some example code.

To find where your libraries are stored, go to arduino > preferences then check the sketchbook location. This is the path where all of your arduino libraries and sketches are found. Also, after you install a new library, you might need to restart your IDE (the arduino code editor) to be able to start using the new library.

const vs #define

Constants are often needed at the beginning of a sketch. Constants are used for hooking up the pins needed to hook up peripherals (such as LEDs, buttons, and signals). The const is Constant and will not be allowed to change anywhere in your code. It is constant – not a variable, after all.

Using the const tag is considered superior to the use of #define because #define is replaced by the text of the variable at compile-time and can lead to problems.

int

int stands for integer. These are your primary data-type for number storage.

pinMode

pinMode configures the specified pin to behave either as an input or an output.

digitalWrite

digitalWrite is used for two main tasks. Setting an output pin to HIGH or LOW

  • Setting an output pin to HIGH or LOW
  • Setting the internal pullup resistor

Serial.begin

Serial.begin, Serial.print, Serial.println

Global, Local, and Static Variables use in Functions

  • Global Variables – Defined at the top of the code
  • Local Variables – Defined inside a function
  • Static Variables – Defined inside a function, only defined the first time it is ran (Keeps variable from being reset every time it is ran)

Global Variables are defined before void setup() These are variables that can be used anywhere but be careful. The use of global variables all over your code can make your code confusing and hard to debug. Global variables also go against the principle of encapsulation – the idea that you should wrap everything that you need in a feature inside a function like a nice little package that is reusable. Using the principle of encapsulation also helps you write DRY code – Meaning Don’t Repeat Yourself. Don’t repeat code over and over, rather call a function that performs a task efficiently and in a way that is easy to debug.

Local variables are declared inside a function and only can be used within the scope of that function. The local variable will not be available for use in other parts of your code.

Passing Variables to Functions

Declaring Local Variables When Passing Parameters To Functions

Call A Function, Pass A Perimeter, and Return A Result

It is possible to call a function, pass it a perimeter and set a variable equal to the result of that function. void function name() is used

Calling Functions

In the void loop function, you can call other functions, like this: functionName

  • functionName();
  • functionName(valueToBePassed); – Passes variable to function
  • int variableName = functionName(valueToBe Passed); – Passes value to function and returns a value to variableName

Running Functions

After your void loop function you can create functions that preform some specific task in your code. It is best practice to call a function when needed rather than running all the time in your void loop. Rather call the fucntion and run it when needed. Here are ways you can write out the function name for specific scenarios.

The void means nothing is returned by the function. The int means something is returned by the function.

  • void functionName() – used when function does not return anything and no perimeters are passed to the function
  • void functionName(int valueThatIsPassed) – Creates local variable to be used in the function called int valueThatIsPassed. valueThatIsPassed set to value passed from void loop.
  • int functionName(int valueThatIsPassed) – Creates local variable to be used in the function called int valueThatIsPassed. valueThatIsPassed set to value passed from void loop. Function returns a value to int variableName in void loop

Here is an example of a fucntion being called in void loop and being passed a value. Then that function runs. The function returns a value to a variable in void loop.

Debouncing A Button

It seems every project I do involves a button, and if you can’t debounce your buttons it is likely you are going to have whacky problems with your code. So, debouncing a button is pretty important.

Below is an example from Adafruit’s article about debouncing. This article is very good and worth a read. Their article goes into more hardware options for debouncing, such as using a 0.1 uF capacitor across the switch contacts.

Debouncing a button is something that seems like it should be easy, but it takes a few steps to make the debounce reliable.

if else

The arduino reference has a good guide for if statements. One thing to be sure of is to use one of the Comparison Operators instead of an Assignment Operator. Meaning use x == 10 rather than x = 10. The former will evaluate if x is equal to 10. The later will set x to 10 and always evaluate as TRUE. Not the desired result when using an ‘if’ statement.

For

The for statement has some tricky looking syntax. But if you just accept it for what it does and move on with your life it is much easier to use. Again the arduino reference is good to check out.

The for statement will:

  • Repeat a block of statements enclosed in curly braces
  • Increment a counter
  • Once the counter reaches a condition, the for statement stops repeating and the arduino moves on to the next block of code.

While

A while() loop will continously and infinitely loop – until the expression inside the parenthesis () becomes FALSE. This means that something inside the while loop must change the condition being tested, otherwise the while loop will never exit. Usually this is done by incrementing a value or testing an external condition such as a sensor or a button press.

If you are testing the condition of a sensor. Usually a digitalRead(inputPin) = variable will be placed inside the while loop. The condition will be set to that variable.

Do…While

The Do…While is similar to the while keyword. But, in the Do…While keyword, the condition is tested at the end of the loop so the do loop will always run at least once before the condition is checked.

I have more to learn about Do…While. I am going to fix up the first part of the blog then get back to this. I hope this blog has been helpful – please comment below if you see anything that needs to be updated!

Leave a Comment

Your email address will not be published. Required fields are marked *