Difference between revisions of "Week 4 Lab"
(→modifying for rudimentary keyboard control) |
(→Dimmer) |
||
Line 16: | Line 16: | ||
== Dimmer == | == Dimmer == | ||
− | |||
Controlling a physical LED with the computer. | 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 <code>void setup()</code> function, and then reads values from the <code>Serial</code> input, and writes them to the <code>ledPin</code> output. | ||
+ | * Connect up the TIP120 driver circuit and motor from last week to the <code>ledPin</code> (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" button (to the right of the "Upload to Board" button) in the arduino software. 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 === | === 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 <code>loop()</code> code with this new one: | ||
void loop() | void loop() | ||
− | + | { | |
char val; | char val; | ||
− | + | byte out_speed; | |
// check if data has been sent from the computer | // check if data has been sent from the computer | ||
if (Serial.available()) { | if (Serial.available()) { | ||
Line 29: | Line 34: | ||
val = Serial.read(); | val = Serial.read(); | ||
− | if((val>='0')&&(val<='9')) | + | 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 <code>Serial.read()</code> and <code>analogWrite()</code> from before. Now we are reading a <code>char</code> rather than a <code>byte</code> 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 (<code>if((val>='0')&&(val<='9'))</code>) then we will set | |
− | * | + | * Compile and upload this sketch to the board: |
* Typing 0-9 on the computer will set the motor speed from (0-98.8%) of full speed) (the <code>(28*val-'0'))</code> in <code>analogWrite()</code> above) | * Typing 0-9 on the computer will set the motor speed from (0-98.8%) of full speed) (the <code>(28*val-'0'))</code> in <code>analogWrite()</code> above) | ||
Revision as of 20:29, 20 April 2009
Contents
Interfacing
- Download and install processing http://processing.org/download
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
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 theSerial
input, and writes them to theledPin
output. - 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" button (to the right of the "Upload to Board" button) in the arduino software. 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()
andanalogWrite()
from before. Now we are reading achar
rather than abyte
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 - Compile and upload this sketch to the board:
- Typing 0-9 on the computer will set the motor speed from (0-98.8%) of full speed) (the
(28*val-'0'))
inanalogWrite()
above)
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 readport = 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.
- 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
- 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, 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!
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."
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