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

Arduino Programming(Upto Variables)

Uploaded by

timtmemory.2025
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Arduino Programming(Upto Variables)

Uploaded by

timtmemory.2025
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Arduino Sketch

Arduino sketch is the name that Arduino uses for a program. It’s the unit
of code that is uploaded to and run on an Arduino board. A basic
Arduino sketch consists of two functions:
setup()
loop()
void is a comment
used to declared
function.
Writing a Sketch

// Hello World Program

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello World");
}

void loop() {
// put your main code here, to run repeatedly:
}
How to Run the Sketch

Follow the steps below to run the sketch:

1. Connect your device to your computer using a USB cable.


2. Click the Upload button to load the program to the Arduino.
3. Now open the Arduino IDE Serial Monitor Window to see the
sketch run and print the text message. The text that the program shows
should be visible in the serial monitor window.
RULES

1. Every Arduino Program has void setup() and void loop()


2. Program runs in linear fashion.(Runs the program line by
line.)
3. Curly brackets {} must be open and close.
4. Program is case sensitive.
5. Add // for comments
6. Put ; after every statement.
The setup() Function

Statements in the setup() function are executed only once when the sketch
is run.

The loop() Function

Statements in the loop() function will run continuously from top to bottom
and then back to the top.

If the loop() function contains two statements, the first statement will be
executed first, then the second statement, then the first statement again
and so on. Hence, the statements in the main loop will be executed
continuously until the Arduino is switched off or reset.
VARIABLES

1. Variable are used to store information to be referenced & manipulated in a


computer program. G

2. Three things are required to declare a variable -

i) Data type
ii) Variable Name
iii) Value.
3. Global variables can be accessed on any function in the program.
4. Local variable are declared inside a function, and can be used only inside that
function.
int val = 10; // Global Variable.
void setup (){
// Put your set up code here, to run once:
int val = 10; // Local Variable.
}

void loop (){

// Put your main code here, to run repeatedly:


}
DATA TYPES

1. bool - A bool holds one of two values, true or false. (Each bool
variable occupies one byte of memory.)

Syntax -
bool var = val;
Parameters -
var: variable name.
val: the value to assign to that variable.
Eg: - bool running = false;
2. Char - A data type used to store a character value. Character literals are written in single quotes,
like this: A (for multiple characters - strings - use double quotes: "ABC").

Syntax -
char var = val;

Parameters -
var: variable name
val: the value to assign to that variable

Example Code -
char myChar = 'A';
char myChar = 65; // both are equivalent
3. float -

Datatype for floating-point numbers, a number that has a decimal


point. Floating-point numbers are often used to approximate analog
and continuous values because they have greater resolution than
integers. Floating-point numbers can be as large as 3.4028235E+38
and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of
information.

Syntax -
float var = val;

Parameters -
var: variable name.
val: the value you assign to that variable.

Example - float abc = 1.17;


4. int -
Integers are your primary data-type for number storage.

Syntax -
int var = val;

Parameters -
var: variable name
val: the value you assign to that variable

Eg: -
int abc = 43;
5. string -
Text strings can be represented in two ways. you can use the String data type, or you can make a
string out of an array of type char and null-terminate it. This page described the latter method. For
more details on the String object, which gives you more functionality at the cost of more memory, see
the String object page.

Syntax -
All of the following are valid declarations for strings.

char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
6. array -
An array is a collection of variables that are accessed with an index number.
Arrays in the C++ programming language Arduino sketches are written in can be
complicated, but using simple arrays is relatively straightforward.
Creating (Declaring) an Array
All of the methods below are valid ways to create (declare) an array.
// Declare an array of a given length without initializing the values:
int myInts[6];
// Declare an array without explicitly choosing a size (the compiler
// counts the elements and creates an array of the appropriate size):
int myPins[] = {2, 4, 8, 3, 6, 4};
Possibilities for declaring strings -
Declare an array of chars without initializing it as in Str1
Declare an array of chars (with one extra char) and the compiler will add the
required null character, as in Str2
Explicitly add the null character, Str3
Initialize with a string constant in quotation marks; the compiler will size the
array to fit the string constant and a terminating null character, Str4
Initialize the array with an explicit size and string constant, Str5
Initialize the array, leaving extra space for a larger string, Str6
Eg: -
char myString[] = "This is the first line"
" this is the second line"
" etcetera";
Arrays of strings -
It is often convenient, when working with large amounts of text, such as a
project with an LCD display, to setup an array of strings. Because strings
themselves are arrays, this is actually an example of a two-dimensional array.
Eg : - char *myStrings[] = {"This is string 1", "This is string 2", "This is string 3",
"This is string 4", "This is string 5", "This is string 6"
};
// Declare an array of a given length and initialize its values:
int mySensVals[5] = {2, 4, -8, 3, 2};
// When declaring an array of type char, you'll need to make it longer
// by one element to hold the required the null termination character:
char message[6] = "hello";
Accessing an Array
Arrays are zero indexed, that is, referring to the array initialization above, the
first element of the array is at index 0, hence
mySensVals[0] == 2, mySensVals[1] == 4, and so forth.

It also means that in an array with ten elements, index nine is the last element.
Hence:

int myArray[10]={9, 3, 2, 4, 3, 2, 7, 8, 9, 11};


// myArray[9] contains 11
// myArray[10] is invalid and contains random information (other memory
address)
To assign a value to an array: -
mySensVals[0] = 10;
To retrieve a value from an array:
x = mySensVals[4];

You might also like