KEMBAR78
Lesson-4-Arduino-Programming-dsBasics.pdf
WHAT IS A PROGRAM
 is a written programming language which enables a computer to
perform its function. Therefore, a program is a written instruction that
process input, manipulate data, and output a result.
Arduino uses C / C++ Programming language. C was created by
Dennis Ritchie between 1969 -1973 and in 1979, Bjarne Stroustrup
created C with classes known as C++.
Both programming languages are still popular in programming
microcontrollers. In programming, we use a combination of letters,
words, and symbols, which are known as Tokens. All written codes will
go through a compiler to convert it to machine language.
Transducers
A Transducer is a device that converts energy from one form to
another. These devices are often used for automation,
measurement, and control system.
Sensors - are transducers that receive signals or stimulus from an
environment or physical system. Converts stimulus into electrical
signal.
Actuators - are transducers that actuate or move something -
converting electrical signal into actions.
Types of Signal
Analog - It can take up any number of values
or readings (Ex. Temperature)
Digital - It is represented by 2 values: either High
or Low or On or Off
Programming Syntax
Syntax refers to a set of rules used to determine if a
certain group of words forms a valid sentence - which is
also true in computer programming.
Arduino follows the C/C++ language. These two
programming languages use the same syntax.
void setup{ }
Void setup is technically a function that you create at the
top of each program. Inside the curly brackets is the
code that you want to run one time as soon as the
program starts running. You set things like pinMode in this
section.
void loop { }
The void loop is a function that Arduino integrates as a part of its
structure. Every command that is inside the loop will run
continuously as long as the microcontroller is turned on.
; (semicolon)
Signifies an end on the command line. Since the compiler does
not read spaces in the program, it looks for semicolons to read
every end of the statements.
{ } curly braces
A group of code statements. When troubleshooting your
program, always check first the curly braces if all opening curly
braces have partner closing curly braces.
() parentheses
A group of arguments for a function, a method, or a code
statement. Since programming languages follow PEMDAS
(Parenthesis, Equation, Multiplication, Division, Addition,
Subtraction), you can also use it to group math within equations.
Ex. random( (5-2), (480 - 220));With the example above, the result
will be: random(3,260);
// single line comment
When you type two forward slashes, the compiler ignores the
code from that point until the end of the line. Use comments to
clarify what the code does, to remember how the hardware is
connected, or to delete code from the software without
removing it entirely.
/* multi-line comment */
A multi-line comment needs to have /* as its opening and */ as
its closing. All characters between the symbols will be
considered as a comment. When you have a lot to type, use a
multi-line comment to make it easier to group the information
together.
Variables
A variable can store data from a sensor or results in a calculation.
These variables can then be called for later use in the program.
Using and Naming a Variable
Creating a Variable; To create a variable in C/C++. We must follow
these basic steps:
Declaration - before a variable is used, we need to declare it first to
inform the compiler about the data type and size. The syntax in
declaring a variable is simple:
Type the data type - for example, "int" for integer type of data.
Followed by the identifier or the name of the variable
In naming a variable, we must be mindful of the following:
Its name must start with a letter.
It may contain letters, numbers, and the underscore character.
It should not be a reserved word in the Arduino IDE.
It should not contain white spaces.
 Initialization of Variables - after declaring the variable, its value is set to default unless we assign
value to it. It is also possible to place value on the variable from the moment that it is declared.
 We created 1 variable to store pin number of the LED that we will use for the activity – led_1
Programming a circuit
Void setup is technically a function that you create at the top of
each program. Every code that is inside the curly braces will run
once as soon as the program starts running.
The pinMode command is usually placed here so that the
microcontroller reads the pin assignment first before running
other commands.
The setup() runs once when the Arduino is first powered on.
 The pinMode() function is used to configure a specific pin to behave either as an input or an output.
 Since the variable led1 is initialized with the value 2, we are configuring pin 2 as an Output.
 Note: We can directly put pin 2 to the setup without having a variable for it. It is also beneficial if we
are saving Arduino's memory.
 Void loop is another function that Arduino uses as part of its structure. The code inside the loop
function runs over and over if the Maker Board is turned on.
digitalWrite();
digitalWrite() is the command that allows you to send 5V or 0V
to an output pin. It takes two arguments: what pin to control,
and what value to set that pin, HIGH or LOW.
delay();
The delay() function lets you stop the Arduino from executing
anything for a period of time. delay() takes an argument that
determines the number of milliseconds before it executes the
next set of code. There are 1000 milliseconds in one second,
delay (250) will pause for a quarter of a second.
Lesson-4-Arduino-Programming-dsBasics.pdf

