root / Ports / examples / dcmotor_demo / dcmotor_demo.pde
History | View | Annotate | Download (1.1 KB)
| 1 | //>>> The latest version of this code can be found at https://github.com/jcw/ !! |
|---|---|
| 2 | |
| 3 | // Demo for the DC Motor Plug |
| 4 | // 2010-11-18 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php |
| 5 | // $Id: dcmotor_demo.pde 7763 2011-12-11 01:28:16Z jcw $ |
| 6 | |
| 7 | #include <Ports.h> |
| 8 | #include <RF12.h> // needed to avoid a linker error :( |
| 9 | |
| 10 | PortI2C myport (1 /*, PortI2C::KHZ400 */); |
| 11 | DeviceI2C expander (myport, 0x26); |
| 12 | |
| 13 | enum {
|
| 14 | MCP_IODIR, MCP_IPOL, MCP_GPINTEN, MCP_DEFVAL, MCP_INTCON, |
| 15 | MCP_IOCON, MCP_GPPU, MCP_INTF, MCP_INTCAP, MCP_GPIO, MCP_OLAT |
| 16 | }; |
| 17 | |
| 18 | static void exp_setup () {
|
| 19 | expander.send(); |
| 20 | expander.write(MCP_IODIR); |
| 21 | expander.write(0); // all outputs |
| 22 | expander.stop(); |
| 23 | } |
| 24 | |
| 25 | static void exp_write (byte value) {
|
| 26 | expander.send(); |
| 27 | expander.write(MCP_GPIO); |
| 28 | expander.write(value); |
| 29 | expander.stop(); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | void setup () {
|
| 34 | Serial.begin(57600); |
| 35 | Serial.println("\n[dcmotor_demo]");
|
| 36 | Serial.println((int) expander.isPresent()); |
| 37 | exp_setup(); |
| 38 | } |
| 39 | |
| 40 | void loop () {
|
| 41 | // turn each motor in either direction for one second |
| 42 | for (byte i = 0; i < 4; ++i) {
|
| 43 | exp_write(1 << i); |
| 44 | delay(1000); |
| 45 | exp_write(0x0); |
| 46 | delay(1000); |
| 47 | } |
| 48 | } |