Robot Magic: Beginner Robotics for the Maker and Magician 19
error instead, go back and make sure everything is
copied exactly from the text and try again.
UNDERSTANDING THE CODE
Now that your sketch is compiled, lets break down
the code for you to understand.
Any light grey text you see in your sketch is a “comment,
which is there for your reference only and will be ignored
by the Arduino board. Comments are a chance to make
notes in between the code. I use comments to help me
remember what a certain line of code means without
affecting the code itself. Single line comments can be
created by typing // at the beginning of a line. Everything
after that will turn to light grey. You can use /* and */
to create a multi-line comment.
Next in the code, you’ll see int led = 13; — this refers
to pin 13 on the board, and in this sketch, pin 13 has been
named led. The Arduino UNO has a built-in LED right on
the board that we can use! This is great for testing code
without plugging in additional components.
Next comes void setup(). Within the void setup()
{ } brackets, we place code that will run once, only at
startup. That means it runs once when we either plus
in the Arduino or when we press the reset button on the
board. The setup is like an introduction before the full
routine.
Within the brackets of void setup() in this sketch, we
have one line of code that says pinMode(led, OUTPUT);
— here we are simply telling the Arduino that the pin
named led is going to output electrical current. In other
Make_RobotMagic_Interior_FIN.indd 19Make_RobotMagic_Interior_FIN.indd 19 9/14/21 3:33 PM9/14/21 3:33 PM
Chapter 2: Getting Started with Arduino
20
words, with power, the LED will turn on. We have to set
these things up first in setup, so when the void loop()
code begins, the Arduino knows which pins to use.
// the loop below runs over and over again
void loop() {
digitalWrite(led, HIGH); // turn the LED on
//(HIGH is the voltage
// level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by
// making the voltage LOW
delay(1000); // wait for a second
}
Next, we see void loop(). All code within the
curly brackets will be repeated continuously forever,
as long as the Arduino has power. The first line is
digitalWrite(led, HIGH); — this tells the board
to turn the pin named led on. As we saw in setup, the
pin named led is connected to number 13 on the board,
so the board gives power to pin 13 when the command
digitalWrite(led, HIGH); is written. The next
command is delay(1000); — delay means to wait, and
1000 means 1000 milliseconds, the equivalent of one
second. You can change the number inside the delay to
the amount you need. If you want the LED to stay on for
five seconds, for example, write delay(5000); in
the code. 13 seconds would be delay(13000);
— but what if we want it to stay on for just half a
second? Well, that would be delay(500);
Make_RobotMagic_Interior_FIN.indd 20Make_RobotMagic_Interior_FIN.indd 20 9/14/21 3:33 PM9/14/21 3:33 PM
Robot Magic: Beginner Robotics for the Maker and Magician 21
Now, if the only commands inside void loop were
digitalWrite(led, HIGH); and delay(500); the
LED would turn on and never turn off. That’s why we have
digitalWrite(led, LOW); in the next line. This tells the
board to turn the LED off.
The next command — delay(1000); — creates another
wait time of one second. And, after the last command, the
code jumps back to the top of the loop and repeats. So, if
you look at the code, you can see the LED will: turn on, wait
for one full second, turn off, wait for another full second,
and repeat.
UPLOADING THE CODE TO YOUR
BOARD & BLINKING YOUR LED
Lets get this uploaded and prove that it works! On your
Arduino UNO board, there is a USB port. This takes a USB
2.0 Type A/B, the same type of USB cord generally used for
household paper printers. This will connect your board to
your computer.
Make_RobotMagic_Interior_FIN.indd 21Make_RobotMagic_Interior_FIN.indd 21 9/14/21 3:33 PM9/14/21 3:33 PM
Chapter 2: Getting Started with Arduino
22
Next, we have to tell the Arduino software that there
is a board plugged in. In the software, click Tools >
Board and select Arduino UNO. Then, click Tools > Port
and select the port that has Arduino listed. Its usually
the last one on the list. After both board and port are
properly selected, we can upload our code by clicking
the circle button with the arrow pointing right. Thats the
upload button. If everything has lined up correctly, your
sketch will say “done uploading.” Look at your board!
Is the light blinking on and off steadily? You did it!
Now, play with what you have created! Change the code!
Try shortening or extending the delay time numbers,
and upload again, so you can start to get a feel for the
control you have. The more you play, the more you’ll
get comfortable and familiar with the technology. The
goal is for the technology to be second nature, so the
later work of adapting robotic movements and actions
to comedic timing is easy. As you start to develop
your own routines, you’ll need to be comfortable with
going back and forth between uploading to the board,
testing, and then going back and tweaking your code.
MORE ABOUT THE BOARD
Lets look at the board again. I’m not going to break down
every single element of the Arduino board here, but there
are a few things I do want to point out.
The Arduino UNO has 13 digital output pins. These pins
are used to activate various components like your relays,
LEDs, and servos. Thats 13 opportunities all on one board.
As you progress with this technology, you may find yourself
using almost all of these pins at once on a single project!
Make_RobotMagic_Interior_FIN.indd 22Make_RobotMagic_Interior_FIN.indd 22 9/14/21 3:33 PM9/14/21 3:33 PM
Robot Magic: Beginner Robotics for the Maker and Magician 23
Imagine the possibilities!
The pins labeled A0-A5 are analog pins. Those are
used mostly as inputs to read sensors. You can declare
an analog pin as a digital pin, too.
You have a barrel jack to power your board with an
external power supply or 9V battery.
You’ll see pins labeled GND, which stands for ground.
Everything that is plugged into an Arduino data pin (like
an LED), must have its other end plugged into GND to
complete the circuit.
The Vin pin is useful as an alternative way to power the
Arduino board, particularly helpful if you don’t have a 9V
plug for the barrel jack. It stands for Voltage In.
Make_RobotMagic_Interior_FIN.indd 23Make_RobotMagic_Interior_FIN.indd 23 9/14/21 3:33 PM9/14/21 3:33 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.140