There are many things to keep in mind when coding such as the syntax, functions, and keywords. It can be difficult to keep track of all these things when getting started, so here’s an Arduino programming cheat sheet to keep track of the basics!
This is the void setup function that runs once the Arduino microcontroller first boots up.
This is function that runs after the void setup has finished. It will continue to run over and over until the Arduino has been powered off.
These double forward slashes create a single line comment. This is great for adding comments throughout your code to describe what each line does. Everything until the end of the line will not count as an actual part of the program
/* */ (multi-line comment)
If you have many lines that you would like to comment, you can use multiline comment. All text in between these symbols will be ignored from the program just line the single line comment.
These curly bracket symbols are used to mark where a block of code begins and ends. This is used in functions and loops.
Just as you mark the end of a sentence with a period, every line of code must end with a semicolon. A semicolon that is missing will not allow the code to compile so it’s important they’re placed properly!
These are used to keep track of numbers in our program. They can be created by writing the data type followed by the name of the variable.
Note: Variable names cannot begin with a number.
One of the most popular variable data types. They are used to represent numbers. Integers can only store whole numbers and not decimals. They are 2 byte values and can store a number from -32,768 to 32,767.
Another data type used to store numbers. When an integer isn’t big enough, long variables are used as they are 4 byte values that can store a number from -2,147,483,648 to 2,147,483,647.
A simple data type that holds one of two values: true or false.
A useful data type as it can hold decimal numbers. Floating-point values can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 4 bytes of information.
These variables are used to store single character values. These are declared in single quotes.
We can manipulate numbers in our program by perform the following math operations.
This single equal sign is the assignment operator. It is used to store the value of the right side of the equal sign to whatever variable is on the left side of the equal side.
This subtracts two numbers.
This operation multiplies two numbers.
This divides two numbers.
In our program we can used certain statements to control the flow of execution. Here’s a few useful statements to keep in mind when programming.
This is called an “if statement”. If statements will execute the code that’s in between the curly brackets if the condition in the parenthesis is true. When the “if” statement condition is false, then the “else if” code will trigger, but when that is also false, the “else” code will trigger.
This is a function that loops a certain piece of code as many times as specified. It can count up using “i++” or count down using “i–“.
Digital Pins Input/Output
A function that sets a pin’s mode. It takes two parameters: pin and mode. Pin is the number of the pin to be used, and mode is set to INPUT or OUTPUT. On an Arduino microcontroller, pins 2-12 can easily be used as digital pins.
When a digital pin is set to OUTPUT, then it can output a HIGH or LOW value. the digitalWrite() function takes in the “pin” number and “value” parameter which is either HIGH (which outputs +5 volts) or LOW (which pulls it to ground, or outputs 0 volts).
When a digital pin is set as an INPUT, you can read the voltage entering it whether it is HIGH (+5 volts) or LOW (0 volts).
Sometimes we want to deal with accurate values gained from analog sensors. This can’t be done with digital functions, so we have useful functions available to help us when working with analog.
This function produces pulse width modulation which allows a voltage output between +0 volts and +5 volts. This function takes the pin number and value between 0 and 255.
Reads the value of an analog pin, the value received can be from 0 to 1024.