CSCI 255 — Using an Arduino

A bit about the Arduino

Let’s talk about the Arduino for a while.

Starting Arduino

Plug in the Arduino board using the USB cable and start up the Arduino IDE. If you are using a Arduino Uno, make the following choices.

If you are using a different type of Arduino, you should match your processor board. If you see /dev/ttyUSB0 as the Serial Port choice, accept it.

If you receive any questions about the group dialout, we will need to handle that. (This is the first time we’ve used the Arduino with the new computers.)

An exploration of the Arduino, ATmega328, and C++

In this part of the lab, you are going to turn an LED on and off as you are introduced to many things that will appear later in this course.

Blinking the LED

Open Blink tutorial, However, don't use breadboard. On the Arduinos we have, pin 13 is connected to a built-in LED and current-limiting resistor. Cut-and-paste the blink program into a new sketch and run-it. You should see the LED blink.

Change the blink rate. Make it blink as fast as you can without it looking like a somewhat dim LED.

Blinking the LED with memory

In Chapter 8 of the textbook, you will read about how “special function registers” are used to control I/O in embedded processors. The special function register looks like a memory location to a C program. In this secdtion, you’ll skip the digitalWrite and write directly to the pin.

Printing in the serial monitor

You will use two very important features of the Arduino language in this part of the lab. Take a look at the following references so that you can do the next part of the lab.

Add a call to Serial.begin(9600) in your setup function to initialize the serial port. Now add a statement to print the variable PORTB in binary in your loop function after each call to digitalWrite.

Build and run your program. You will need to do ToolsSerial Monitor to see the output of your program.

Be sure PORTB is being printing within the serial monitor. The output line should look something like the following:
Read the documentation for Serial.print() until you get this right.

Special function registers of the ATmega328

The variable PORTB is a special memory location within the ATmega328 chip. Writing to this location sets the output of pins 8 to 13 of the Arudino to either 0 (low) or 1 (high). In embedded microcontrollers these pins are called GPIO, general-purpose input/output, pins.

Take a look at how the C code implements GPIO in Digital Write (around line 137). Setting an individual bit is accomplished by use of masking operations inside this if statement:

    if (val == LOW) {
        *out &= ~bit;
    } else {
        *out |= bit;

The * in the above code segment is the C and C++ dereference operator. You will meet it in Chapter 6.

Look at the Arduino page for port manipulation to see how to change PORTB without calling DigialWrite, along with a reason why you might want to do this.

Modify your loop function so that it turns the LED on and off directly, that is, with statements similar to
    PORTB |= Bxxxxxxxx ;
    PORTB &= ~Bxxxxxxxx ;
Figure out the xxxxxxxx on your own.

Big hint: Pin 13 corresponds to bit 5 of PORTB. By the way, the rightmost bit is number 0.

C++ implementation of Arduino classes

Serial is an object within the C++ class HardwareSerial and print is a method of HardwareSerial. The HardwareSerial class extends the Stream class, and The Stream class extends the Print class.

Every Computer Science guru must have seen C++ at least once. Take a look at the following C++ modules and be glad you didn’t have to take CSCI 202 using C++.

Most likely, the instructor will guide you through this.

Input to the Arudio

Right now, your Arduino has only one output. In this part of the lab, you will also process input.

Start by taking a look at an Arduino lesson, from Adafruit, on Digital inputs. Best to open it in a second browser window.

Building a breadboard

Stop the Arduino IDE and unplug your Arduino.

Get a breadboard (the smaller ones will work fine) and create a circuit similar to that shown in lesson but include a second LED connected to the Arduino output pin of your choice. (I suggest pin 2.)

You must use a current limiting resistor for each LED, because only pin 13 is connected through a resistor on the Arduino board. You do not need a pullup resistor for your switches, because there is an internal pullup of 20 to 50 kΩ in the ATmega328 chip. This is common for microcontroller chips.

Also because the USB standard limits current to 500 mA, we are not going to bother with the resettable fuse.

Wire up your breadboard so that in has two input switches and two output LED’s.

Have someone, maybe a fellow classmate, check your wiring.

Loading the initial program

Start up the Arduino IDE and cut-and-paste the program from the lesson into a sketch.

Test the program.

Improving the program

Meaningful variable names

Change the names of the variables to something more useful than buttonApin and ledPin. I suggest you use leftSwitch, rightSwitch, leftLight and rightLight to hold the pin numbers.

Once you’ve done this, run your sketch again to make sure it works.

Meaningful function names

Think a little about how your program reacts to the switches and LED’s. What value is read when then switch is pressed: HIGH or LOW? What value is written to turn on the LED: HIGH or LOW? You might recall from the lab with transistors, it could be confusing to remember if the “output” of the switch was 0 or 1 when it was pressed. Let’s write two functions to help us out here.

These two functions are very short, probably just a single ifelse statement.

Again, test your program to verify this change. This time have your program turn on both LED’s.

The combinational gates

Modify your program so that the left LED displays the NAND of the two inputs and the right LED display the NOR. Do this without any ifelse statements in the loop(). Just use C’s logical operators and two simple statements.

If you can’t remember NAND and NOR try out this simple JavasScript page.

The SR flip-flop

Now create an SR flip-flop with your Arduino. Assume your switches are S and R and your LED’s are Q and Q'. You may assume that no one ever presses both S and R at the same time. If they do, your flip-flop is allowed to self destruct.

You may want to ad an extra variable to your program that records the present value of the flip-flop, but this really isn’t necessary.

Show off your flip-flip.

If you can’t remember the SR try out this simple JavasScript page.

The evil of delay

It’s time for the instructor to talk about one of his favorite topic: the evils of delay. However, it will take him a while to get to it.

The T flip-flop

The T flip-flop will toggle its output whenever the left switch is pressed. (You may ignore the right switch.) The initial state of the flip-flop should be 0 for the T.

Create a T flip-flop Arduino program. This time the solution is a little harder. Your program must remember the old value of the left switch so it can determine when the input has changed from 0 to 1.

Test your program by pressing the button about one a second, maybe to the beat of Stayin’ Alive. (It’s too bad we don’t have the sound working in the lab.) Sometimes it may not change.

Switch (de)bounce

Generally when you press a switch it will bounce on and off a few times. The bounces are very quick. You won’t see them when turning on a desk lamp. However, computers are so fast that the Arduino may &ldquot;think” you are really pressing that switch several times a second.

You can fix switch bounce mechanically by using mercury in the switch, but too much mercury can make you mad. You could also use a low pass filter, but that requires a capacitor and a resistor and clearly costs more.

Alternatively, you can add a call to delay() to your program to make it wait for the bounces to subside. You can find a few tutorials for switch debounce with delay on the internet.

Try to debounce the switch using delay. You have to call delay whenever the switch changes, even if it is changing from 1 to 0.

You may need to experiment to find the best value for the argument to delay.

The problem with delay

Avoid delay in embedded systems. When your Arduino sketch is executing delay() it does nothing else. That means it may miss other important events.

Once you start using delay(), it becomes hard to break the habit. The designers of Processing tried to remove delay() between versions 1 and 2 of Processing but the delay() addicts demanded its return. It is back in Processing, but is no longer mentioned in the Processing reference page. By the way, JavaScript does not have a delay operator, but jQuery does.

Also never use Arduino interrupts to debounce a switch. That is simply silly.

Using timers to avoid using delay

Fortunately there are many on-line tutorials on how to use timers to debounce a switch. Read the Debounce tutorial on the Arduino web site to see how this is done.

Debounce your switch with timers. You may need to tune the debounce “delay” to get your program to working smoothly.