Jump to content

Arduino Applications and Programs


Simond
 Share

Recommended Posts

Dave, may I respectfully suggest for the future that you keep the code in loop() to a few lines (3 to 6, perhaps) and move all the content into separate functions to make the code easier to follow. For example

void loop() {
   readButtons();
   readSwitches();
   setupServos();
   moveServos();
}

...R

Link to post
Share on other sites

  • RMweb Premium

It all comes down to style, preference and the amount of time you spend. I tend to be a straight down the page sort of guy when it comes developing prototypes but the code could most certainly be improved.

 

Cheers

Dave

Link to post
Share on other sites

Dave

 

Thanks for the post - few tricks in there, some of which I may well shamelessly copy.

 

 

Robin

 

I'm only just getting my head around the concept of functions.

 

In the days when I programmed in compiled HP basic, and HPGL, they used to be called "subroutines", I think... :)

 

I quite like the approach - more learning to do!

 

Best

Simon

Link to post
Share on other sites

Dave

 

Thanks for the post - few tricks in there, some of which I may well shamelessly copy.

 

 

Robin

 

I'm only just getting my head around the concept of functions.

 

In the days when I programmed in compiled HP basic, and HPGL, they used to be called "subroutines", I think... :)

 

I quite like the approach - more learning to do!

 

Best

Simon

 

Unless things have changed, a function returns a value whereas a subroutine doesn't.

Link to post
Share on other sites

Rod

 

It was a very long time ago, 1986 or 1987 from memory, so indeed things may have changed, but I am pretty sure the subroutines I wrote had return values, and the Wikipedia article on subroutines supports this, suggesting I'm not senile yet!

 

http://en.wikipedia.org/wiki/Subroutine

 

I have heard the differentiation you have stated, but I'm not sure if this is general

 

Best

Simon

Link to post
Share on other sites

I'm not good on the exact terminology, I call them all functions in my Arduino code. The ones that start with void such as

  void loop() {
  }

don't return values and if they return a value they must be defined with the type of value they return, for example 

byte myDemo() {
    return(65);
 }

...R

Link to post
Share on other sites

I've only seen the distinction made in BASIC. My C64 had functions which returned values and I think had to fit on a single line, and subroutines which were just reusable bits of code invoked with GOSUB and exited with RETURN.

 

No other language I've used has that distinction. A function may be declared to return a value or not, and if it is declared to return a value it must do so in all cases, and if declared to not return a value, it may never do so.

 

So call them subroutines or functions - it doesn't matter.

 

The only distinction anyone worries about these days is that of functions vs methods in an object oriented language (and Arduino is programmed in C++ which is one of these), and even then most developers don't care because it's usually obvious from the context in the conversation.

 

For interest's sake, in OO languages like C++ a function is code which can be called but is not associated with any particular class, whereas a method is exactly the same as a function, but defined within a class and has an implicit pointer to the class it belongs to if it isn't a static method. Some OO languages don't have functions at all, like Java. But we still sometimes call them functions when we're thinking about other things.

 

All of which is way more than you need to know to write cool Arduino programs as we saw with the above signal controller.

 

Regards,

David.

  • Like 1
Link to post
Share on other sites

  • 2 weeks later...

Although I have moved on from DCC to battery powered radio control (BPR/C) I have been following the Thread about a DCC controlled turntable because of my interest in the Arduino microcontroller system. I still have a Hornby Elite DCC controller.

It seemed to me that the code concerned with getting the data from the DCC system was unnecessarily complex so, just out of curiosity, I started to investigate it further. I downloaded the DCC library from mynabay.com and was disappointed to discover absolutely no documentation apart from a couple of example sketches - one of which has been incorporated in the turntable code. (If there is documentation elsewhere that I have overlooked, I apologize). And I didn't make much headway trying to understand the internals of the library.

I then found another DCC decoder library at http://mrrwa.org which was easier to figure out, although also written in a style that may appeal to dyed-in-the-wool C++ programmers. And, like the mynabay code it included some additional functionality that, for me, obscured the basics.

