Just thought I'd share this with the hope that someone else will find this heartening. (Warning: might be a bit long and waffling - its probably better not to read it, but I wanted to write it.)
About 3 years ago, I got my first arduino kit - It was thrilling, I had been playing around with pics and stuff, but the assembly was getting in the way of me creating stuff. As the arduino was based on C, and I had some experience with this, I was like a fish in water.
About ten minutes after making a few leds blink and getting an LCD work, I started wondering what I could do with it that could make my life a better one.
Then I thought - what about all my snakes and other pets? Thermostats are expensive in the petworld, coming in at £30 for a very simple single model, so I set to work.
I added feature after feature, and worked hard to ensure that the circuit was all mains safe.
I worked out ways of keeping time (accurate enough for me) and only switching relays one at a time.
Eventually I ended up with a lovely piece of kit that had the following features:
Arduino controlled relay box, with 10 outputs.
Umlimited temp sensors possible for each relay (instead of taking one sensors reading, they were averaged)
Each relay could be a thermal relay for heaters, or timed for lights
LCD screen for standalone mode and buttons for setting all variables.
Serial communication to allow for computer based data-logging.
All seemed to be perfect - or as near as I needed it to be - UNTIL:
I then set about configuring my pc to log the data I was now collecting in droves, but every now and then, the USB port would lock up - no data would come in for a few minutes, and usually when it did resolve itself, it was on another USB port... eg would switch between /dev/ttyUSB0 and /dev/ttyUSB1 (Im using linux).
I was at this time, using shell scripts to collect the data, and send time updates to the USB port, and I was limited in how precise a control I could get - but it worked (mostly), and I wrote a script to check which port was there and ran the appropriate script. The odd thing is that the arduino would work perfectly, without a reset through this - and commonly showed uptimes of 90+days, even though the USB port dropped 10-20 times a day (although sometimes it didnt drop once)
A friend at this point suggested that the problem might be due to electrical noise from the relays. I tried everything I could to clean it - opto-isolation (I thought it still leaked throught the mains wiring in the house) didnt seem to work, nor did putting all the seperate parts on different power supplies. There was also the strange factor that it seemed to happen regardless of relay states. ie it could happen when they were all off, or all on or anywhere in between. After a few weeks - I gave up, and accepted that as it worked, I would study on, and see if I could build a mkII without this problem.
As I have never written any executables for linux before, I thought that the next logical step was to get into practice, and write a C program to collect the data more reliably, [Note to self, NEVER program the serial port in C again] or so I thought, as the data came in, and would work fine, but in the way of all experiments - soon as you left it alone, it reverted to a mess of errors. I ended up running it once a minute and killing the previous one (if still running). This gave me a readable graph, without any messy gaps, but some of the data was still off every now and then (values transposed etc).
Then I started finding out about a component I had never really understood - The capacitor. What a cylindrical wonder this is, and I thought as I learnt more about power spikes, I thought that finally I had a fix.
Lots of values were tried, more data read, google started to fade from overuse, my wife laughed at me wanting datasheet sarnies for work... and to no avail.
Then one day I read that the arduino, rather than being C based as I assumed, was actually C++ based, so I thought why not have another crack at this problem, and at least try to clean the data a bit more before passing to my datalogger. Well, well, to my surprise, it was a LOT easier, and allowed for much finer control of the ports, so I decided to go right for it.
As I had become accustomed to the serial.print() debug method while playing with the arduino, I started using it in my C++ code as well (or the equivalent) and started to notice a few things about the data I was parsing:
Errors always seemed to occur while data was being transmitted by my time syncher script (I had forgotten about that - apart from knowing the time was right all the time), or errors also occurred when a read was not completed by the previous program before I killed it (as it would readock from time to time).
As I had never logged the errors in this level of detail, I was gobsmacked! I hastily rewrote my data collector program for my arduino with the following changes:
Only use 1 program to communicate with the arduino on each serial port.
Turn off data output on arduino before sending new times (as when time is updated, it confirms it).
Start each collecting session by enabling my arduino output.
I have now finally (just as I type this) had my first complete 24 hours of data without a single error or transposition. My serial port has remained static, and Im feeling a fool for not realising that the problem was pc software related, not arduino software/hardware related.
Lessons learnt from this:
- I am an idiot (I might as well say it as I know you lot are too polite)
- I wish I had started taking part on here earlier - someone may have helped me.
- Plan your communication better, and try to use standardised data packets with start/end and crc bits
- Stick at it, the solution will come in the end as long as you never give up and keep trying new things.
- Put the time in - its worth it.
- Glue is hot,
solder is too,
glue melts,
and now I know fingers do!
I am currently working through a backlog of fun parts I got from ebay to play with including:
RTC clocks, latches, LEDs, etc
I plan to try a day simulator with an array of RGB leds to provide my geckoes with natural colour for dawn/dusk and also to give a moonlight glow for them to hunt at night with.
What fun!