Worksheet 3 Quarter 3 Robotics and Electronics
Worksheet 3 Quarter 3 Robotics and Electronics
DEPARTMENT OF EDUCATION
Region I
SCHOOLS DIVISION OFFICE
DAGUPAN CITY NATIONAL HIGH SCHOOL
Dagupan City
Worksheet
in
Robotics
and
Electronics
Basic Arduino Sketch
(Worksheet 3, Quarter 3)
Francisco C. Dacoco
Master Teacher I
Getting Started
A code or source code is a computer program or set of instructions written by a programmer to enable the
computer or electronic device to perform its task. The coding structures and concepts of Arduino is similar to other
common computer programming languages like Java or C++. So, a background in the fundamentals of computer
programming will make learning to program an Arduino very fast. But for the sake of those who are not acquainted in
programming, this module will help you learn the basic concept of programming Arduino.
1
Study Time
Basic Arduino Sketch
Arduino Ide is like a text editor that allows you to write your code. The file generated or
saved using the Arduino IDE is called a sketch. It is saved as a plaintext file with the extension
name .ino. The written code is in human-readable format, which means data presented can be
naturally and easily read by humans. Arduino IDE also allows you to compile the code. Compiling
refers to the process of translating the code written in a programming language into machine-
readable language, a language that a computer ca understand. If there is any error during the
compiling of the program, an error message will be displayed in the Status Bar, which is located at
the bottom of the IDE. Errors should first be fixed before the program starts executing, the pro-
cess of running or performing the task.
Arduino code is case sensitive, which means small letters (lowercase) are distinguished
from capital letters (uppercase) and typing the wrong case will result to a syntax error. Like other
languages, Arduino code should conform with the correct syntax– a set of rules, format,
statements, commands, or declaration in a programming language.
It is also important to remember that almost every line of code in Arduino must end with a
semicolon (;) just like other programming languages. It is sometimes referred to as program
terminator and indicates the end of the statement in a program. Failure to end in this manner can
also cause a syntax error.
setup() FUNCTION
Upon launching the Arduino IDE, you will be prompted with the default sketch. The Arduino
sketch is divided into two functions: the void setup() and void loop(). The function (otherwise
known as a procedure or sub-routine) is a named piece of code that can be used from elsewhere
in a sketch. It is the part of a computer code that instructs the computer to perform a specific
task. For example, here's the definition of the setup() function from the Blink example:
Void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
The first line provides information about the function, like its name, "setup". The text before
and after the name specify its return type and parameters: these will be explained later. The code
between the { and } is called the body of the function: what the function does.
You can call a function that's already been defined (either in your sketch or as part of
the Arduino language). For example, the line pinMode(ledPin, OUTPUT); calls the pinMode
() function, passing it two parameters: ledPin and OUTPUT. These parameters are used by
the pinMode() function to decide which pin and mode to set.
2
Comments
Syntax
pinMode(LED_BUILTIN, OUTPUT);
Parameters
3
The digitalWrite() functions outputs a value on a pin. Writes either a HIGH or LOW value to a digi-
tal pin. If the pin has been set up as an OUTPUT with pinMode(), the voltage will be set to 5 V (or
3.3 V for 3.3 boards) for HIGH and ) V (ground) for LOW. For example, the line:
Syntax
digitalWrite(ledPin, HIGH);
Parameters:
set the ledPin (pin 13) to HIGH, or 5 volts. Writing a LOW to pin connects it to ground, or 0 volts.
The delay() causes the Arduino to wait for the specified number of milliseconds before continuing
on to the next line. There are 1000 milliseconds in a second, so the line:
delay(1000);
4
Activity Time Activity 1: Identify me!
1. A set of instructions that enable the computer or electronic device to perform its task.
3. Process of translating the code written in a programming language into machine language.
4. Set of rules on how letters and characters are formed to represent a command/code.
5. Function that prepares the pin to be used in the circuit and sketch.
8. Function that enables the microcontroller to wait until it process the next command/code.
10. Ignored by the compiler but is included in the code to serve as a note or reminder to the programmer while editing
the code.
Performance Task
Create a sketch that will control the LED in the Arduino microcontroller connected to digital pin 13. The LED will
light up for half a second and will turn off for half a second. This sequence will only be done for the first three seconds
when the program is uploaded.
After three seconds, the LED will change the on-off pattern; light up for one second and turn off for one second.
This pattern will continuously executed.
5
Rubrics: Basic Arduino Sketch
Light Emit- For the first For the first For the first The LED light up The LED on pin
ting Diodes three seconds, three seconds, three seconds, and turns off 13 does not
the light the light the light after three sec- light up and
emitting diode emitting diode emitting diode ond or more. turns off.
lights up and lights up and lights up and
turns off for turns off for turns off but
half a second. half a second; not with the
The LED’s on- but turns on-off specified time
off pattern every second allotment.
changes to one without inter-
second continu- val.
ously.
Time allot- The prototype The prototype The prototype The prototype The prototype
ment was submitted was submitted was submitted was submitted was submitted
on time. one meeting two meetings three meetings more than
late. late. late. three meetings
late.
Total Points
Note: for simulations of your output you may visit the Tinkercard or watch
https://www.youtube.com/watch?v=yyG0koj9nNY&list=PLV6cmKvnKRs5geApVORPW79U6s3wpa0Ht