Program structure:
Every Arduino program has two main functions. Functions are parts of a computer program that run
specific commands. Functions have unique names, and are “called” when needed. The necessary
functions in an Arduino program are called setup() and loop().
The setup ():
The setup runs once, when the Arduino is first powered .This is where you configure the digital pins.
The loop ():
The loop runs continuously afer the setup() has completed. The loop() is where you’ll check for voltage on
the inputs, and turn outputs on and of.
The variables:
Variables are names you give to places in the Arduino’s memory so you can keep track of what is
happening. These values can change depending on your program’s instructions. Variable names should
be descriptive of whatever value they are
storing. For example, a variable named switchState tells you what it stores: the state of a switch. On the
other hand, a variable named “x” doesn’t tell you much about what it stores
Create a variable: To create a variable, you need to declare what type it is. The
data type int will hold a whole number (also called an integer); that’s any number without a decimal point.
When you declare a variable, you usually give it an initial value as well. The declaration of the variable as
every statement must end with a semicolon(;)
The constants:
Constants are similar to variables in that they allow you to uniquely name things in the program, but unlike
variables they cannot change.
Comments:
If you ever want to include natural language in your program, you can leave
a comment. Comments are notes you leave for yourself that the computer ignores. To write a comment,
add two slashes // The computer will ignore anything on the line afer those slashes.
Pin configuration:
The Arduino’s digital pins can act as both inputs and outputs. In your code, you’ll
configure them depending on what you want their function to be. When the pins
are outputs, you can turn on components like LEDs. If you configure the pins as
inputs, you can check if a switch is being pressed or not. Since pins 0 and 1 are used
for communicating with the computer, it’s best to start with pin 2.
Command digitalWrite:
The Arduino’s digital pins can read only two states: when there is voltage on an
input pin, and when there’s not. This kind of input is normally called digital (orsometimes binary, for two-
states). These states are commonly referred to asHIGH and LOW. HIGH is the same as saying “there’s
voltage here!” and LOW means“there’s no voltage on this pin!”. When you turn an OUTPUT pin HIGH
using a command called digitalWrite(), you’re turning it on. Measure the voltage between the pin and
ground, you’ll get 5 volts. When you turn an OUTPUT pin LOW, you’re turning it of.
Time function:
The delay() function lets you stop the Arduino from executing anything for a period of time. delay() takes
an argument that determines the number of milliseconds before it
executes the next set of code. There are 1000 milliseconds in one second. delay(250) will pause for a
quarter second.