Jul 05

Sketch 4

Copy and paste everything below the /****/ into a blank sketch.

/*****************************/
//Sketch_4
/* This program goes with Lab 4 of the OpenTag laser tag tutorial.
It plays a basic game of free for all laser tag. A player is out
if they get tagged 5 times.
*/

#include
//TimerOne will make it easy to pule an infrared LED 38,000 times
//a second

// Variables (nicknames for pins)
int button_pin = 12;
int led_pin = 11;
int ir_led_pin = 10;
int ir_receiver_pin = 9;

//you start with 5 health. This is the first time that we will use
//a variable to keep track of and change a number.
int health = 5;

void setup() {
//first, start up the TimerOne library. We want it to pulse
//38,000 times a second, which is about once every 26 milliseconds
Timer1.initialize(26);

//tell the arduino if a pin is input/output
pinMode( button_pin, INPUT);
pinMode(led_pin, OUTPUT);
pinMode(ir_led_pin, OUTPUT);
pinMode(ir_receiver_pin, INPUT);
}

void loop() {
int button_state = digitalRead( button_pin ); //read button
if (button_state == HIGH) { //if the button is pushed
/* If we are pushing the button, then we are trying to send
a tag. However, we can only tag if we have more than 0 health.
We can use the if statement and a comparative function called
greather than, which uses the > symbol. Greather than will
be true (the code in the if statement will be run) if the
number on the left is greather than the number on the right.
We want to send a tag if health is greater than 0. In code,
that is written as:
*/
if (health > 0){ //if we have health
//so, we know that we are pushing the button and have health
//now we can send a tag.
//first, pulse the infrared LED with the TimerOne code
Timer1.pwm(ir_led_pin, 512); //send pulse
delay(10); //delay for 10 milliseconds
Timer1.disablePwm(ir_led_pin); //turn off Timer One
}
}

//now, we want to see if we are getting tagged
int ir_receiver_state = digitalRead(ir_receiver_pin);
//if the receiver sees something, it will send a LOW or 0 signal
if (ir_receiver_state == 0){
//when we are tagged, we lose 1 health.
/* In order to tell the arduino to subtract 1 health, we
need to tell it what health should now be. To do this, we
will say that the health I should have is equal to the health
that I had minus 1. That line of code is written below.
*/
health = health - 1;
//now that we see something, indicate it by turning the LED
//on for 1 second
digitalWrite(led_pin, HIGH);
delay(1000); //a delay of 1000 is 1 second
digitalWrite(led_pin, LOW); //then turn LED off
}
}

//Tia tia tia!

Jul 05

Lesson 4

Now that we have an arduino that can both send and receive signals using infrared light, we can program it to play a basic game of laser tag.

Basic Laser Tag

We will start with a simple game of elimination. Each player has a tag unit, and the objective is to tag the other players 5 times. If you get tagged 5 times, you are out. The person who is left when everyone else is out is the winner.

So, how do we program the arduino to do this? We are nearly there. We already have code that sends in infrared signal (light pulsed 38,000 times a second) that our infrared receiver can receive. We also have code that checks to see if our receiver is receiving anything. What we need to do is add health. If you are out of health, then you can not send tags. Every time you receive a tag, your health goes down by one. You start with 5 health.

That's all there is to building a simple game of laser tag. From here, we are going to start adding bells and whistles to make the game more fun to play. But after you have completed this lesson and lab, you can play laser tag. Go for it. Write down what you liked and didn't like. If you want to change something, you can do it. This is your game of laser tag, and you can build it to do whatever you want. But first, let's make the code. To the code!

Jul 04

Sketch 3

This is the code for Sketch 3. Copy and paste everything below the /*************/ into a new arduino sketch.

/************************************************/
//Sketch_3
/* This program goes with Lab 3 of the OpenTag laser tag tutorial.
It sends a pulsed IR signal (38,000 times a second) when a button
is pressed and turns on an LED if a signal is seen
*/

