We will be using git to distribute the code you need for studios and assignments. You will also use git to turn in your work. Here's a great video that explains what git is and how it works:
Before you can complete any of the work, you must set up your repository. You can find instructions on how to do this here.
Read the instructions above before clicking this link! Click here to access the Canvas page with the repository for this studio.
When you access the repository, make a note of the Local Destination
directory, as it will be needed later.
The Arduino is a (very) small computer that has dramatically fewer capabilities than the desktop or laptop machines that you have used in the past to run Java programs. For example, it doesn't have a keyboard, it doesn't have a screen, its processor is well over 100 times slower, and you might even be wondering, “what is the point?” The point is that small computers like the Arduino are priced relative to their capabilities. Want a computer for under $10? If so, the Arduino is a great choice! Its small size makes it incredibly useful for lots of jobs where it would seem overkill to use a $1500 laptop. Over the course of the semester, we'll discover all that the Arduino can do, even though it starts out as humbly as it does.
You will need to assemble your the main components of your Arduino kit. The tutorial from SparkFun (the supplier of the hardware) is here, or you can follow the description below. Either gets you to the same place.
The Arduino RedBoard:
The BreadboardHolder:
And the (solderless) Breadboard:
When done, it should look like:
You will need to:
Attach the Breadboard to the Breadboard holder with the labels on the breadboard facing the correct way, as shown above. The breadboard has an adhesive backing. Remove the protective layer and stick it to the holder in the indentation. The column letters a-j on the top and bottom of the breadboard should be in the same direction as the “sparkfun” label on the breadboard holder.
Put the RedBoard on the Breadboard Holder. Again, the print/labels should match those shown above. The Redboard has four holes for screws and the kit includes two screws. Any two holes will be suitable, but diagonal holes are recommended. Screwdrivers are available from the instructors and TAs.
Although the Arduino itself is a computer separate from your laptop or desktop, its lack of screen serves as an impediment to programming it directly. We write and compile programs for it on a larger computer and then send them over to the Arduino via USB. The Arduino, as you'll soon see, runs one program at a time, for as long as it's plugged in.
As you might imagine, the transfer process is very complex, and until the Arduino came out in 2005, microprocessor programming was a long and arduous task. Luckily for us, we are past those dark ages of computing and we have the Arduino IDE (Integrated Development Environment). You may be familiar with the Eclipse IDE, which we use in CSE 131 to write and compile Java programs. The Arduino IDE helps you write and compile Arduino programs and also manages uploading those programs to your Arduino board.
Arduino programs, however, are not written in Java. They are written in a variant of C, one with extra libraries specifically for writing Arduino programs. If you have already been introduced to C, programming the Arduino should be a snap.
In this first exercise you will get accustomed to the basics of Arduino C by writing a simple “heartbeat” program and uploading it to your Arduino board.
By the end of the exercise, you should know:
Onward!
To use your own laptop for development, you'll find the Arduino IDE on the official site, for Windows, Linux, and Mac. Don't use the web editor, install the latest IDE (currently 1.8.19) on your machine.
If you have any trouble with the installation process, finish the studio exercises using one of your group's computers, and then ask for help with the installation.
Do the studio today. Install the software later.
First, you need to check out your repository so that you can commit the changes you make today. You may also need to tweak Arduino's preferences to make it easier to use.
Open Eclipse. (If you haven't already installed Eclipse, see the instructions here)
Make sure to note where your Eclipse workspace is located. You will need it.
If you don't know, File>Switch Workplace>Other...
will show you. Press Cancel
if you don't want to change it.
In CSE 132 this semester, we will be using Git as our version control system. We have made a git Tutorial to get your workspace set up and running.
Make a note of the Local Destination
directory, as it will be needed later.
In Eclipse nagivate to the studio01
helloworld.ino
file. If the icon shows the Arduino icon (blue/green circle with a white infinity symbol), double click on the file. If not, right click on the file name and select Open With > Other...
and select the Arduino application (called Arduino file
on Windows systems).
The helloworld.ino
file is a complete Arduino program. Compiling and uploading it should help you learn the Arduino interface.
helloworld
and upload it to the Arduino (the Verify button just compiles your program, looking for errors in the code).Done uploading.
).Hello, world!
.Hello, world!
again. Newline
. Change this to the No line ending
option. This has to do with input to the Arduino from the keyboard. Although this studio will not provide keyboard input to the Arduino, future studios and assignments will, and it is important that the No line ending
option is selected or unintended isues may arise. You should get into the habit of making sure this option is selected.The Arduino has two entry points into your code, or, in other words, two places it looks to run your code. Whenever the Arduino starts up or is reset, the Arduino runs the void setup()
function once. After that, the Arduino runs the void loop()
function over and over until the Arduino is unplugged or reset.
Opening serial monitor or pushing the reset button on the Arduino both reset the Arduino.
Note that opening the serial monitor will sometimes print garbage data as the signals between the Arduino and computer sync up.
Problems uploading?
Considering that you are compiling a program from C, using an old USB driver designed by one company to communicate with a board designed by another company running code designed by a third, it's surprising that Arduino upload works as often as it does.
But stuff goes wrong. A lot. Here's how to troubleshoot your upload.
- Is your code free of syntax errors? Make sure that your code is correct (Verify it and make sure the status is
Done compiling.
)- Are you writing to the correct port? Look under
Tools>Port>
and select a different one. On Windows it will be something likeCOM3
. On Mac, it will be something like/dev/cu.usbmodem1492
. There's no good way to find the correct one aside from guess-and-check.- Restart the Arduino IDE and plug everything in again. It works a lot of the time.
Now you'll write your own Arduino program in C.
Take a quick look at the Arduino reference. It should be surprisingly similar to Java.
Pay special attention to the Serial
library (under Communication in the Functions section) and the delay()
function.
Arduino sketches have the suffix .ino
and are required to be contained inside a folder with the same name as the sketch. Inside the studio01
folder, author a program called heartbeat
(so the heartbeat.ino
file should be in studio01/heartbeat/
) that, once per second, prints:
<n> sec have elapsed
where <n>
is the number of seconds that have passed since the program was reset. The delay()
function will come in handy, as will Serial.print()
and Serial.println()
. There are a viariety of ways to create new files and folders. Feel free to create the directory heartbeat
and hearbeat.ino
however you wish. One such way is to right-click directly on the studio01
directory in Ecliplse and nagigate to New->Folder
and enter the name heartbeat
. Then similarly right-click on your newly-created hearbeat
folder and navigate to New->File
and enter the name heartbeat.ino
.
Note that C strings work differently than Java strings (namely string + string
does not exist in C, and Arduino C does not support printf()
), so you'll need to work around that. The Arduino reference page on strings explains the C implementation in more detail.
Verify that your newly authored program is functioning at least approximately correctly.
Now that you know the basics of the Arduino IDE and its programming language, it’s time to go a bit more in-depth about how it and computers in general work.
If you took CSE131 or AP Computer Science, you should already be familiar with Java. We will still use a lot of Java in this class, but we’re also switching the emphasis to another language called C.
We do this for two reasons:
Unlike Java, which runs on a virtual machine on top of your existing operating system, C compiles to pure machine language: the direct instruction set that your computer actually runs. In other words, if you’d like to see the exact instructions your computer uses to draw a pixel onscreen in a video game or download your email, you look at a compiled C program.
Lucky for us, uncompiled C (like, C code) looks a lot like Java. So much so that you should be able to read C with no problems.
Compiled code is a different story. We’ll deal with the human-readable “rendition” of machine-language, Assembly, later in the semester, but for now we’ll deal with the more immediate problem of how programs interact with the computer
But before we can ever talk about I/O (input/output) and programs and compiling things, you need to understand how the computer stores data. In other words, before you can understand how computers manipulate data, you must know how they represent it.
To that end, we’ve prepared some pen-and-paper exercises to get you thinking about data like a computer does.
If you are having trouble with the concepts behind any of these questions, try reading Chapter 8 in the course textbook or look through the Guide to Information Representation.
Binary is what every form of data on your computer eventually boils down to: some chain of HIGH
s and LOW
s, zeroes and ones. These questions will explore your understanding of binary as a base 2 number system.
The subscript on a number indicates its current base. If no base is given, assume base 10.
Hexadecimal is base 16. It ends up being a very nice way of representing computer-related numbers because it jives with binary so well.
A
through F
to go up to base 16. What is the highest base we can represent if we use all the letters of the alphabet?If possible, have a TA check your answers before moving on
Both the Arduino IDE and your Eclipse Workspace should be set up before starting this section (see above for help).
bits.ino
( bits / bits.ino
)Run bits.ino
in the Arduino IDE, opening up the Serial Monitor after it is done uploading.
In the Serial Monitor you will see the number 100 written in Decimal
, Hexadecimal
, and Binary
. You will also see what the number looks like after a LEFT-SHIFT
, RIGHT-SHIFT
, and INVERSE
.
LEFT-SHIFT
do?RIGHT-SHIFT
do?INVERSE
do?a
void setup() {
unsigned a = 100; //Change this number
...
a
greater than 32768
LEFT-SHIFT
?Here is a great introduction to FSMs. They are also described in Chapter 7 of the text (read Section 7.1).
For the final part of the studio we have designed another program FSM.ino
, a simplified binary counter, designed to introduce you to the concepts and syntax of FSMs that you will need for Assignment 1
FSM.ino
( FSM / FSM.ino
)FSMs.ino
onto the ArduinoHere is a visual depiction of the FSM:
1 : 001
.Here is this FSM’s enum
:
enum State {
up0, // 0
up1, // 1
up2, // 2
up3, // 3
up4, // 4
up5, // 5
up6, // 6
up7, // 7
};
State counterState = up0;
What does this Output:
state = up4;
Serial.print(state);
Enum types in C are just numbers with readable names, and those names are not compiled into your program – meaning “up4” is not accessible at runtime. In other Languages, however, Enums sometimes have a toString()
or name()
method that will allow access to a name at runtime.
An Enum
is useful when a variable can only take one out of a small set of possible values. Examples include Months, Game Pieces, or the Cardinal Directions.
A great way to think about these are Drop Down Selection boxes:
There can only be one selection from Multiple Choices
enum Fruit {
Banana,
Apple,
Mango
};
Fruit myFruit = Banana;
switch(myFruit) {
case Banana:
print("Banana Selected");
break;
case Apple:
print("Apple Selected");
break;
case Mango:
print("Mango Selected")
break;
}
switch
statements are where enums shine.
Here is the Switch statement from the Example:
switch(state){
case up0: //When state up0 , the FSM must:
bit1 = 0; //set the bits to match the Count
bit2 = 0;
bit3 = 0;
pprint(state);
state = up1; //Move to the next state
//The next loop will go to case up1
break; //Break to the end of the switch
//So the next case won't run too
case up1: //only if counterState == up1
}
Here is an example of an FSM that can both switch and remain in its own state depending on the Input:
Things can can get Confusing Quickly:
Go through Key Concepts in your head to make sure you have all that you need to start on Assignment 1. As always if you think you missed a concept or just want a wonderful TA to explain it to you Please Ask (it’s their job). In addition, there will be a lecture on Wednesday that will go over a large fraction of this material.
Make sure to commit your workspace
In Eclipse's package explorer, any files you have modified since your last commit are prefixed with a >
.
If you have saved some files from the Arduino IDE, they might not yet appear in the navigation pane of Eclipse. Right-click on your repo and choose Refresh
and they should show up.
Right-click the outer-most folder (you want to commit everything within), and choose Team>Commit...
. Write a helpful message and press Commit and Push
.
You can verify that your changes went to the server by going to your GitHub page for your repository. Click the “Source” link on the left side of the page and make sure your file exists with the code that you wrote.
Check out with a TA
Repository structure for this studio:
studio01/
helloworld/
helloworld.ino
heartbeat/
- heartbeat.ino
bits/
bits.ino
FSMs/
FSMs.ino
This is a checklist for you to see what the Studio is designed to teach you.
break
Generated at 2025-01-15 23:32:15 +0000.
Page written by Claire Heuckeroth, Ben Stolovitz, Sean Schaffer, and Roger Chamberlain.