Arduino spooky projects_class3

Click here to load reader

  • date post

    06-May-2015
  • Category

    Technology

  • view

    1.433
  • download

    0

Embed Size (px)

Transcript of Arduino spooky projects_class3

  • 1.Spooky ProjectsIntroduction to Microcontrollers with ArduinoClass 321 Oct 2006 - machineproject - Tod E. Kurt

2. Whats For Today Controlling Arduino from a computer Controlling a computer from Arduino Servomotors R,G,B LED color mixing 3. Remove ProtoShield First half of class, we dont need it And we want to observe the Arduino boardShields down, capn! 4. Recap: ProgrammingEdit Compile Reset UploadRemember: always start from a known working system 5. Communicating with Others Arduino can use same USB cable for programming and to talk with computers Talking to other devices uses the Serial commands Serial.begin() prepare to use serial Serial.print() send data to computer Serial.read() read data from computerCan talk to not just computers.Most things more complex than simple sensors/actuators speak serial. 6. Watch the TX/RX LEDS TX sending to PC RX receiving from PC Used when programmingor communicating (and keep an eye on that peskypin13 LED too) 7. Arduino Says Hiserial_hello_worldSend Hello world! to your computer (and blink LED)Click on Serial Monitor to see outputWatch TX LED compared to pin13 LEDThis sketch is located in the handout, but its pretty short.Use on-board pin 13 LED, no need to wire anything up. 8. Telling Arduino What To Doserial_read_basic You type H LED blinks In Serial Monitortype H, press Send Watch pin 13 LEDThis sketch is in Examples/serial_comm/serial_read_basic.Notice how you might not always read something, thus the -1 check.Can modify it to print hello world after it receives something, but before it checks for H.This way you can verify its actually receiving something. 9. Arduino Communications is just serial communications Psst, Arduino doesnt really do USB It really is serial, like old RS-232 serial All microcontrollers can do serial Not many can do USB Serial is easy, USB is hardserial terminal from the olde days 10. Serial Communications Serial because data is broken down intobits, each sent one-by-one on a single wire:H = 0 1 0 0 1 0 0 0 = L H L L H L L L =HIGHLOW Toggle a pin to send data, just like blinking an LED Only a single data wire is needed to send data. One other to receive.Note, a single data wire. You still need a ground wire. 11. Arduino & USB-to-serialArduino board is really two circuitsUSB to serial ArduinomicrocontrollerOriginal Arduino boards were RS-232 serial, not USB. 12. New Arduino Mini Arduino Mini separates the two circuitsArduino Mini USB adapterArduino Miniaka. Arduino StampIf you dont talk with a computer, the USB-to-serial functionality is superuous. 13. Arduino to Computer Laptop Arduino boardTXUSBRXArduinoUSB to serial USB to serialprogrammer driverchipArduinoRX TXmicrocontroller-OR- Processing sketch-OR- Java program-OR- ... USB is totally optional for ArduinoBut it makes things easierOriginal Arduino boards were RS-232 serial, not USB. 14. Arduino & USB Because Arduino is all about serial, And not USB, Interfacing to things like USB ash drives,USB hard disks, USB webcams, etc. is notpossibleAlso, USB is a host/peripheral protocol. Being a USB host means needing a lot of processingpower and software, not something for a tiny 8kB microcontroller.It can be a peripheral. In fact, there is an open project called AVR-USB that allows AVR chips likeused in Arduino to be proper USB peripherals. See: http://www.obdev.at/products/avrusb/ 15. Controlling the Computer Can send sensor data from Arduino to computer with Serial.print() There are many different variations to suite your needs: 16. Controlling the Computer You write one program on Arduino, one on the computerIn Arduino: read sensor, send data as byte In Processing: read the byte, do something with itBut writing Processing programs is for another time 17. Controlling the Computer Receiving program on the computer can bein any language that knows about serialports C/C++, Perl, PHP, Java, Max/MSP,Python, Visual Basic, etc. Pick your favorite one, write some code forArduino to controlIf interested, I can give details on just about every language above. 18. Another Exampleserial_read_blink Type in a number 1-9 and LED blinks that number Converts number typed into usable numberThis sketch is also in the handout 19. Pulse Width Modulation More commonly called PWM Computers cant output analog voltages Only digital voltages (0 volts or 5 volts) But you can fake it if you average a digital signal ippingbetween two voltages. For example... 20. PWMOutput voltage is averaged from on vs. off timeoutput_voltage = (on_time / off_time) * max_voltage5 volts 3.75 Volts0 volts75%25%75%25% 75% 25%5 volts 2.5 Volts0 volts 50%50%50%50%50%50%5 volts0 volts1.0 Volts20%80%20%80%20%80% 21. PWM Used everywhere Lamp dimmers, motorspeed control, powersupplies, noise making Three characteristics ofPWM signals width Pulse width range (min/max) Pulse period(= 1/pulses per second)height Voltage levels(0-5V, for instance)period 22. Servomotors Can be positioned from 0-180 Internal feedback circuitry & gearing takes care of the hard stuff Easy three-wire PWM 5V interfaceMore specically, these are R/C hobby servos used by remote control enthusiastsIn general, servomotor is a motor with an inherent feedback mechanism that allows you to sendposition commands to it without requiring you to do the position reading. 23. Servos, good for what? Roboticists, movie effects people, and puppeteers use them extensively Any time you need controlled, repeatable motion Can turn rotation into linear movement with clever mechanical leversEven clothes use servos now: http://www.technologyreview.com/read_article.aspx?id=17639&ch=infotech 24. Servos Come in all sizes9g from super-tiny to drive-your-car But all have thesame 3-wireinterface157ghttp://rctoys.com/http://hobbypeople.net/ 25. Servo Mounts & Linkagesmounting bracket: http://www.sierragiant.com/prod28.htmlsdfsdf 26. ServosGround (0V)180Power (+5V)Control (PWM) PWM freq is 50 Hz (i.e. every 20 millisecs) Pulse width ranges from 1 to 2 millisecs 1 millisec = full anti-clockwise position 2 millisec = full clockwise position 27. Servo Movement0 degrees 45 degrees 180 degreeshigh high highlowlowlow1000 microseconds 1250 microseconds2000 microseconds To position, send a pulse train from 1 to 2 ms To hold a position, pulses must repeat Takes time to rotate, so pulse too fast & it wont move1 millisecond = 1000 microsecondSee http://www.societyofrobots.com/actuators_servos.shtml 28. Servo Movement 0 degrees90 degrees180 degrees1000 microsecs 1500 microsecs2000 microsecs In practice, pulse range can be 500 to 2500 microsecs(and go ahead and add a wire marker to your servo like the above)Put the red arm on your servo. Needs a philips screwdriver.Many commercial servo drivers have a calibration setting to deal with servo variability 29. Servo and Arduino First, add somejumper wires to theservo connector 30. Servo and Arduino Plug power lines in,Plug signal to digital pin 7 31. Moving a Servo Move the servo across its full range of motion servo_move_simple Uses delayMicroseconds() for pulse width Uses delay() for pulse frequencySketch is in the handoutCreated a custom function to handle making servo pulsesNew function delayMicroseconds(). Like delay(), but sec instead of msec.(and actually, just delaying 20 msec is kinda wrong. should be: 20 - (pulsewidth/1000) 32. Serial-controlled Servoservo_serial_simpleDrive the servoby pressing number keys Takes the last servo example andadds the lastserial example to it.This sketch is located in the handout.Why that for loop? Because it takes time for the servo to get to a position and it has no memory. 33. Controlling Arduino Any program on the computer, not just the Arduino software, can control the Arduino board On Unixes like Mac OS X & Linux, even the command-line can do it: demo% export PORT=/dev/tty.usbserial-A3000Xv0 demo% stty -f $PORT 9600 raw -parenb -parodd cs8 -hupcl -cstopb clocal demo% printf "1" > $PORT # rotate servo left demo% printf "5" > $PORT # go to middle demo% printf "9" > $PORT # rotate servo rightUnix is rad. 34. Take a Break 35. Servo Timing Problems Two problems with the last sketch When servoPulse() function runs,nothing else can happen Servo isnt given periodic pulses to keep itat positionIf a servo is not being constantly told what to do, it goes slack and doesnt lift/push/pull 36. Better Serial Servoservo_serial_betterWorks just like servo_serial_simple(but better) Update the servo when needed, not just when called at the right timeUses millis() toknow what time it isThis sketch is located in the handout.Trades memory use (the extra variables), for more useful logic.Can call updateServo() as often as you want, servo is only moved when needed. 37. Multiple Servos The updateServo() technique can beextended to many servos Only limit really is number of digital outputpins you have It starts getting tricky after about 8 servosthough 38. Arduino PWMwhy all the software, doesnt Arduino have PWM?Arduino has built-in PWMOn pins 9,10,11Use analogWrite(pin,value)It operates at a high, xed frequency (thus not usable for servos)But great for LEDs and motorsUses built-in PWM circuits of the ATmega8 chip - no software neededThe PWM speed used for analogWrite() is set to 30 kHz currently.When programming AVRs, PWM speed can be set to just about any value. 39. R,G,B LEDs Three PWM outputs and three primary colors.Just screams to be made, doesnt it? Arduino 220 (red,red,brown) orboard330 (orange,orange,brown) pin 11 pin 10pin 9gnd With RGB you canred green bluemake any color (except black)Put back on the ProtoShield for this.Use either the 220 or 330 ohm resistors in your kit, if you dont have enough of one or the otherI have lots more 220 if you need them 40. R,G,B LEDsCut leads of resistors and LEDs to make for a more compact circuit.Also, less likely to short against itself. 41. RGB Color FadingdimmingLEDsSlow color fading and mixing Also outputs the current color valuesto the serial portThis sketch is located in the handout.It just ramps up and down the red,green,& blue color values and writes them with analogWrite()from http://www.arduino.cc/en/Tutorial/DimmingLEDs 42. Mood LightDiffuser made frompiece of plasticscratched with sandpaperAlso, can use plastic wrap scrunched up to make an interesting diffuser. 43. Serial-controlled RGBserial_rgb_ledSend color commands to Arduinoe.g. r200, g50, b0 Sketch parses whatyou type, changesLEDsg50This sketch is located in the handout.Color command is two parts: colorCode and colorValuecolorCode is a character, r, g, or b.colorValue is a number between 0-255.Sketch shows rudimentary character string processing in Arduino 44. Reading Serial Strings New Serial function inlast sketch:Serial.available() Can use it to read allavailable serial data fromcomputer Great for reading stringsof characters The readSerialString()function at right takes acharacter string and sticksavailable serial data into itPay no attention to the pointer symbol (*)Must be careful about calling readSerialString() too often or youll read partial strings 45. Going Further R,G,B LEDS You can pretty easilyreplicate the Ambient Orb($150) functionality Make a status display foryour computer Computer-controlled accentlighting (a wash of coloragainst the walls)Ambient Orb doesnt connect to computer though. Uses the pager network.Ambient Devices: http://www.ambientdevices.com/ 46. Glowing Orb 47. Going Further Servos Mount servo on a video camera computer-controlled camera motion Make a robot (a little obvious) Lots of spooky uses theyre the core of movie animatronicsIm not too mechanical, so I dont have many concrete and still working examples of servo use. 48. Going Further Serial communications Not just for computer-to-Arduinocommunications Many other devices speak serial Older keyboards & mice speak are serial(good for sensors!) Interface boards (graphic LCDs, servodrivers, RFID readers, Ethernet, Wi-Fi) 49. Serial Examplesto Wi-Fito Ethernetto graphic LCDto 8-servo controllerLantronix Wi-Port and Lantronix Xport http://lantronix.com/Seetron Serial Graphic display and Mini SSC http://www.seetron.com/slcds.htm http://www.seetron.com/ssc.htm 50. Serial Examples to RoombaHacking Roomba, out in a few weeks, by me. ;-)http://hackingroomba.com/ 51. Next Week All about piezos Building a melody player Using piezos as pressure & knock sensors Using Processing with Arduino Stand-alone Arduino 52. END Class 3http://todbot.com/blog/spookyarduinoTod E. Kurt [email protected]