Introduction to Arduino Programming 2: Push my Button

Introduction to Arduino Programming

A series based on New York University’s Introduction to Computer Programming class from the Courant Institute of Mathematical Sciences.

This is intended as a “gentle” introduction to computer programming and Arduino. The only prerequisite is that you’ve completed our Arduino Basics guide!

Push my Button(s)

Press Any Key to Continue

We’re going to read the value of an input bit (in this case a button) and display it on the serial monitor of the Arduino IDE and that value will tell us if we’re pressing a button or not.

The serial monitor will let us display our values just like how we displayed “hello littleWorld” in the previous tutorial (click-through to see animation):

We’ll also be picking up the following skills:
– Learning about the basics of computer programming.
– Setting pin modes.
– Reading input values.
– Setting output values.

Equipping your coding utility belt.

bat dance

As we start writing more complex programs with our Arduino we’ll need to expand our coding vocabulary. Here are some terms you’ll need in your
programming utility belt:

Types

“What type of person doesn’t like pizza?” 🍕

We’ve seen them, but haven’t really talked about types. It’s the keyword that’s in front of our setup() and loop() functions, void.


VOID is a kind of type, think of types like different states of matter. You have a liquid types (milkshakes) and solid types (pizza 🍕), in computer programming you have things like numbers and strings. The number types are, well, numbers. The most common numbers we will deal with are integers, whole numbers that can be negative or positive (up to a point).

STRINGS are a “sequence of characters”, for example: “abc” is a string of three characters. Strung together like this!

They are usually surrounded by double quotes (bunny ears if you will):

“I LOVE PIZZA”;

“Chill out, it’s just an expression!”

We’ve already been using these left and right! Expressions are evaluated by the Arduino and can be functions, strings (text), numbers, or mathematical operations like 2 + 2. We’ve seen it in our own code like the:


inside of Serial. println();.
(Note the quotation marks!)

“Let’s not get into an argument”

argv

We’ve also been doing this all day long and it’s not as antagonistic as it sounds. Arguments are just expressions that are passed into functions. Say what?! Let’s look at our good old buddy Serial.begin() again.

The expression 9600 is being passed into Serial.begin(). In this case that number set’s how fast we’re going to communicate to the Arduino. In the case of Serial.println() the expression is “hello littleWorld”. Some functions like the one we’re going to cover in this tutorial require at least some amount of arguments, usually greater than 1.

Quick review:

  • Data types are just different kinds of data. The Arduino needs to know if we’re using numbers or a string of characters.
  • Expressions are data that can be evaluated to numbers, strings, or functions.
  • Arguments is data that is passed into functions, values like numbers or strings.

On to the code!


And the circuit!

Hook-up a p1 POWER Bit to a button and into the a0 bitSnap, on the Arduino Bit.

New stuff!

You’ll notice that there are some lines of code you haven’t seen before. Let’s check them out!

Variable: Buckets and buckets of data

Variables! Our first line of code is a variable. In this case a number or integer that is assigned or given the name “button”. You can name it whatever you want but just don’t start the name with numbers or have spaces. If you need spaces in your name use underscores for now.

Screen Shot 2016-08-04 at 4.16.57 PM

Few things are going on here:
int is the type of this variable, and is a number.
– the = is an assignment operator. What you need to know about the assignment operator is that you’re giving a name (in this case the name is “button”) to whatever is on the right side of the =. Since button is an int, anything on the right side of the assignment statement will be a number.
You’ll notice that we’re using the number A0, if you check out the circuit we just built, there is a bitSnap labeled a0. That’s the bitSnap we’ll be using for our input.

mahbucket
src: khan academy

In it’s most primitive sense variables are labeled buckets of data that let us “hold” on to numbers or strings or other kinds of expressions. Meaning that if you wanted to reuse the number 9600 but call it something like “speed_limit” so it’s more “human readable” then you would use a variable. For example:


We just assigned our pizza loving string to a variable called message.

State your case!

One more detail to cover as we move on to the to even more tasty concepts. Each time we assign a variable it’s called a statement. They cause the state of a program, we’ll talk about them in detail in the future.

 

New functions / commands for the Arduino (OH MY!)

Let’s check out pinMode()


We know it’s a function because it has () at the end of it and we know it takes two arguments, the first being button that is a variable (which we assigned to A0) and a mode that’s either input (like a button or slider) or output like a bargraph.

On to analogRead()


Now you’ll notice we’re encountering another function, this one is especially important.

It let’s the Arduino tell us if a button has been pressed or not.

Let’s flash our Arduino! (no, not that kind of flashing)

Once you’ve typed up the sample code, hit the upload (make sure your circuit is turned on before load the Arduino IDE)
button found in the tool bar. It looks like this: ❤

This will flash the memory in our Arduino so it can run our program!

 

The Expected Outcome!

(click-through to see animation)

In your serial monitor, you should be able to see lots of 0s when you’re not pushing a button and 1023s when you are.

Think of the button as a dam that is holding back the electricity from your power bit from flowing to your Arduino. When the button is pressed the current flows openly at full force (1023). The Arduino has the ability to read the flow of the current coming through.

 

1023? Yeah you know me!

1023 is an 10 bit binary value all that means is your analogRead input will be either 0 and 1023 when using a button. The Arduino assigns a value to the amount of current that is coming in through it’s circuit. Try replacing the the button with a slider or a dimmer and see what happens!

 

You made it!!!

Congrats we’re one step closer to being an Arduino pro!

Here’s an emoji for all your hard work: 🎉

On to the next tutorial: you light up my life.

 

TL;DR


More Tips & Tricks

Tips & Tricks
Tips & Tricks: IR LED

The IR LED (or infrared light-emitting diode) module sends out light with longer wavelengths than visible light, similar to the li...

Tips & Tricks
Synth Tips & Tricks: Record your Music

When you make sweet beats with the Synth Kit, you might want to record them and share with your friends. This is a basic overview ...

Tips & Tricks
CodeKit Tips & Tricks

Get all the tips & tricks for your new Code Kit in one place