0% found this document useful (0 votes)
14 views

Arduino Programming Guide

Uploaded by

Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Arduino Programming Guide

Uploaded by

Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Arduino Programming Guide

Introduction
This guide covers fundamental concepts and functions in Arduino programming, including
variables, control structures, functions, libraries, and common built-in functions.

1. void setup() and void loop()


In Arduino, every program (called a sketch) must have two main functions:
• void setup(): This function runs once when the program starts. It is used to initialize
variables, pin modes, and start communication with other devices.
• void loop(): This function runs repeatedly after the setup() function completes. The code
inside loop() will execute in a continuous cycle.

Example:
```cpp
void setup() {
pinMode(13, OUTPUT); // Set pin 13 as an output
}

void loop() {
digitalWrite(13, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
}
```

2. pinMode(), digitalWrite(), and delay()


• pinMode(pin, mode): Configures the specified pin to behave either as an input or output.
• digitalWrite(pin, value): Sets the output value of a specified digital pin to HIGH or LOW.
• delay(ms): Pauses the program for the amount of time (in milliseconds) specified as a
parameter.

Example:
```cpp
void setup() {
pinMode(8, OUTPUT); // Set pin 8 as an output
}
void loop() {
digitalWrite(8, HIGH); // Turn on the LED
delay(500); // Wait for 500 ms
digitalWrite(8, LOW); // Turn off the LED
delay(500); // Wait for 500 ms
}
```

3. Variables
In Arduino, variables store data values that can be used later in the program. Here are some
common variable types:
• int: Represents integer numbers.
• float: Represents numbers with decimal points.
• boolean: Represents true or false.
• byte: Represents an 8-bit number (0-255).
• char: Represents a character.

Example:
```cpp
int number = 10;
float temperature = 36.5;
boolean isOn = true;
byte pinNumber = 13;
char letter = 'A';
```

4. Global vs Local Variables


• Global variables: Defined outside any function, accessible from any function in the
program.
• Local variables: Defined inside a function, accessible only within that function.

Example:
```cpp
int globalVar = 5; // Global variable

void setup() {
int localVar = 10; // Local variable
}

void loop() {
// Can access globalVar here, but not localVar
}
```

5. Comparators and Logical Operators


• Comparators: Used to compare values. Examples: ==, !=, <, >, <=, >=.
• Logical operators: Used to combine multiple conditions. Examples: && (AND), || (OR), !
(NOT).

Example:
```cpp
int a = 10;
int b = 20;
if (a < b && b > 15) {
// Code executes if both conditions are true
}
```

6. Control Structures
Control structures like if, else, and else if are used to perform different actions based on
different conditions.

Example:
```cpp
int temperature = 25;
if (temperature > 30) {
// Code for high temperature
} else if (temperature < 15) {
// Code for low temperature
} else {
// Code for normal temperature
}
```

You might also like