/*
first, let's add the TimerOne library. To add a library, you
type #include and then the library name inside <>
*/

#include
//TimerOne will make it easy to pule an infrared LED 38,000 times
//a second

// Variables (nicknames for pins)
int button_pin = 12;
int led_pin = 11;
int ir_led_pin = 10;
int ir_receiver_pin = 9;

void setup() {
//first, start up the TimerOne library. We want it to pulse
//38,000 times a second, which is about once every 26 milliseconds
Timer1.initialize(26);

//tell the arduino if a pin is input/output
pinMode( button_pin, INPUT);
pinMode(led_pin, OUTPUT);
pinMode(ir_led_pin, OUTPUT);
pinMode(ir_receiver_pin, INPUT);
}

void loop() {
int button_state = digitalRead( button_pin ); //read button
if (button_state == HIGH) {
/*so, this is the first change. What we want to do is have
the arduino turn on the infrared LED on and off at 38,000
times a second.
we already know that digitalWrite is the command to tell the
arduino to set a pin to HIGH or LOW voltage. We want to
do that 38,000 times a second. That means we need to
turn the LED off then on about every 0.026 seconds.
(1/38,000 = 0.02632)
We could use digitialWrite to turn the infrared LED on and off,
or, we could use the TimerOne code to turn the infrared LED
on and off at precisely the time we want. We just need to tell
the arduino how long to pulse the LED. For that, we use the
delay function. The delay function tells the arduino to pause
for a set number of microseconds, or 1/1000th's of a second.
For our code, we will have the infrared LED pulse for 10
milliseconds every time we push the button
*/
//first, pulse the infrared LED with the TimerOne code
Timer1.pwm(ir_led_pin, 512); //send pulse
delay(10); //delay for 10 milliseconds
Timer1.disablePwm(ir_led_pin); //turn off Timer One
}
/*we don't need to do anything if the button is low
else {
digitalWrite(led_pin, LOW);
}
*/
//now, we want to see if we are getting tagged
int ir_receiver_state = digitalRead(ir_receiver_pin);
//if the receiver sees something, it will send a LOW or 0 signal
if (ir_receiver_state == 0){
//now that we see something, indicate it by turning the LED
//on for 1 second
digitalWrite(led_pin, HIGH);
delay(1000); //a delay of 1000 is 1 second
digitalWrite(led_pin, LOW); //then turn LED off
}
}
//Mj csy ger xyvr sr er PIH, csy ger hs ercxlmrk!

Jun 23

Lab 3

We are going to add an infrared (IR) LED and an infrared receiver to the arduino. We already know how to add an LED, and we have chosen to put it on pin 10. The new part is the infrared receiver. This outputs whether or not it sees an infrared signal turning on and off 38,000 times a second. We need to give this sensor power, ground, and connect its output wire to our arduino so that our arduino can sense the signal (high or low voltage) that the infrared sensor gives as an output. Below is a circuit diagram of what we need to add.

Figure 1: Picture of connections. (Click on the picture to make it bigger).

Lab3_bb
Note: I couldn't find an IR sensor in the software I use to draw circuits, so my IR sensor doesn't look exaclty like an IR sensor.

So, now that we have a red LED, infrared LED, infrared sensor and button, how do we want to program them? So, for laser tag, we want to push a button and send a signal out of our infrared LED (to send a tag). When we receive a tag on our infrared receiver, we want to indicate that we have been tagged with our red LED. So, we need to modify our code to follow those instructions.

We are going to use other people's code to make building laser tag easier. We call other people's code libraries. It is sort of like checking out a book at the library, only you are checking out other people's code and using it in your own code.

The first library that we are going to use will turn on and off our infrared LED 38,000 times a second. That library is called TimerOne. You can download the library here (note, download the file TimerOne-r11.zip for the latest version) and read about how it works with the TimerOne Tutorial (the tutorial on how it works is very advanced. I can go over it later if you would like. You do not need to know the details on how it works to use it properly).

