Classes/2010/VIS147A/Lab9

From Robert-Depot
Jump to: navigation, search

Interfacing

This is the program we are going to use for our in class work, and for your homework this week. To get a sense of what people do with processing, you can look at their exhibition space http://processing.org/exhibition/, or do some google searching. We are going to use Processing because it is easy to get started with, quick to make experiments, and it has a similar layout and feel to our Arduino software.

Serial Basics

Basic text output from the microcontroller is accomplished with

Serial.print()

and

Serial.println()


You can use this to verify values and behavior inside of a program, for debugging, for example:

Displaying values of sensors:

val=analogRead(input_pin);
Serial.print(val);

(assuming you've done the necessary setup).

Checking program flow, communicating the status of things to the user or debugger (you!).

Inside the Arduino software, we will use the "Serial Monitor" to observe what is going on with the serial port. Here is where you will see data printed with the Serial.print() command, and you can also send some very simple messages back to the processor with the "Send" button.

  • Open the Sketchbook->Examples->Communication->ASCIITable, compile it, and run it on your processor.
  • This program does basic text communication from the processor to your PC, running through all of the characters in the alphabet.
  • Click on the "Serial Monitor" button (to the right of the "Upload to Board" button) in the arduino software. verify that your processor is communicating with the PC (do you see the letters streaming by?) This is your processor sending data to the computer. We will use this later.

Sending Data from your Arduino to the Computer

For this section you are going to use three potentiometers (or light sensor, or whatever you would like) to drive an application on the computer. Examples->Communication->VirtualColorMixer

  • Wire up three potentiometers as voltage dividers (like last week), and connect them to Analog_IN pins 0, 1, and 2.

Pot in.png Color mixer pot in.jpg

  • Compile the Virtual Color Mixer, and upload it to your arduino.
  • In the Serial Monitor, verify that you see R, G, and B values, and they change as you move the potentiometers.

Color mix serialout.png

  • Create the processing Sketch "arduino_color_mixer"
  • Change the Serial port in the initialization in Processing. (port = new Serial(this, Serial.list()[2], 9600); on my computer)
  • Change the Integer.parseInt(buff); statements to say rval = Integer.parseInt(buff)/4;. This relates to the issue last week with the analogRead() and analogWrite(): our Arduino is transmitting a 10bit (0-1023) value captured from the analogRead(), but the background(rval, gval, bval); command in Processing wants 8bit (0-255) values for Red, Green, and Blue. Thus, we need to divide the 10 bit values by 4.

Arduino color mixer.png

  • Run this program (same as on the Arduino, click the little "Play" button at the upper left). NOTE: Hold down the reset button on your Arduino while you start the Processing applet. Release the button once the new window has appeared. If you do not, the Processing applet will start reading mid-data stream, which will confuse it and crash. You should see something like the window below:

Color mix.png

  • You should be able to change the color of the on-screen square by dialing the knobs back and forth. This behavior could be anything: you could move images around on the screen according to the knobs, you could trigger media clips, you could fire off e-mails. Anything you can program on the computer could be triggered by input from your Arduino. This is the first half of an interactive microprocessor / computer sculpture.

Sending Data from the Computer to Control your Arduino

Controlling a physical LED with the computer.

  • Compile the Examples->Communication->Dimmer example, and upload it to your board. This example sets up communication on the serial port (in the void setup() function, and then reads values from the Serial input, and writes them to the ledPin output.
  • Hook up a simple resistor and LED to the pin 9, to test the dimer example.

D out 2.png

  • Connect up the TIP120 driver circuit and motor from last week to the ledPin (pin 9). You should now be able to control the motor speed via the serial port on your computer... interesting...
  • Click on the Serial Monitor. Try sending different values to the board. Type in numbers and press Enter (or click on the "Send" button, either will work).
    • What behavior do you observe?
    • Can you get the motor to speed up or slow down?

modifying for rudimentary keyboard control

As-is, the Dimmer code is not very useful for controlling through the serial terminal like this. Now we are going to modify it to so that it works a little better. Our goal is to be able to set the speed of the motor typing the numbers 0-9 on the keyboard. 9 will go fastest (nearly full speed), and 0 will be a dead stop.

  • Make a new copy of the Dimmer example, and replace the original loop() code with this new one:
void loop()
{
 char val;
 byte out_speed;
 // check if data has been sent from the computer
 if (Serial.available()) {
   // read the most recent byte (which will be from 0 to 255)
   val = Serial.read();
   
   if((val>='0')&&(val<='9')) {
     val=val-'0'; // set value between 0 and 9. 
     out_speed=val*28; // set out_speed between 0 and 252.
     analogWrite(ledPin, out_speed);
   } 
 }
}
  • What I have done is change the Serial.read() and analogWrite() from before. Now we are reading a char rather than a byte from the Serial port, and we are checking whether this character it reads is one of the characters between '0' and '9': a letter on the keyboard was pressed, it is one of the numbers? If it is (if((val>='0')&&(val<='9'))) then we will set the speed of the motor proportionally.
  • Compile and upload this sketch to the board, and try it out. Start the Serial Monitor again, and try sending the numbers 0-9 on the keyboard, the motor should speed up and slow down appropriately.
  • EXPLANATION: What did we just do? The problem in our original Dimmer code was that the microcontroller was looking just at the binary value of the data it was receiving, but did not know what key was pressed. So, when we typed the number 0, which happens to have a binary value of ___ in the ASCII table, it will set the speed to that value, not to 0. We changed the program so the microcontroller is looking for the specific characters, '0' to '9', and then doing the appropriate math to set the output to the right speed.

controlling your arduino from processing app

Save your modified Dimmer example, and revert back to the original dimmer code. We are now going to learn how to control the processor from the processing App. See the commented Processing code hidden at the end of the Dimmer program.

  • In Processing, open a new sketch, and copy this code into it. Save this sketch as "arduino_test" or something like that. It should look like this:

Arduino serialtest processing.png

  • Try running this sketch. When you do, it may give you an error about "COM1" or something of that sort. You need to tell the program what serial port your arduino is connected to. When the program runs, you will see a list of Serial ports, you can see mine in the picture above. On my comnputer, I see that my serial port is the "tty.usbserial-A900acnt", which is listed as device number 2 ([2]), so I go back into the processing code and change the line that says port = new Serial... to read port = new Serial(this, Serial.list()[2], 9600);
  • Now if I run the sketch again, I know that it will be properly communicating with the Arduino. You will need to modify that line to reflect whichever serial port your arduino is connected to.
  • Upload the original (unmodified) Dimmer code to your arduino in the arduino software. Your processor should now be running that code... waiting for input!
  • Run your newly created arduino_test sketch in processing. You will see a small graphic window (like the one below), as you drag your mouse forwards and backwards across that window, your motor should speed up and slow down. Allright!! Now you have a simple graphical interface!

Arduino serialtest.png

Send data from your sensor to the computer

  • Assuming you have a sensor which produces an analog output, hook it up to one of the Analog Inputs on your arduino. If you do not have a sensor, use the light sensor from last week. If you are not sure how your sensor works, come over and talk to me and we can try to figure it out.
  • Run a short program to read data from that sensor and send it to the Serial port.
    • You will need a setup() block, which initializes the Serial communication Serial.begin(9600);
    • And in the main loop of the program (loop()), you will need to do the analogRead() and Serial.print() commands from above.
  • See what the output from your Sensor is. How does it change as you manipulate your sensor? This is what we were getting at last week figuring out the voltage range for our sensors: when we want to use a sensor to make a project sensitive and responsive to the environment, we need to know what voltages correspond to the conditions of interest. When you start with a new project, this sort of Serial communication is a very valuable tool.

Homework

With your sensor, build a simple computer/microprocessor project. Establish communication between the computer and processor, either controlling the device with commands from your computer program, or driving the computer program with information from the processor. You may use Processing, and adapt one of the simple examples we have seen in class, or you can use any other program you are comfortable with (Flash, MaxMSP, PD), so long as you can demonstrate it to us in class next week. We will take the first part of class to look at your these mini-projects.

The little program from the first part of the lesson, where your Arduino program outputs the data from your sensor Serially will be useful here. Now you just need to make a program on the computer to take that data and do something with it. If you are looking for inspiration, pick a couple of the sketchbook examples in Processing and run a few of them. You will need to intergrate the Serial data input into one of those examples, or come up with something new. We can work on this in class today.

Interesting Side Note: Pachube

http://www.pachube.com/ a service that enables you to connect, tag and share real time sensor data from objects, devices, buildings and environments around the world. The key aim is to facilitate interaction between remote environments, both physical and virtual."

Extra Info: Interfacing with Other Devices

Connecting to various external devices and components: http://www.arduino.cc/playground/Main/ComponentLib