Forum 2005-2010 (read only)
Topic: Generic controller
(Read 780 times)
Apr 01, 2010, 09:42 pm
Hm, looks like my sketch won't fit; barely squeezed it into an old Diecimilia I had laying around... Anyhow, I got tired of writing and running custom servers to interface flash with arduinos on com ports so I bought one of the ethernet shields. I also saw patterns to what most installations need and have available:
-power and network is usually not a problem
-installations usually have 1 or 2 buttons, 1 or 2 led's, and (possibly) 2 encoders
I added support for 2 analog ins, too, but since they float these are off by default.
I know that the application's description is a little daunting, but what I'm hoping is that once folks figure this thing out they can use it for many installations. There's also lots of neat snippets in the sketch, so maybe it has some functionality you want and lots that you don't. Feel free to recycle code.
Anyhow, with this application you have 2 interrupt-driven encoders on pins 2/4 and 3/5; these can be run in relative mode, where it reports any non-zero values in the last 1/30 sec, or in absolute mode, where it reports the current value every 30th sec and has an adjustable lower and upper limit. You also have 2 analog inputs on a0 and a5 (split as far apart as possible to minimize crosstalk in wires) and 4 input/output/input-outputs on pins 6 through 9. In input-output mode the pins are usually in output mode but shift periodically to input and then grab the value. If you're driving an LED with the same pin the shift is imperceptible, the option is there for those people that are running out of pins. If you're in output mode it will just stay as output so that's probably a better idea if you're, say, driving a relay...
Values are written to nvram; if you give it a new ip or change pin modes or output values and then unplug it and plug it back in the changed values will be read in and utilized. Set it up for your installation and then read away.
2 tcp servers run on the arduino's ethernet shield: one always at 23 (default telnet) and one configurable to the port of your choice (defaults to 8910). This is useful if interfacing with flash software, where ports below 1024 can sometimes be a problem. Changes to ip/subnet/port don't take effect until after a restart; I played a bit with watchdog and a while(1) loop but it wasn't reliable, so just press the reset button after you make the settings changes that you want.
Details (also available in the .pde comments):
2: encoder 1 a
3: encoder 2 a
4: encoder 1 b
5: encoder 2 b
6-9: io's 1-4
a0: analog in 1
a5: analog in 2
the following respond with their current value as [parameter]:[value] :
ip (ip address), sn (subnet), mac, port, e1 (encoder 1), e2 (encoder 2), 1 (io1)-4 (io4), a1 (analog 1), a2 (analog 2), ? (reports lots of stuff in a comma-separated list)
In addition, you can write values as [parameter]:[value] :
ip, sn; example: "ip:10.1.5.33"
mac; example: "mac:0a0b225c12"
port; example: "port:7745"
encoders have lots of parameters; e1 reports value, e1l:-2000 sets the lower absolute limit to -2000, e1h:2010 sets the upper absolute limit to 2010, e1r puts encoder 1 in relative mode, and e1a sets encoder 1 to absolute mode. Same applies to e2 for encoder 2.
1i sets io1 to input only mode, 1o for output only mode, 1io for input/output mode. 1l sets io1 to low, 1h sets it to high (only matters if you're in output mode). Same applies to 2 through 4 (the other io's)
"aon" turns on analog in reporting, "aoff" turns it off. default is off because I hate float.
"on" turns on unprompted reporting; you'll get button presses/releases when they happen and encoder values and (if on) analogs every 30th of a second. "off" turns this off, so you'll have to query for what you want using "1" or "e2" or something like that.
If you're using more than one of these on a network you will (obviously) need to give each a unique ip and (more subtle) may run into problems if you don't give each of them a unique mac address.
Download is available at http://rshields.com/arduino/ (the described script is ethShowController.pde). Feedback appreciated, post as a reply here. And if you really mess up your settings try messing with the "signature check" in readWriteNVRam(), which will force it to go back to 192.168.0.50/255.255.0.0/port 8910, with all io's in input mode.