Professional Documents
Culture Documents
Robotics m3
Robotics m3
Code/Source
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.
Sketch
-It is saved as a plain text 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 can understand
-Errors should first be fixed before the program starts executing, the process of running or
performing the task.
Syntax
-a set of rules, format, statements, commands, or declaration in a programming language.
-Every line of code in Arduino must end with a semicolon (;) 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
Two Functions of Arduino Sketch:
- void setup()
- void loop()
-The function is the part of a computer code that instructs the computer to perform a specific
task.
-Once the sketch starts, it initially calls the setup function. This only runs once to set up your
program by initializing the variables and values, pin modes, calling libraries, etc.
-The pair of open and close curly brackets { } in the sketch identifies a line of code to be
executed when a function is called.
Comment
-is generally included anywhere in the program, though not required but recommended to write
a description or notes in the program. It will not affect the execution of the program since it
will be ignored by the compiler. This serves as a reminder for the programmer while editing the
code.
Single Line Comment - It starts with a double slash (//) symbol and anything typed within
that line will be ignored by the compiler.
Multiline Comment -It starts with a slash and asterisk (/*) and ends with an asterisk and
slash (*/). Anything in between these symbols will be considered as part of the comment.
Loop() Function
Looping is among the powerful basic concepts in computer programming. It is a programming
function that repeatedly executes a sequence of instructions until a certain condition is
reached. The loop function in Arduino code performs the same way by consecutively looping a
certain line of code inside the function. This function is generally used to control the Arduino
board.
Parameters
pin: specific number of the pin it is connected to the board
MODE: INPUT, OUTPUT
digitalWrite() Function
The digitalWrite() function writes either a HIGH or LOW value to a digital pin. If the pin has
been set up as an OUTPUT with pinMode( ) similar to the previous example, the voltage will be
set to 5 V (or 3.3 V for 3.3 V boards) for HIGH and 0 V (ground) for LOW.
If you do not declare the pinMode, the default mode is "input."
Syntax
Parameters
pin: the pin number
VALUE: HIGH, LOW
delay() Function
The delay( ) function causes the program to pause for a specific period of time in milliseconds
before executing the next line of code. One second is equivalent to 1000 milliseconds.
Syntax
Parameters
ms: the number of milliseconds
to pause (unsigned long)
What is a code or source code in programming?
a) A computer program b) A document containing programmer's notes
c) A hardware component d) A type of code used in music
What is the default digital pin for the built-in LED on an Arduino Uno?
a) Pin 1 b) Pin 5
c) Pin 9 d) Pin 13
What function is used to set up a specific pin as either digital input or digital output?
a) pinMode() b) setPin()
c) digitalPin() d) configurePin()
Which Arduino function is used to make a digital pin output a HIGH voltage?
a) setHigh() b) digitalWrite(HIGH)
c) highVoltage() d) digitalWrite(pin, HIGH)
What is the syntax for setting a pin as an input in Arduino using pinMode()?
a) pinMode(pin, INPUT) b) pinMode(INPUT, pin)
c) setInput(pin) d) setPinMode(pin, INPUT)
Which of the following is not a valid value for the pinMode() function in Arduino?
a) INPUT b) OUTPUT
c) HIGH d) INPUT_PULLUP
Answers:
a) A computer program
c) .ino
b) Translating code into machine-readable language
c) With a semicolon (;)
d) setup()
c) To initialize and set up the program
c) // your comment here
d) /* your comment here */
c) loop()
d) Pin 13
a) pinMode()
c) Writes a HIGH or LOW value to a digital pin
c) 1000
d) delay()
c) delay(milliseconds)
d) digitalWrite(pin, HIGH)
a) pinMode(pin, INPUT)
c) HIGH
c) To provide explanations and notes for programmers
c) Repeatedly executing a sequence of instructions