= Touch and Feel = Robots need to be able to sense their environment. One of the most basic sensing devices is the mechanical switch. A switch is any device that opens or closes a circuit mechanically. You can use a switch to detect when a robot has bumped into something or to stop a moving part (like an arm) from exceeding its range of motion (this is known as a **Limit Switch**). You have a couple of switches in your kit. [[Image(http://ecx.images-amazon.com/images/I/41BuqbQZt8L._AC_UL115_.jpg, align=right)]]. Each switch has two leads on the bottom and a button on top. When you push the button, the two leads are connected electrically. **Exercise**: Use a multimeter to measure the continuity or resistance of the switch in the open (button not pressed) and closed (button pressed) positions. * **Exercise**: [http://playground.arduino.cc/Main/FlightGearInputPushbutton read the position of a switch]: connect a switch to your arduino using the protoshield and breadboard. 1. Put the leads of the switch on opposite sides of the valley in the middle of the breadboard. NOTE: if you put both switch leads in the same row of holes on the same side of the valley, then they are already connected together by the breadboard and pushing the button will do nothing. 2. Use a jumper wire to connect one of the switch pins to +5v 3. Use a jumper wire to connect the other switch pin to an Arduino digital pin 4. Connect a 10K resistor to the switch pin that is connected to the Arduino digital pin 5. Connect the other end of the 10K resistor to Ground 6. Write software to change the state (on or off) of an LED each time the switch is pushed. == Why the 10K resistor? == The 10K resistor is called a pull-down resistor. It is used to keep the Arduino digital input in a known state when the switch is open. Without it, the digital input (which is very sensitive) could change its state due to electrical noise (e.g. from fluorescent lights). The 10K resistor keeps the input pin at the ground level maintaining a 'low' at the digital input until the button is pushed. When the button is pushed, the input pin is connected directly to +5v through the switch. Because the switch has extremely low resistance, the +5V overwhelms the weak pull-down resistor and the input pin will read high. It's important to understand pull-up and pull-down resistors as they are required in many circuits. This [https://www.youtube.com/watch?v=wxjerCHCEMg brief video tutorial] explains pull-up resistors. They are such a common requirement that many modern microcontrollers (like the arduino) have them built in where they can be turned on and off under software control see [https://www.arduino.cc/en/Tutorial/InputPullupSerial here]. == Debouncing == You may notice that your LED sometimes changes to the wrong state when you push the button. This is due to **switch bounce**; please see [http://www.labbookpages.co.uk/electronics/debounce.html this tutorial]. Switch bounce is particularly problematic when you are using a switch to count events. For example, to count rotations of a robots wheel to see how far you've gone. There are a number of ways to eliminate switch bounce using hardware solutions, but the least expensive solutions use software. The most common approach is to note the time when the switch first closed and then not count any more closures until a debounce period has elapsed (usually a few tens or hundreds of milliseconds). **Exercise**: write software to debounce your switch so it will reliably toggle the LED state. == Libraries == Arduino has a huge collection of libraries containing software routines you can incorporate into your programs to solve all sorts of problems. Naturally there is an already written library for switch debouncing: [http://playground.arduino.cc/Code/Bounce Bounce library]. **Exercise**: Add a library to your sktch (see [https://learn.adafruit.com/arduino-tips-tricks-and-techniques/arduino-libraries here] for details. == Switch terminology == There are many different types of switches (push button, toggle, knife, etc.), but there is some standard terminology engineers use to describe switches electrically that you should learn along with their schematic symbols: [[Image(switch_symbols.png, align=right,width=250,margin=10)]] * SPST = Single-Pole, Single-Throw: your push button switch has two leads that are either open or closed * SPDT = Single-Pole, Double-Throw: a switch with three leads: a common lead that is connected to one of the other two leads based on the switch position. * DPST = Double-Pole, Single-Throw: two SPST switches with a single control * DPDT = Double-Pole, Double-Throw: two SPDT switches with a single control [[Image(reed_switch.jpg,align=left,width=128,margin=10)]] Every possible combination can be found; you can read a clear explanation [http://www.musicfromouterspace.com/analogsynth_new/ELECTRONICS/pdf/switches_demystified_assembly.pdf here] (with good pictures) Switches are mechanical, but you don't necessarily have to touch a switch to operate it. Magnets are commonly used to open or close a switch. [https://www.sparkfun.com/products/8642 Magnetic reed switches] are frequently used in alarm systems where a magnet in a door or window opens or closes a switch mounted on the frame. In robotics, a magnet could be mounted on a moving arm and reed switches can be used to determine when the magnet is nearby. **Exercise**: Use a magnet to operate a reed switch [[BR]][[BR]] == Potentiometer (a variable resistor) == So far we've only looked at digital inputs: devices that have only two states (open/closed, on/off, high/low, etc.). The real world is not black and white and we often need to determine a shade of gray (where is an arm in its range of motion? How much pressure is the robotic hand applying to that egg?). Analog input devices can measure an input over a continuous range of values. A common example is the volume knob on a radio. A volume control is typically (or at least historically) made using a variable resistor known as a "potentiometer". A variable resistor changes its resistance according to some external stimulus (such as turning a knob). You can make a variable resistor yourself using a pencil. **Exercise**: draw a line on a piece of paper with a pencil; then go back and forth over it with the pencil **many** times so that you have deposited a great deal of graphite on that line. Place one lead of your multimeter at one end of the line (touching the graphite) and place the other lead somewhere in the middle of the line; measure the resistance of the graphite between the leads, then slide the lead in the middle closer and further from the other lead and watch the resistance change on the multimeter. [[Image(http://www.criticalvelocity.com/products/pot02_side.jpg, align=right)]] A potentiometer is very similar to the resistor you just made. Your kit contains a potentiometer with a black knob and three leads: one at either end of the resistor and a lead that connects somewhere between the two based on the position of a mechanical knob. See this [https://search.yahoo.com/yhs/search?p=Potentiometer+tutorial&ei=UTF-8&hspart=mozilla&hsimp=yhs-001 tutorial] **Exercise**: measure the resistance between leads of the potentiometer as you turn the knob. A common configuration for a potentiometer is to connect the lead at one end to +5V, the lead at the other end to Gnd, and then as the knob is turned, the voltage at the middle lead varies from 0v to 5v. **Exercise**:connect your potetiometer as described above and measure the voltage at the center lead as you turn the knob. Your Arduino computer has analog input pins that can be used to measure a voltage. See this [https://www.arduino.cc/en/Tutorial/Potentiometer example] and [http://www.instructables.com/id/Arduino-Basics-Using-potentiometers-I-made-it-a/ this one]. **Exercise**:use your Arduino to turn an LED on when the voltage at an analog input pin exceeds a threshold (see above example) **Exercise**:use your Arduino to vary the blinking speed of an LED based on the position of a potentiometer == More Experiments == * [https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v32/experiment-5-push-buttons Push Buttons]