Library for the Sparkfun 5- Way Tact Switch (Part# BOB- 1187) Board is modified for use with a single analog input.
Fork of FivePosSwitch by
FivePosSwitch.h@5:413df8008ce3, 2015-10-08 (annotated)
- Committer:
- electromotivated
- Date:
- Thu Oct 08 23:54:31 2015 +0000
- Revision:
- 5:413df8008ce3
- Parent:
- 4:10c4b7cb4e8c
enum DOWN (=4) definition had compile conflicts with cmath.h, which apparently has a #define something 4. This would cause problems when using the DOWN tag. Just made values negative to hopefully avoid the evil #define errors;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| electromotivated | 0:0388385d4480 | 1 | /* |
| electromotivated | 0:0388385d4480 | 2 | Bryce Williams |
| electromotivated | 0:0388385d4480 | 3 | 10/04/2015 |
| electromotivated | 0:0388385d4480 | 4 | |
| electromotivated | 0:0388385d4480 | 5 | Library for modified Sparkfun 5- Way Tact Switch Breakout (Part# BOB-11187) |
| electromotivated | 0:0388385d4480 | 6 | |
| electromotivated | 0:0388385d4480 | 7 | Breakout Board Mod: Remove the 10k pull- up resistors. This will make |
| electromotivated | 0:0388385d4480 | 8 | the board a basic breakout for the 5-Way switch. |
| electromotivated | 0:0388385d4480 | 9 | The Vcc pin (+) can be clipped or left unimplented |
| electromotivated | 0:0388385d4480 | 10 | (i.e. no connect). |
| electromotivated | 0:0388385d4480 | 11 | |
| electromotivated | 0:0388385d4480 | 12 | Circuit Setup: |
| electromotivated | 0:0388385d4480 | 13 | Vcc |
| electromotivated | 0:0388385d4480 | 14 | | |
| electromotivated | 0:0388385d4480 | 15 | ---------------- | ---------- |
| electromotivated | 0:0388385d4480 | 16 | | 5- Way (-)|-------| | mBed | |
| electromotivated | 0:0388385d4480 | 17 | | | 40.0k | | |
| electromotivated | 0:0388385d4480 | 18 | | Tact (R)|--------vvv--------| | | |
| electromotivated | 0:0388385d4480 | 19 | | | 15.0k | | | |
| electromotivated | 0:0388385d4480 | 20 | | Switch (D)|--------vvv--------| | | |
| electromotivated | 0:0388385d4480 | 21 | | | 6.67k | | | |
| electromotivated | 0:0388385d4480 | 22 | | (L)|--------vvv--------|--------------------|AnalogIn | |
| electromotivated | 0:0388385d4480 | 23 | | | 2.5k | | | | |
| electromotivated | 0:0388385d4480 | 24 | | (C)|--------vvv--------| > | | |
| electromotivated | 0:0388385d4480 | 25 | | | | > 10k | | |
| electromotivated | 0:0388385d4480 | 26 | | (U)|-------------------| > | | |
| electromotivated | 0:0388385d4480 | 27 | | | | |--------- |
| electromotivated | 0:0388385d4480 | 28 | | (+)|-----------X --- |
| electromotivated | 0:0388385d4480 | 29 | ---------------- GND |
| electromotivated | 0:0388385d4480 | 30 | |
| electromotivated | 0:0388385d4480 | 31 | Theory of Operation: Each switch output pin is connected to one terminal of |
| electromotivated | 0:0388385d4480 | 32 | a resistor (each of different value). The other terminal |
| electromotivated | 0:0388385d4480 | 33 | is connected to a single analog in pin on the mBed. |
| electromotivated | 0:0388385d4480 | 34 | The mBed analog pin is also connected to a 10k resistor |
| electromotivated | 0:0388385d4480 | 35 | running to ground. This makes a network of seperate |
| electromotivated | 0:0388385d4480 | 36 | voltage dividers, between each switches output pin |
| electromotivated | 0:0388385d4480 | 37 | resistor and the single 10k resistor from the mBed |
| electromotivated | 0:0388385d4480 | 38 | analog in pin to ground. Since only one position can |
| electromotivated | 0:0388385d4480 | 39 | be active at a time only one voltage divider voltage |
| electromotivated | 0:0388385d4480 | 40 | is read by the mBed. Thus each switch position produces |
| electromotivated | 0:0388385d4480 | 41 | a unique voltage, at the analog in pin of the mBed, |
| electromotivated | 0:0388385d4480 | 42 | when active. |
| electromotivated | 0:0388385d4480 | 43 | |
| electromotivated | 0:0388385d4480 | 44 | NOTE: For proper operation resistor values should be as close as POSSIBLE |
| electromotivated | 0:0388385d4480 | 45 | to those shown in the circuit setup diagram! |
| electromotivated | 0:0388385d4480 | 46 | */ |
| electromotivated | 0:0388385d4480 | 47 | #ifndef FIVE_POS_SWITCH |
| electromotivated | 0:0388385d4480 | 48 | #define FIVE_POS_SWITCH |
| electromotivated | 0:0388385d4480 | 49 | #include "mbed.h" |
| electromotivated | 0:0388385d4480 | 50 | |
| electromotivated | 5:413df8008ce3 | 51 | enum ACTIVE_POSITION{NONE = 0, UP = -1, CENTER = -2, LEFT = -3, DOWN = -4, RIGHT = -5}; |
| electromotivated | 0:0388385d4480 | 52 | |
| electromotivated | 0:0388385d4480 | 53 | class FivePosSwitch{ |
| electromotivated | 0:0388385d4480 | 54 | public: |
| electromotivated | 2:a415472059c3 | 55 | /** Constructor for FivePosSwitch |
| electromotivated | 2:a415472059c3 | 56 | @param <pin> The AnalogIn Pin |
| electromotivated | 2:a415472059c3 | 57 | */ |
| electromotivated | 0:0388385d4480 | 58 | FivePosSwitch(AnalogIn pin); |
| electromotivated | 2:a415472059c3 | 59 | /** Get the currently pressed switch position |
| electromotivated | 2:a415472059c3 | 60 | @return The currently pressed switch position |
| electromotivated | 2:a415472059c3 | 61 | */ |
| electromotivated | 4:10c4b7cb4e8c | 62 | ACTIVE_POSITION getPosition(); |
| electromotivated | 0:0388385d4480 | 63 | private: |
| electromotivated | 0:0388385d4480 | 64 | AnalogIn _pin; |
| electromotivated | 0:0388385d4480 | 65 | static const float TOLERANCE = 0.075; // Allowable tolerance in evaluating for active switch |
| electromotivated | 0:0388385d4480 | 66 | // (0.075 will give a 0.05 deadband between posistions) |
| electromotivated | 0:0388385d4480 | 67 | }; |
| electromotivated | 0:0388385d4480 | 68 | |
| electromotivated | 0:0388385d4480 | 69 | #endif |
