0% found this document useful (0 votes)
33 views18 pages

Robotics Training LNCHS 4

The document provides an overview of basic Arduino programming concepts including: - Variable data types like int, float, boolean and their uses. - Common arithmetic, comparison and logical operators. - The concept of variables, their naming conventions and scopes (global vs local). - Basic program structure with setup() and loop() functions. - Example code to print "Hello World" to the serial monitor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views18 pages

Robotics Training LNCHS 4

The document provides an overview of basic Arduino programming concepts including: - Variable data types like int, float, boolean and their uses. - Common arithmetic, comparison and logical operators. - The concept of variables, their naming conventions and scopes (global vs local). - Basic program structure with setup() and loop() functions. - Example code to print "Hello World" to the serial monitor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

LNCHS

JHS ROBOTICS
Dennis F. Erasmo | 2022
ARDUINO
PROGRAM
STRUCTURE
Remember these:
• { } (curly braces) – also referred to as just “braces” or “curly
brackets” define the beginning and an end of function blocks.

Unbalanced braces can often lead to cryptic, impenetrable compiler


errors that can sometimes be hard to track down in a large program.

• ; (semicolon) – must be used to end an statement and separate


elements of the program.

Forgetting to end a line in a semicolon will result in a compiler error.


/*Comments*/(block comments) – or multi-line comments , are areas of text ignored
by the program and are used for large text descriptions of code or comments that help
others understand parts of the program. They begin with /* and end with */ and can
span multiple lines.

//(line comments) – single line comments begin with // and end with the next line of
code. Like block comments, they are ignored by the program and take no memory
space.

Single line comments are often used after a valid statement to provide more
information about what the statement accomplishes or to provide a future reminder.
digitalWrite() - write a HIGH or LOW value to digital pin.
• If the pin is configured as an OUTPUT with pinMode(), its voltage will be set to the
corresponding value 5v or 3.3v for HIGH and 0v for LOW
• HIGH = 5volts or 3.3volts LOW = 0volts
• Syntax: digitalWrite (pin,value);
• Example: digitalWrite(led,HIGH);

analogWrite() – writes an analog value(PWM wave) to a pin. Can be used to light LED at
different brightness or drive a motor at various speeds.
• Can be used in digital I/O pin numbers 3,5,6,9,10,11.
• Parameters: 0-255
• Syntax: analogWrite(pin,value);
• Example: analogWrite(led,100);
digitalRead() – reads value from a specified digital pin either HIGH or LOW
• HIGH=1 LOW=0
• Can be used in digital pins 1-13
• Syntax: digitalRead(pin);
• Example: value=digitalRead(5);

analogRead() – Reads the value from the specified analog pin. Arduino boards contain a
multichannel, 10-bit analog to digital converter, it will map input voltages between 0-5v or
3.3 v into integer values 0 and 1023
• Can be used in analog pins A0-A5
• Syntax : analogRead(pin);
• Example: value=analogRead(A2);
pinMode() – configures the specified pin to behave either as an INPUT or an OUTPUT
• Must be included in void setup()
• Syntax: pinMode(pin, Mode);
• Example: pinMode(led,OUTPUT);
STRUCTURE

void setup()
{ }

void loop()
{ }

*programs for
Arduino are
usually referred
to as “sketch”
• The setup() function is called when a sketch starts. Use it to initialize the variables, pin
modes, start using libraries etc. The setup function will only run once, after each power
up or reset of the Arduino Board. Code that needs to only run once (such as to set up
the board for your application) should be placed in the setup() function.
• The code to be run continuously after initial setup has finished goes into the loop()
function.

• When the board finishes uploading the code, or is turned on once it contains this
code, it starts at the top of the sketch and carries out the instruction sequentially. It
runs the code in setup() once and goes through the code in loop(). When it goes to the
end of the loop (marked by the closing bracket } ), it goes back to the beginning of the
loop.
Data Types
Numeric Types Bytes Range Use
int 2 -32768 to 32767 Represents positive and negative integer values.

