Week 4 Lab

From Robert-Depot
Revision as of 20:10, 20 April 2009 by Rtwomey (talk | contribs) (controlling from a Processing app)

Jump to: navigation, search

Interfacing

Serial Interface, for debugging

Sketchbook->Examples->Communication->ASCIITable

Basic text output from the microcontroller,

Serial.print()

and

Serial.println()

Use this to verify values and behavior inside of a program, for debugging. Examples:

  • Displaying values of sensors Serial.print(val)
  • Checking program flow.

Dimmer

Examples->Communication->Dimmer Controlling a physical LED with the computer.

modifying for rudimentary keyboard control

void loop()
{
 char val;
 
 // 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'))
       analogWrite(ledPin, 28*(val-'0'));
 }
}
  • Connect up the TIP120 driver circuit and motor from last week. Voila! You have a simple motor speed control, through the computer.

controlling from a Processing app

See the commented out Processing code at the end of Dimmer example.

  • In Processing, open a new sketch, and copy this code into it. Save this sketch as "arduino_test" or something like that.
    • 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. On my computer, I see this output in the Processing window:
Available serial ports:

Stable Library
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
[0] "/dev/tty.modem"
[1] "/dev/cu.modem"
[2] "/dev/tty.usbserial-A900acnt"
[3] "/dev/cu.usbserial-A900acnt"
[4] "/dev/tty.Bluetooth-PDA-Sync"
[5] "/dev/cu.Bluetooth-PDA-Sync"
[6] "/dev/tty.Bluetooth-Modem"
[7] "/dev/cu.Bluetooth-Modem"
    • I know 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.
  • Computer -> Arduino
  • Arduino -> Computer

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 ideas for these projects.

Extra

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