Changes

Jump to: navigation, search

Week 4 Lab

5 bytes removed, 15:46, 21 April 2009
no edit summary
* 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 to the Computer from your Arduino ==
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.
[[Image:Pot_in.png]] [[Image:Color_mixer_pot_in.jpg|300px]]
*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.
[[Image:Color_mix_serialout.png|300px]]
*Create the processing Sketch "arduino_color_mixer"
*Change the Serial port in the initialization in Processing. (<code>port = new Serial(this, Serial.list()[2], 9600);</code> on my computer)
*Change the <code>Integer.parseInt(buff);</code> statements to say <code>rval = Integer.parseInt(buff)/4;</code>. This relates to the issue last week with the <code>analogRead()</code> and <code>analogWrite()</code>: our Arduino is transmitting a 10bit (0-1023) value captured from the <code>analogRead()</code>, but the <code>background(rval, gval, bval);</code> command in Processing wants 8bit (0-255) values for Red, Green, and Blue. Thus, we need to divide the 10 bit values by 4.
[[Image:Arduino_color_mixer.png|300px]]
* 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:
[[Image:Color_mix.png]]
 
* You should be able to change the color of the on-screen square by dialing the knobs back and forth.
 
== Controlling the Arduino from a Computer ==
* 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 Send Data from a Processing app =the Computer to Control your your Arduino ==
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:
* You can interface the arduino with any other program that can communicate via the serial port. This could be MaxMSP, Java, C++, OpenFrameworks (http://www.openframeworks.cc/download) or many others. Here is a complete list: http://www.arduino.cc/playground/Main/InterfacingWithSoftware
** Also you can communicate via network with one of these serial-to-network proxies: http://www.arduino.cc/playground/Interfacing/SerialNet
 
== The other way around: Driving a Computer Program with Data from the Arduino ==
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.
[[Image:Pot_in.png]] [[Image:Color_mixer_pot_in.jpg|300px]]
*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.
[[Image:Color_mix_serialout.png|300px]]
*Create the processing Sketch "arduino_color_mixer"
*Change the Serial port in the initialization in Processing. (<code>port = new Serial(this, Serial.list()[2], 9600);</code> on my computer)
*Change the <code>Integer.parseInt(buff);</code> statements to say <code>rval = Integer.parseInt(buff)/4;</code>. This relates to the issue last week with the <code>analogRead()</code> and <code>analogWrite()</code>: our Arduino is transmitting a 10bit (0-1023) value captured from the <code>analogRead()</code>, but the <code>background(rval, gval, bval);</code> command in Processing wants 8bit (0-255) values for Red, Green, and Blue. Thus, we need to divide the 10 bit values by 4.
[[Image:Arduino_color_mixer.png|300px]]
* 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:
[[Image:Color_mix.png]]
 
* You should be able to change the color of the on-screen square by dialing the knobs back and forth.
== Send data from your sensor to the computer ==

Navigation menu