unsigned int 2 0 to 65535 Represents only positive values; otherwise, similar to int.

long 4 -2147483648 to Represents a very large range of positive and negative values.
2147483647
Represents a very large range of positive values
unsigned long 4 4294967295
Represents numbers with fractions; use to approximate
float 4 3.4028235E+38 to realworld
-3.4028235E+38 measurements.
Data Types
Numeric Types Bytes Range Use
double 4 Same as float In Arduino, double is just another name for float.

boolean 1 false (0) or true Represents true and false values.


(1)

char 1 -128 to 127 Represents a single character. Can also represent a signed value
between -128 and 127.

byte 1 0 to 255 Similar to char, but for unsigned values.

Other types

string - Represents arrays of chars (characters) typically used to contain text.

void - Used only in function declarations where no value is returned.


ARITHMETIC OPERATORS

Assume Variable A holds 10, and Variable B holds 20

Operator Name Symbol Description Example

Assignment operator = Stores the value to the A=B


right of the equal sign in
the variable to the left of
the equal sign

Addition + Adds two operands A+B will give 30

Subtraction - Subtracts second operand A-B will give -10


from the first

Multiplication * Multiply both operands A*B will give 200

Division / Divide numerator by B/A will give 2


denominator

Modulo % Operator and remainder B%A will give 0


after an integer division
COMPARISON OPERATORS

Assume Variable A holds 10, and Variable B holds 20

Operator Name Symbol Description Example

Equal to == Checks if the value of two operands is equal or not, if (A==B) is not true
yes, then the condition becomes true.

Not equal to != Checks if the value of two operands is equal or not, (A!=B) is true
If values are not equal, then the condition becomes
true

Less than < Less than the value of right operand, if yes, the (A<B) is true
condition becomes true

Greater than > Greater than the value of right operand, if yes, the (A>B) is not true
condition becomes true

Less than or equal to <= Checks if the value of left operand is less than or equal (A<=B) is true
to the value of right operand, if yes, the condition
becomes true

Greater than or equal to >= Checks if the value of left operand is greater than or (A>=B) is not true
equal to the value of right operand, if yes, the condition
becomes true
What is a Variable?

A variable is a place to store a piece of data. It has a name, a value, and a type. For example, this
statement (called a declaration):
int pin = 13;
• creates a variable whose name is pin ,
• whose value is 13 ,
• and whose type is int .

Note: a variable must be named according to its function.

Example:
int ledpin=13; //it means that the LED pin is connected to pin number 13 in the arduino board

Int tempValue=0; //value of temperature


Variable Scope

Global Variables – are defined outside of all the functions, usually at the top of the
program. The global variables will hold their value throughout the life-time of your
program.
A global variable can be accessed by any function. That is, a global variable is
available for use throughout your entire program after its declaration.
Example of a Global Variable
int ledpin=2; //global variable “ledpin”  global variables are usually declared on the top of the program

void setup()
{
pinMode(ledpin,OUTPUT);
}

void loop()
{
digitalWrite(ledpin,HIGH);
delay(1000);
digitalWrite(ledpin,LOW);
delay(1000);
}
Local Variables – are declared inside a function or block. They can be used only by the
statements that are inside that function or block of code. Local Variables are not
known to function outside their own.
Example of a local variable
int sensor=A1; // global variable “sensor”

void setup()
{
Serial.begin(9600);
pinMode(sensor,INPUT);
}

void loop()
{
int sensorValue = analogRead(sensor); //local variable “sensorValue” declared inside a block of code
Serial.println(sensorValue);
}
Example 1: Very BASIC Program
HELLO WORLD!

void setup()
{
Serial.begin(9600); //for you to be able to use the Serial Monitor
}

void loop()
{
Serial.println(“HELLO WORLD”); //will be displayed in the serial monitor
delay(1000);
}

You might also like