Close

November 29, 2019

Arduino IDE Quick Programming Guide

STRUCTURE

Every Arduino program (also called sketch) has two required functions.

void setup() {   }

All the code between the two curly brackets will run once when Arduino is powered up. It’s a good place to do setup tasks like setting pin modes or initialising libraries.

void loop() {   }

This function runs after setup is completed. It will run again and again as long as Arduino is powered up.


Syntax

/*    */ 

Everything between /* and */ is ignored by the Arduino IDE when it runs the sketch.

//

This is for single-line comments. They start with // and continue to the end of the line.

;

Every single line of code must end with a semicolon. A missing semicolon is often the reason why a program fails to compile.


VARIABLES

A variable is a place to store a data. It has a type, a name, and a value.

int LEDpin=13;

This code creates a variable of type integer, with the name LEDpin and the value 13. Once specified, this variable will represent the value 13 throughout the entire program (global variable) or entire function (local variable).

int

Integer is the most common datatype for numbers. Its value ranges from –32,768 (or –215) to 32,767 (or 215 – 1). It uses 2 bytes (or 16 bits) of storage.

float

Float is used for a number that has a decimal point. A floating-point number ranges from –3.4028235E+38 to 3.4028235E+38. It uses 4 bytes (or 32 bits) of storage.

char

This is a data type that takes up 1 byte of memory that stores a character value. It’s written in single quotes, for example ‘A’. For multiple characters or strings, use double quotes, for example “ABC”.

boolean

A boolean holds one of two values: true or false.


OPERATOR

Math Operator

The Math operator is used for manipulating numbers, like simple math.

=

Assignment, makes a variable equals a value. For example “x=5+6” makes x equals 11

+

Addition

Subtraction

*

Multiplication

/

Division

Comparison Operator

The Comparison operator tests whether a certain condition has been satisfied.

==

Equal to; Example: “7 == 5” is FALSE while “7 == 7” is TRUE

!=

Not equal to; Example: “7 != 5” is TRUE while “7 != 7” is FALSE

<

Less than; Example: “7 < 5” is FALSE while “5 < 7” is TRUE

>

Greater than

<=

Less than or equal

>=

Greater than or equal


CONTROL

if (condition) {  }

This will execute the codes between the curly brackets if the condition is TRUE.

if (condition) {action a}
else {action b}

This will execute action a if the condition is TRUE; else action b is executed.

for (int x=0; x<100; x++)
{ }

This loop is used to repeat a block of codes a number of times. In this case x is initialized with 0, and will be incremented (can be decremented as well using x–) as long as x<100. As a result, the block of codes within the curly bracket will be repeated 100 times.


FUNCTION

Digital

pinMode (pin, mode);

Configure a pin either in INPUT or OUTPUT mode. “pin” is the pin number from 0 to 19 (digital input/output ranges from 0 to 13 while analog input ranges from 14 to 19).

digitalWrite (pin, value);

Once a pin is configured as an OUTPUT, its value can be set either HIGH (+5V) or LOW (ground).

digitalRead (pin);

Reads the value from a specified INPUT pin, either HIGH or LOW.

Analog

analogWrite (pin, value);

Pin 3, 5, 6, 9, 10, and 11 support PWM (pulse width modulation), a kind of rapid ON and OFF signal that simulates analog signal. At a value of 0, the signal is completely OFF while at a value of 255, the signal is completely ON.

analogRead (pin);

Reads the value from a specified analog INPUT pin. The value ranges from 0 to 1023, corresponding to input voltages between 0 to 5V.

Leave a Reply

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