I Can Store and Retrieve Variables

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

I can store and retrieve

variables
Warm Up

1. Transistor has ___________ pn junctions


2. Void means ____________,therefore void setup() returns________ value.
3. In “Serial.begin(9600);” 9600 is ____________________.
4. Bare minimum function must at least have _________________ and
____________
5. A Arduino code set is called as _________________
Store and Retrieve Values

Variables are names you can create for storing, retrieving, and using values in
the Arduino microcontroller’s memory. Here are three example variable
declarations from the next sketch:
int a = 42;
char c = 'm';
float root2 = sqrt(2.0);
 The declaration int a = 42 creates a variable
named a. The int part tells the Arduino software
what type of variable it’s dealing with. The int type can
store integer values ranging from -32,768 to 32,767. The
declaration also assigns a an initial value of 42. (The initial
value is optional, you could instead just declare int a, and
then later assign the value 42 to a with a = 42.)
 Next, char c = 'm' declares a variable named c of the
type char (which is for storing characters) and then assigns
it the value 'm'.
 Then, float root2 = sqrt(2.0) declares a variable
named root2. The variable type is float, which can hold
decimal values. Here, root2 is initialized to the floating-
point representation of the square root of two: sqrt(2.0).
// Sketch for Store and Retrieve Local variables

void setup()
{
Serial.begin(9600);
int a = 42;
char c = 'm’;
float root2 = sqrt(2.0);
Serial.println(a);
Serial.println(c);
Serial.println(root2);
}
void loop()
{
// Empty, no repeating code.
}
Sketch to see that 'm' really is 109
void setup()
{
Serial.begin(9600);
byte c = 'm’;
Serial.println(c);
}
void loop()
{
// Empty, no repeating code.
}
Global vs. Local Variables
 So far, we’ve declared variables inside a function block (inside the function’s
curly braces), which means they are local variables. Only the function
declaring a local variable can see or modify it. Also, a local variable only
exists while the function that declares it is using it. After that, it gets
returned to unallocated memory so that another function (like loop) could
use that memory for a different local variable.
 If your sketch has to give more than one function access to a variable’s value,
you can use global variables. To make a variable global, just declare it
outside of any function, preferably before the setup function. Then, all
functions in the sketch will be able to modify or retrieve its value. The next
example sketch declares global variables and assigns values to them from
within a function.
// Sketch for Store and Retrieve Local variables
int a;
char c;
float root2;

void setup()
{
Serial.begin(9600);
a = 42;
c = 'm';
root2 = sqrt(2.0);
}
void loop()
{
Serial.println(a);
Serial.println(c);
Serial.println(root2);
delay(1000);
}
Modify your sketch to find out the
decimal ASCII codes for your name.
Printing your name:

void setup() { Serial.write(value1);


Serial.begin(9600); Serial.write(value2);
Serial.write(value3);
byte value1 = 82; Serial.write(value4);
byte value2 = 101; Serial.write(value5);
byte value3 = 115; Serial.write(value6);
byte value4 = 104; Serial.write(value7);
byte value5 = 109; Serial.write(value8);
byte value6 = 97; Serial.write(value9);
byte value7 = 32; Serial.write(value10);
byte value8 = 82; Serial.write(value11);
byte value9 = 97; Serial.write(value12);
byte value10 = 106; Serial.write(value13);
byte value11= 103; Serial.write(value14);
byte value12 = 111;
byte value13 = 108; }
byte value14= 105; void loop()
Serial.print("My name is: "); {

}
Homework:

 There are lots more data types than just int, char, float, and byte.
 Open the Arduino Language Reference, and check out the Data Types list.
https://www.arduino.cc/reference/en/
 Follow the float link and learn more about this data type.
 The long data type will be used in a later sections; open both
the long and int sections. How are they similar? How are they different?

You might also like