Once you have the file downloaded, you need to tell the arduino programming software where the library is so that you can use it in your code. The arduino software has a folder where it stores all of the libraries it knows about. To move the library to a place where the arduino software can find it, you can follow the tutorials from either the arduino library installation or the sparkfun library tutorial. Once you have the TimerOne library installed, it's time to modify the code to make a rudimentary laser tag unit. To the code!

Jun 22

Lesson 3

We are going to use infrared light (the same kind TV remote controls use) to communicate between two arduinos so that they can play laser tag. This lesson goes over how to do that.

Now that you can get your arduino to respond to a button press, the next thing to do is make it send a signal using infrared light when you push a button. We need an emitter (an infrared LED to create the light) and a receiver to sense the light. We already know how to light up an LED, and if we use an LED that emits Infrared light, then the emitter is done.

So, if we wanted in infrared receiver, how would we build one? We could make one that turns on when it sees infrared light(remember, the arduino only communicates with voltage. When the receiver sends a signal saying that it sees something, it will do that by changing the voltage on one of its pins). However, there are a lot of sources of infrared light naturally. The sun, your body, fire - anything that gives off heat also emits infrared light. So, how can we make sure that the infrared light that our detector sees is coming from our infrared LED?

Well, the people who made the receiver made it only respond to infrared light that blinks. Really fast. For the receiver we will use, whenever the receiver gets hit with 38,000 pulses of light per second, then the receiver will say that it is seeing our infrared LED (and one of its pins will change from high to low voltage). Since our arduino can make the LED blink really fast (to create an output for the receiver), and our arduino can read high or low voltage from the receiver, our arduino can use the infrared LED and receiver to communicate. So voila! A way to take an infrared signal and make it control the arduino.

So, now that we have an idea of what we want to do, how do we do it? We're going to need to write a piece of code that pulses an LED on and off 38,000 times a second. We will also have to have the arduino check the infrared receiver to see if the infrared receiver is receiving the pulsed infrared light. This is the basics of communicating between arduinos. So let's build it. To the Lab!

Bonus Knowledge

The infrared reciever we will use is active low. That means when it activates [sees a infrared signal pulsing 38,000 times a second], it makes its output pin low. Why does it make the pin low and not high when it senses a signal? That has to do with power consumption, industry standards, etc. We won't go into that. If you really want to know, ask me. I'll be glad to try to answer.

There is a lovely tutorial at adafruit that goes into how to use IR (infrared) sensors, which you can find here if you want more information. For now, it's on to the circuit diagram and the code. (well commented, of course!).

Aug 13

Key Qualities for a school Makerspace and Bareduino

I came across and article on Make about the key qualities that are needed in a makerspace for kids in school. The main point of the article was that people need a few things to make. First, they need the basic skills of what tool to use and how to use it. Then they need to problem solve, to determine the reason that the thing they are making isn't working. Finally, they need the confidence to try something new. To learn something as they go. That confidence is what I want to impart on people. I want to empower them, to make them realize that they can build something complex, like laser tag. To be able to feel like they can do it and that it is within their reach to understand, to build, to create. The original article is here.

In other news, Niek Blankers at NiekProductions posted schematics for a bareduino, an arduino compatible based off of the ATiny45/85 that uses a small circuit board and minimal support components. It may be something that i could use if I wanted to have a separate controller that looked for tags, say on someone's head. His post is here.

Apr 23

A little Rant

I came across an article about an arduino class taught by a 14 year old in Sydney, Australia. I am pleased to see young people teaching others about technology. I mean, I'm pleased whenever anyone promotes science and technology irregardless of age. Although, I am still not satisfied with how things currently work.

