5,710
edits
Changes
→Dimmer
== 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 ===
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)