I have now written a program that uses the pulse detection technique from the mrrwa library in a way that, I hope, will be easy to follow for anyone that is interested in what goes on inside the "black box". The code simply shows the DCC packets in the Arduino Serial Monitor and it should be a simple matter to extend the code to make use of the packets to control stuff.

I have divided the code between 3 files as I think it makes it easier to follow. All the files should go in a directory called ReadDccPackets which is the name of the main Arduino sketch. The Arduino system loads the othe .ino files in alphabetical order which is why the file names begin with A_ and B_.

The overall approach of the program is as follows ...
The file B_dccDetectBits.ino has the code that detects the data in the DCC signal and it stores the bits into a circular buffer that can be accessed by the code in A_dccGetPacket.ino.

The system for detecting bits is actually very simple. In the DCC system a 1 is represented by pulse with a half-wavelength of about 58 microseconds and a 0 is represented by a pulse with a half-wavelength of about 100 microseconds. The code uses an interrupt to identify the rising part of a pulse and then takes a reading of the pin 80 microseconds later. If the pin is still high the pulse is a 0, otherwise it was a 1.

The code in A_dccGetPacket.ino draws from the buffer containing the bits and examines them. Each packet comprises a preamble followed by an address byte, some data bytes and a checksum byte. The code saves the packet where it can be accessed by the code in ReadDccPackets.ino.

I have actually described the process backwards. In reality the process begins with by the code in ReadDccPackets.ino which starts the continuous process of detecting DCC bits (beginBitDetection()) and calls for a packet to be extracted (getPacket()) and then displayed (showValidPacket()) in the Serial Monitor. If you want to see all of the packets the function getPacket() must be called sufficiently often - but I haven't tested how often. If data is not drawn from the buffer containing the bits it will fill up and then additional bits will be lost until the getPacket() function draws more bits from the buffer to make space for new ones.

The physical mechanism (using an opto-isolater) for connecting the DCC signals that is described in the Turntable should be used to connect to Arduino Uno pin 2. I didn't have an opto-isolator so I made a crude breadboard circuit using a diode and a voltage divider. However I didn't have any train motors running which might cause higher voltage pulses and the opto-isolator would be a safer way to isolate the DCC from the Arduino.

I am presenting this for the benefit of anyone who might share my curiosity for what underlies the DCC library.

 

Enjoy or ignore as it suits you.

 

...R

ReadDccPackets.ino

A_dccGetPacket.ino

B_dccDetectBits.ino

  • Informative/Useful 1
Link to post
Share on other sites

  • 2 weeks later...
  • 1 month later...

Hi All

 

A great thread, thanks for all your posts. I have only recently got an Arduino UNO and have miles to go to catch up with you all, at the moment my greatest success is to get an LED to fade up and down.

 

Keep up the posts, they are all very interesting.

 

All the best

 Martin

Link to post
Share on other sites

  • 2 weeks later...

Martin

 

Post the code!

 

Another newby will follow your footsteps, and learn from it, and maybe one of the experienced types will say "oh, but if you do... It'll be better/simpler/cheaper/quicker" and we'll all learn!

 

Best

Simon

  • Like 1
Link to post
Share on other sites

  • 1 month later...
  • 4 weeks later...

Dave

 

Thanks for posting this - I've been working on lots of non-Arduino things for the last few months so eye off the ball!

 

 

I presume these are the files (from post 80)

 

https://skydrive.liv...ANFaOxzOQgfUx0I

https://skydrive.liv...ANB2K9orPIVwORc

https://skydrive.liv...AGrKuRh4uU7kjO8

 

There's a wealth of info in that thread

 

Best

Simon

Link to post
Share on other sites

  • 3 months later...
  • 2 months later...

 

Hi Simond - thanks for the links. I'm just wondering (since I haven't used Arduino yet, but am an IT geek) - is the ATMega2560 board in the last link really all you need for a programmable I/O solution via the Arduino spec? Then you download the free programmer software, connect up and you're away? If that's true, that's pretty good value for 10 quid, particularly as it even includes the USB cable!

 

Thanks,

Alan

Link to post
Share on other sites

Hi Alan

 