For example, this 14-year-old boy was able to learn how to build robots using an arduino, build a web-server, create systems using raspberry pi. He learned for free through the internet. And that's great. I would have loved to have had something like that. But being able to learn a hodgepodge of technical knowledge on the internet doesn't make me happy. Having a multitude of arduino tutorials available to me does not give me a path to learn why an arduino works. I can learn how to build a robot. Someone has already written code to communicate with a radio transceiver and I can blindly put his or her library into my code and use it, but no one is there to tell me how it all works. Why it works. How electrons magically make a servo turn and stop at five degrees. Why it does that. What the control signals are that make that happen. How it uses interrupts to send the signal every 20ms, and what an interrupt is. Where is that tutorial?

Because I couldn't find it. When I was starting off learning about the arduino, there wasn't a path for me to take that could have shown me what I could do with electronics. Sure, there are plenty of websites that have lessons on electronics, arduino, etc. There's a series of books that teach electricity and circuits. There are the tutorials  on the adafruit learning center, a series of about 50 tutorials about lots of stuff you can do with arduino, lots of random tutorials on the arduino website, lists of tutorials on instructables, the list goes on. Google arduino tutorial and there will be lots of hits. They all have instructions to teach you how to use the arduino to solve a particular problem. You want to spin a motor? No problem. There's a tutorial that will tell you how to do it.

Great, right? There's a bunch of information out there about learning the basics of electronics. We're good. Wrong. There are a lot of wonderful tutorials to teach people specific things, but there isn't a formal path to take someone from beginner to someone who understands how an arduino works. There is no one to teach people what is under the hood of an arduino, to spark their interest in the world inside the ATMega328p, no mention of how a microprocessor, compiler or register works. We are still failing to teach the more important answer to the quest to knowledge. Why?

I don't want to just teach people how to blink an LED. I don't want to just teach people how to build a robot or laser tag or a mailbox that tweets when the mail comes in. If that's all you want to know, you can search online, find someone who's already done it and copy what they've done without learning why it works. Which is a great starting point, but that is not where the journey should end. There is so much more. So much left undiscovered by building something just to make it work.

I've never been satisfied with a black box, not knowing why I put one thing into it and it spits out another. I want to look inside the black box of technology, unpack its innards and understand what is really going on. And now that I've been lucky enough to study under professors who taught me, I know what I've been yearning to know since I was in high school. And it makes me angry that there still isn't a resource that I could've used to start me down this path earlier. To formalize my internet education from a random walk through youtube videos into a structured series of lessons that teach why an arduino works. That reveal what is going on in that little black chip.

Teaching someone how to do something is fine. But teaching them why is the key to understanding. There's nothing teaching people why an arduino works that is freely available and in a place where people who are looking for it can find it. There is no place to learn why an arduino works here on the internet that people can start at and learn from. So I'm going to try to change that. Even if I fall flat on my face, I will still be making forward progress. I hope to positively change a single life. If I can do that, then I will have succeeded.

Apr 19

Lab 2

In order to send a light signal (which is how the arduino will communicate when playing laser tag), we need to program the arduino to turn on a light when you push a button. In order to do this, you need to learn how to control the arduino's inputs, outputs and the basics of programming in the arduino IDE.

After reading through the example sketch (Sketch 2), you should have an idea of how to write a program to turn on an LED when a button is pressed. The circuit that you need to build to light up an LED when a button is pressed is shown below.

Figure 1: Breadboard of Lab 2 circuit (click on picture to make it bigger)

Lab2_bb
Xohd voccyx, dro grsvo vyyz!

The arduino runs off of five volts, and the LED needs 0.01 amperes (10 mA) of current. The circuit above makes pin 12 high (5V) when the button is pressed, and when the button is not pressed, the resistor pulls the pin to ground. This is called a pull-down resistor, as it makes the pin grounded (it is 0 volts) when the button is not pressed. This keeps static electricity from building up on the pin and causing the arduino to think that the pin is high when it shouldn't be. (in general, any pin that you are using should always be connected to either 5V or 0V [ground], so that static or charge from another source dos not build up on the pin).

The button does not need to have much current running through it, and to save power and use standard components, we choose a current value of 0.0005 Amperes (0.5 mA) through the pull-down resistor. Without the resistor, you will be connecting power (5V) to ground (0V), and create a short. A short will try to pull as much current as it can (it has roughly zero resistance, so the current will be very high), which will cause the batteries to quickly die and the components to heat up (and potentially fry). Remember the pull down resistor.

You now have enough information to calculate the resistance values for the two resistors in the circuit. After building the circuit and testing it with the arduino code that was provided, try adding another LED onto pin 10 and having it turn on when the button is not pressed, and turn off when the button is pressed.

Bonus Knowledge
The pin numbers that we are using were chosen to make the schematic look nice. Any of the digital pins can be used for this sketch.

Apr 18

Lesson 2

In order to tell the arduino how to play laser tag, we need to understand how to give it instructions on what to do. We do this using our computer and a program that allows us to write instructions for the arduino. The purpose of this lesson is to introduce people to the arduino and its integrated development environment (IDE) that is used to program it.

To begin with, you need to tell your computer how to talk to the arduino. To do this, you need to get the correct software running on your computer. Fortunately, there is a tutorial on the arduino website that goes through the steps required to set this up, which you can find here. In addition to the setup, the arduino website has many reference pages and forums that are useful for finding answers to questions and solutions to problems.

So, what can an arduino do? An arduino can interact with the outside world using electricity through its pins (the little pieces of metal sticking out of the microcontroller). It can measure the voltage that is on specific pins and output voltages on other pins. This can be used to read sensors, move a motor, turn on a light, tweet, send an e-mail, or do pretty much anything that you can have done by turning on a switch, or pushing a button. But in order to communicate to the arduino without breaking it, there are some things that we need to be aware of.

First, the arduino can only supply 0.03 Amperes (30 mA) of current from any pin. That means that if whatever circuit is attached to a pin tries to draw more than 0.03 Amperes (30 mA) of current, the arduino will eventually overheat and break. It could break that pin or the entire board. This is why you learned Ohm's law. Every circuit that is turned on by the arduino can not draw more than 0.03 Amperes (30 mA) of current, or it will damage the arduino. In addition, the arduino uses a specific voltage, five volts. We will be using the arduino's digital inputs. Digital, or discreet inputs, are like counting numbers. They can only take on certain values, like 1, 2, 3, 4, etc.
For the arduino, it can only count to one. When it reads a voltage, the voltage is read as either 0 or 1. Since the arduino likes using five volts, we will give it a five volt signal. The arduino will think that any signal that it reads on one of its pins that is five volts is a one (or that the voltage is set to high). If the arduino does not sense five volts, it will think that the pin is a 0 (or that the voltage is set to low).

So, now that we know the basics of electricity, we need to learn the basics of programming the arduino. In the arduino IDE (programming environment), a file is called a sketch, and I wrote an example sketch called first sketch to go over the basics of the arduino IDE. Read it. It goes over basic coding practices and tips for writing code.

After reading the example sketch, which you can find here, you can complete Lab 1.
Now go build something.

