Saturday, April 2, 2011

Final Class: Arduino and Physical Computing

Hello class! Time sure has flown by, much like how fast this class will go by with all of the cool things we are going to learn today.

Here is a brief break down of the things to do:
  1. Review array code from last week and discuss in depth what arrays are and how they are to be used.
  2. Learn how to control an RGB LED.
  3. Use a potentiometer attached to the Arduino to adjust the speed of a movie playing in Processing.
  4. Control a drawing program with values sent from Arduino.
Let us start by reviewing Arrays from last week.

Arrays and For Loops:

Arrays are a way of declaring multiple of the same type of variable in a row in memory. Usually they are used with the idea that the values are multiples of the same type of object. What I mean is while the value of the photocell is an integer it doesn't make sense to include it in an array of integers that represent the digital pins used to power LEDs. Arrays are declared a little differently then other variables. Here are a few ways to declare an array:

int myInts[6];// this creates an array that has 6 places but does not define what the values are
int myPins[] = {2, 4, 8, 3, 6};// this creates an array of 5 places and defines the values
int mySensVals[6] = {2, 4, -8, 3, 2};// this creates an array of 6 places but only defines the first 5 values

This is how you access the values that are stored in the arrays above:

myPins[0], where the value would equal 2.

The number inside of the square brackets [ ] indicates which element to access. Arrays are 0 based, meaning the first element is considered the 0 place not the 1st place.

Here is how you change the value of an element of an array:
myPins[1] = 9;// now the values for myPins look like this: {2, 9, 8, 3, 6}

The real power of arrays is unleashed when combined with for loops. The structure of a for loop is a little confusing at first, but once you understand how to use them they are essential. Basically a for loop is a way of repeating a command a determined number of times. You can use an array to count from any number by any number to any number. This makes it perfect for working with arrays because instead of manually putting 0-4 inside of the [ ] to access the values of myPins array, you can use a variable.

Here is a for loop that prints the values of myPins array to the serial monitor:

for(int i = 0; i < 5; i++){
Serial.print(myPins[i]);
Serial.print(", ");
}
Serial.println();

This snippet of code will display the values with a comma and space between each value and then a line break after the last element has been printed. I recommend that you take a look at the for loop link above. The guys at Arduino have a wonderful description of how the mechanics work.

RGB LED Control:

RGB LEDs are actually 3 LEDs in one. In our case 1 red, 1 blue and 1 green LED all with the same ground pin. The RGB LED you have has a clear lens and 4 leads. The leads are as follows:
The longest lead is the ground.
The lone lead next to the longest is the red LED.
The first lead on the other side of the longest leg is the green LED.
The opposite lead from the red lead is the blue LED.

Create this circuit and then load the code that is in your class4 folder.

Start by loading the class4/rgb_LED sketch. With this sketch there are two modes: animate_rainbow and contol_rainbow. The sketch begins running animate_rainbow(), which runs the LEDs through a ROYGBV looping color animation. Commenting animate_rainbow() and uncommenting control_rainbow will allow you to use the potentiometer as a control for which color is displayed.



Using Arduino and Processing together:

First off, what is Processing? From the processing website:

Processing is a programming language, development environment, and online community that since 2001 has promoted software literacy within the visual arts. Initially created to serve as a software sketchbook and to teach fundamentals of computer programming within a visual context, Processing quickly developed into a tool for creating finished professional work as well.

As a programming language, Processing is built from Java and has a strong visual output capability. When we open up the Processing IDE you will notice a strong resemblance to the Arduino IDE. This is because they were meant to work together and the founders of both languages worked together to make them look and work similar. The similarities are greater than skin deep, with Processing having a huge community following offering up tons of code resources.

There are a few ways to work with Arduino and Processing, but I am going to demonstrate only one way today. If you are interested you can check out the Arduino library available in Processing. The library allows you to make Arduino calls directly from Processing.

Today we are going to send a byte through serial to Processing and use that value to adjust the speed of a looping movie. Let's start off by making a simple circuit with the Arduino using a potentiometer and 2 different colored LEDs. The potentiometer will give us a value and the LEDs will give us an idea of what that value is before it gets to Processing. Go ahead and make the circuit in the following image. This is to help differentiate between when the video will be playing forwards and backwards.


Now load the sendSerial sketch in the Arduino class4 folder.

The code maps the value of your potentiometer to light up one LED with one half of the values and light tup the other with the other half of values. The center position of the potentiometer will render both LEDs off. These results mimic how the Processing will react to the values of the potentiometer.


Open Processing and load the serial_control_VideoPlayback sketch in the for Arduino_class4 folder.
Now with the Arduino plugged into the usb slot, go ahead and press the play button in Processing. You will notice that when you change the potentiometer, the speed at which the movie is playing will change. It is a lot of fun to manipulate computer programs with physical objects!


Now load the serial_control_diameter sketch in the for Arduino_class4 folder in Processing. Using the same functionality of the Arduino code, we now have different functionality on the computer. Now when turning the potentiometer you can control the diameter of an ellipse that is being drawn where our mouse is.