I bought a genuine Arduino complete with USB cable, I already had a few diodes, a servo or three and dome other bits & bobs, and spend a happy week over Christmas 2012 playing with it. You'll see my signals in the Porth Dinllaen thread as an example, and more in the bits & pieces for turntable operation, for which I bought a stepper motor and a stepper driver board (after frying an H bridge!)

 

I haven't tried the clones myself yet, but am assured that they work identically. Yes, they're decidedly cheap.

 

The programming is easy if you have any background, and the device appears thoroughly robust.

 

I will get round to some more work on interlocking at some point, but the work Fabrice did seems much in line with my initial thoughts, so I may simply copy his program and modify to suit.

 

HTH

Simon

  • Like 1
Link to post
Share on other sites

  • 1 month later...

Thanks Simon - that's really useful for future reference. My current layout build is for a small shunting plank, so unfortunately a turntable won't be included soon. :-/

 

However - I've picked up my first Arduino (Uno) and got the initial 'blink' LED project working, so I'm away, although progress might be slow for a few weeks due to work. I've picked up a few small servos as well, so might have a go getting those working over the holiday.

 

As a matter of interest, I discovered the following Adafruit servo driver board, which will power up to 16 servos (or many more if chained with more boards), with control selection provided digitally via a few outputs from an Arduino or Raspberry Pi:

http://www.modmypi.com/raspberry-pi/breakout-boards/adafruit/adafruit-pwmservo-driver-16-channel-12-bit-pca9685

 

Would this make sense (I probably need about 9 point servos and more for signals), or is it better to just use an Arduino Mega or similar with one output per point/signal from the Arduino?

 

Thanks,

Alan

Link to post
Share on other sites

Alan

 

Thanks for the link. I haven't been playing with the Arduino for a while, so not keeping up with developments, but this looks like an ideal way of increasing the outputs, which may well offer a cost-effective way to indicate a "locked" lever on a frame.

 

That it will drive 16 servos directly is a real bonus, as is the I2C connection. Would allow local drive to servos, hopefully avoiding the jitter issue, whilst keeping the Arduino near the control panel.

 

Certainly offers some potentially useful options

 

Best

Simon

Link to post
Share on other sites

Just had a look at  the Adafruit  for myself looks like a good option but if your looking at costs then the Nano or Pro mini would be a better option.

Can not seem to pick the Adafruit for less than a Tenner but you can pick up Nano's for about £2 pounds and Pro mini for less.

 

 

Alan

Link to post
Share on other sites

Alan

 

I guess there's probably not much in it when you add in the extra gubbins, and time, to connect 16 servos and power supply to a clone.

 

I do like the idea of it being a node to connect 16 outputs to two wires which then go back to the control panel, thus keeping the servo wires short.

 

Best

Simon

Link to post
Share on other sites

Alan - I agree with you on cost - the 'clone' Arduinos seem to be very cheap, and for my limited requirements, I might just upgrade to an ATMega instead. If I can afford it, I would try to get an 'official' version, however, to try to support the Arduino project as much as I can.

 

I think on reflection, the potential advantages of servo boards increase with a larger layout. If you had 80-odd points and signals to control, I'm guessing it would probably be a better investment to have one Arduino for control and 5 'slave' Adafruit servo boards, even if it initially costs slightly more, since you only have to update code / logic in one place (and hopefully accesssible!).

 

The 'MegaPoints' solution developed by Dave Fenton costs £50 per custom ATMega328-based board, which supports 12 points/signals (http://loolee.org/megapoints/). He has put a lot of thought and work into his solution in the last year, which now includes a DCC module and Mimic-type panels to control and display point operation.

Even so, £50 per 12 points (excluding panels and servos) would soon mount up for a large layout, particularly considering the ATMega328 chip (also the 'brain' of many Arduino boards) costs under £2. So again, the use of purpose-built servo boards compares well in this case, in cost too, assuming that the modeller is prepared to program an Arduino board / ATMega chip appropriately.

 

 

 

Just had a look at  the Adafruit  for myself looks like a good option but if your looking at costs then the Nano or Pro mini would be a better option.

Can not seem to pick the Adafruit for less than a Tenner but you can pick up Nano's for about £2 pounds and Pro mini for less.

 

 

Alan

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...