Note: The brains of the arduino is a microchip called the ATmega328p. Atmel, the company that produces the ATmega328p has a naming convention for their chips. It starts with AT, which stands for the company Atmel. This chip is part of the mega family, which as 32 or more pins, has 32 kilobytes of program memory (which is why 32 is in the name), it has 8 kilobytes of flash memory (which is why there's an 8 at the end), and it is the low power chip (p). Put all of those features together, and you get the ATmega328p. This naming convention lets you compare chips simply by looking at their names. For more information, the Atmel naming convention for these chips is here.

Apr 18

Sketch for Lesson 2 and Lab 2

Copy and paste everything below the /******/ into a blank sketch file (a blank file that opens up in the arduino software package) in the arduino IDE and read through the file to learn about the basics of writing a program for your arduino to run.

/************************************************************/

// this is a comment. A comment is not code that the arduino will try to run.
// a comment is a note to the programmer.
// a comment that is on one line starts with two slashes: //

/* a comment that spans multiple
lines starts with /* and ends with */

// You should always leave yourself comments that explain what you are doing
// When you don't, you'll forget what the code does and have trouble
// coming back to it later and changing stuff.

/*
When you know what you are doing, then you don't need to leave as detailed
comments, but until that point, good comments are key to good code
*/

/*
Speaking of good comments, notice how much easier this comment is to read
than the last one because there is one space before I started writing.
making your comments easy to read and understand is good and worth the time.
Don't make your comments so long that people have to make the arduino window bigger just so that they can see them because this is annoying for people who don't want to expand their window just so they can read the comment that you put in your code but decided not to press enter any time soon because you just had to write it all on one line
*/

/*
Normally, you would start a file with the following:
Name and Purpose of the file
Whether or not it is in the public domain (useful to know if other people
want to use your code).

For Example:
Sketch_2 is meant to introduce people to the arduino integrative
development environment (IDE) that is used to program the arduino
microcontroller. It goes over comments, intputs/outputs and if/else statements
This code is in the public domain.
*/

/*
The arduino has many pins that can be used to receive information from
the world (it can receive input) or send information out (output).
The arduino's pins can be used for either input or output, so you need to tell
the arduino which function that
you want the pin to perform. For this lesson and lab, we will be re-creating
turning on an LED with a button. To do this, we need to tell the arduino
which pins to use and whether we want those pins to be inputs or outputs.
The arduino's pins are labeled with numbers, but that's not easy to remember.
When programming, you can create nicknames (called variables) to name things
so that they that are easier to remember. For example, if we want to put our LED onto
pin 11 on the arduino board, we may want to create a nickname (variable) for that pin
and call it led_pin, so that we can use the word led_pin in our code instead of
the number 11. That line of code is shown below. It
creates a variable (an int or integer, which will be described later)
called led_pin and sets it equal to 13. the ; tells the arduino code that it is
the end of the line of code.
Note: nicknames (variables) can't have spaces. You can use underscores (_)
though, to separate your words and make them easier to read
*/
int led_pin = 11;

/*
We also want to give a nickname for the pin that we attach to
the button. Why? Because we want to see if the button is pressed and
turn on the LED if the button is pressed, so we need another nickname
to keep track of the pin that the button is attached to.
*/
int button_pin = 12;

/*
Now that we have nicknames that we can remember, we can start coding.
Arduino sketches always have two things:
1. a setup routine
2. a loop routine.
The setup routine is run
every time the processor is turned on or reset. This is where you set
pins to be inputs or outputs or do things that you want to happen every time
you start or reset the arduino. After that comes the loop routine, which
runs whatever code is in the loop over and over and over until the arduino
turns off.
*/

/*
the setup routine runs once when you press reset and when the arduino
turns on for the first time:
it looks like this:
void setup() {
Here are what that means:
1. void means that the routine does not send anything back
after it is done running. You can have the arduino return or send something
after it is done running some code, but this code doesn't require that.
2. setup is the name of the routine. A routine or grouping of code is called a
function, which we will explore more later
3. The two parenthesis () mean that there is no input to setup. Other functions
can have inputs, which you would put between the parenthesis (we don't need
and input for setup, and never will)
4. Finally, the curly bracket, { tells the arduino that all the code
starting here is for the setup function.
*/

void setup() {

/*
So, we want to have our led pin be an output. Fortunately, the arduino IDE
has a lot of built in functions that make it easy to do things. For example,
the pinMode function lets you tell the arduino to change a pin to an input
or output. You can set a pin to an input or ourput by writing the command
pinMode( pin to set, OUTPUT or INPUT);
You'll notice that output is written in all capitals.
That is because OUTPUT has a special meaning in arduino sketches, and when
the code is compiled from code that you can read (for example, the word
pinMode) into code that the arduino actually runs (it only understands 1s
and 0s, voltage high and voltage low), the compiler (the software that
converts human readable code into machine readable code) will replace
all instances of the word OUTPUT with the correct value so that the arduino
knows to make the pin an OUTPUT. This is similar to how we created a
nickname for the pin that we attached our LED and button to.
*/
pinMode(led_pin, OUTPUT);
//now we need to set the button pin to be an input
pinMode( button_pin, INPUT);
// we now have our pins set, so we can write our code.

} //this curly bracket ends the setup routine.
// curly brackets tell the program that all the code you write in between the
// brackets belongs to a specific routine or function.

// the loop routine runs over and over again forever:
void loop() {
/*
Now that we have set everything up, we can tell the arduino what to do.
But first we need to have a clear idea of what we want it to do.
We want the arduino to see whether or not a button is pressed and turn on
an LED only if it is pressed. Written another way, if the button is pressed,
we want to turn the LED on, otherwise we will turn it off.
Fortunately, there is a command called if that checks to see if something
is on or off. Since the arduino only understands voltage, it thinks that
HIGH voltage (5 volts) is on and LOW voltage (0 Volts) is off. Remember
that the arduino can't count to 5. If it sees a HIGH voltage signal, it
will count that as a 1.
*/

/*
Now that we know that we want to use an if statement to check to see if the
button is pressed, we are going to make another variable that keeps track of
the button so that we can see if that variable is high or low (1 or 0).
The circuit is shown in Lesson 1, and the button_pin will be high (1) if
the button is pressed and low (0) if the button is not pressed. We want to
read the voltage of the button pin, and to do that arduino has a command
called digitalRead. This reads in the value of a pin as either high (1) or
low (0). It is called digitalRead because it will read in only a digital
value, which is either 1 or 0. It will only output 1 or 0, nothing in between.
If the voltage value is not 0V or 5V, it will round to 1 or 0 depending on
how close the voltage is to 0 or 5. digitalRead works by writing
digitalRead( pin to read voltage from);
*/
int button_state = digitalRead( button_pin );
/*
Note: In later code, all of our variables that we want to use throughout the
sketch (global variables) will be defined before we call the setup routine.
This is so that every time we run the loop, we don't make another
button_state variable. I'm doing it this way so that you can see what is
going on.
*/

/*
Now we want to see if the button is high or low. To do that, we want to
check to see if the button_state variable is equal to 1 or 0. To compare
two values, we can use two equals signs (==). One equals sign is used to
set a value, for example button_state = 0. Two equals signs tells the
arduino to compare the two values and see if they are equal. If they are,
then the statement is true or 1. If not, the statement is false or 0. This
is how we can use the if statement, by checking to see if a certain condition
is true.
Note: after a function or an if statement, you should indent your code to make
it easier to know where that code fits in the grand scheme of things.
*/
if (button_state == HIGH) {
// turn LED on
digitalWrite(led_pin, HIGH);
}
/*
But what if the button is not pressed? Then we want to execute a different
piece of code. After an if (or series of if statements), you can write an
else statement that will run if the if code did not run. So, we want
to turn the LED off if the button is not pressed. To do that, we use the
else command (which doesn't have a condition to check, and uses curly
brackets to tell the arduino what code to execute.
*/
else {
//turn LED off
digitalWrite(led_pin, LOW);
}

/*
That's it. The arduino will now constantly check the the voltage on the
button pin and turn the LED on or off depending on whether the button is
pressed or not.
*/

} // this curly bracket ends the loop. When the arduino runs the code and gets
// to this point, it will start the loop over.

/*
Here is the complete code without my comments. Does it make sense?
int led_pin = 11;
int button_pin = 12;

void setup() {
pinMode(led_pin, OUTPUT);
pinMode( button_pin, INPUT);
}

void loop() {
int button_state = digitalRead( button_pin );
if (button_state == HIGH) {
digitalWrite(led_pin, HIGH);
}
else {
digitalWrite(led_pin, LOW);
}
*/

// Hmh csy jmkyvi syx xli Giewev gmtliv?