Arduino Programming(Upto Variables)
Arduino Programming(Upto Variables)
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
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
Statements in the setup() function are executed only once when the sketch
is run.
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
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.
}
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 -
Syntax -
float var = val;
Parameters -
var: variable name.
val: the value you assign to that variable.
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: