Changes

Week 4 Lab

1,621 bytes added, 04:29, 21 April 2009
Dimmer
== Dimmer ==
Examples->Communication->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 <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 ===
* Click on As-is, the Dimmer code is not very useful for controlling through the "Serial Monitor" button 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 right 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 experimenting original <code>loop()</code> code with this new one:
void loop()
{
char val;
byte out_speed;
// check if data has been sent from the computer
if (Serial.available()) {
val = Serial.read();
if((val>='0')&&(val<='9')){ analogWrite(ledPin, 28*( val=val-'0'); // set value between 0 and 9. out_speed=val*28; // set out_speed between 0 and 252. analogWrite(ledPin, out_speed); }
}
}
 * Connect up What I have done is change the TIP120 driver circuit <code>Serial.read()</code> and motor <code>analogWrite()</code> from last weekbefore. Voila! You have 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 simple motor speed controlletter on the keyboard was pressed, through it is one of the computer.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)