Lesson-4-Arduino-Programming-dsBasics.pdf

  • 2.
    WHAT IS APROGRAM  is a written programming language which enables a computer to perform its function. Therefore, a program is a written instruction that process input, manipulate data, and output a result. Arduino uses C / C++ Programming language. C was created by Dennis Ritchie between 1969 -1973 and in 1979, Bjarne Stroustrup created C with classes known as C++. Both programming languages are still popular in programming microcontrollers. In programming, we use a combination of letters, words, and symbols, which are known as Tokens. All written codes will go through a compiler to convert it to machine language.
  • 3.
    Transducers A Transducer isa device that converts energy from one form to another. These devices are often used for automation, measurement, and control system. Sensors - are transducers that receive signals or stimulus from an environment or physical system. Converts stimulus into electrical signal. Actuators - are transducers that actuate or move something - converting electrical signal into actions.
  • 4.
    Types of Signal Analog- It can take up any number of values or readings (Ex. Temperature) Digital - It is represented by 2 values: either High or Low or On or Off
  • 5.
    Programming Syntax Syntax refersto a set of rules used to determine if a certain group of words forms a valid sentence - which is also true in computer programming. Arduino follows the C/C++ language. These two programming languages use the same syntax. void setup{ } Void setup is technically a function that you create at the top of each program. Inside the curly brackets is the code that you want to run one time as soon as the program starts running. You set things like pinMode in this section.
  • 6.
    void loop {} The void loop is a function that Arduino integrates as a part of its structure. Every command that is inside the loop will run continuously as long as the microcontroller is turned on.
  • 7.
    ; (semicolon) Signifies anend on the command line. Since the compiler does not read spaces in the program, it looks for semicolons to read every end of the statements.
  • 8.
    { } curlybraces A group of code statements. When troubleshooting your program, always check first the curly braces if all opening curly braces have partner closing curly braces. () parentheses A group of arguments for a function, a method, or a code statement. Since programming languages follow PEMDAS (Parenthesis, Equation, Multiplication, Division, Addition, Subtraction), you can also use it to group math within equations. Ex. random( (5-2), (480 - 220));With the example above, the result will be: random(3,260);
  • 10.
    // single linecomment When you type two forward slashes, the compiler ignores the code from that point until the end of the line. Use comments to clarify what the code does, to remember how the hardware is connected, or to delete code from the software without removing it entirely. /* multi-line comment */ A multi-line comment needs to have /* as its opening and */ as its closing. All characters between the symbols will be considered as a comment. When you have a lot to type, use a multi-line comment to make it easier to group the information together.
  • 12.
    Variables A variable canstore data from a sensor or results in a calculation. These variables can then be called for later use in the program.
  • 14.
    Using and Naminga Variable Creating a Variable; To create a variable in C/C++. We must follow these basic steps: Declaration - before a variable is used, we need to declare it first to inform the compiler about the data type and size. The syntax in declaring a variable is simple: Type the data type - for example, "int" for integer type of data. Followed by the identifier or the name of the variable In naming a variable, we must be mindful of the following: Its name must start with a letter. It may contain letters, numbers, and the underscore character. It should not be a reserved word in the Arduino IDE. It should not contain white spaces.
  • 15.
     Initialization ofVariables - after declaring the variable, its value is set to default unless we assign value to it. It is also possible to place value on the variable from the moment that it is declared.  We created 1 variable to store pin number of the LED that we will use for the activity – led_1
  • 16.
    Programming a circuit Voidsetup is technically a function that you create at the top of each program. Every code that is inside the curly braces will run once as soon as the program starts running. The pinMode command is usually placed here so that the microcontroller reads the pin assignment first before running other commands. The setup() runs once when the Arduino is first powered on.
  • 17.
     The pinMode()function is used to configure a specific pin to behave either as an input or an output.  Since the variable led1 is initialized with the value 2, we are configuring pin 2 as an Output.  Note: We can directly put pin 2 to the setup without having a variable for it. It is also beneficial if we are saving Arduino's memory.  Void loop is another function that Arduino uses as part of its structure. The code inside the loop function runs over and over if the Maker Board is turned on.
  • 18.
    digitalWrite(); digitalWrite() is thecommand that allows you to send 5V or 0V to an output pin. It takes two arguments: what pin to control, and what value to set that pin, HIGH or LOW.
  • 19.
    delay(); The delay() functionlets you stop the Arduino from executing anything for a period of time. delay() takes an argument that determines the number of milliseconds before it executes the next set of code. There are 1000 milliseconds in one second, delay (250) will pause for a quarter of a second.