simond

Western Thunderer
I doubt it’s beyond you or indeed any of us, but where I would agree is that inevitably there is a simple line of code an example of which one cannot find anywhere and on which everything depends.

Then, after much googling…
 

michael080

Western Thunderer
What I am trying to say is, when I wanted to add some new functionality for my own Arduino projects, and found that I had to write an interrupt handler and alter the PWM frequency to drive a motor, I met up with some cerebral end stops.
Fully understand what you mean. Arduinos are nice devices as long as you stay within its framework. If you need anything interrupt driven, it may be easier to download the microchip studio and program directly on bare silicon. The MC-studio has a nice library of examples. However, for interrupts, I recommend the corresponding atmega328P manual .
But then, this might be daunting for many of us.

Michael
 

76043

Western Thunderer
I successfully tried out the predecessor to DCC-EX, DCC++ as I had an Arduino kicking around. I recently looked at it again to see how I could make it work with an Android phone as a throttle and found I need to upgrade to an Arduino Mega, with a WiFi module. All well within my cheapskate budget, but then I realised I had a twin track panel mount Gaugemaster feedback controller and a power supply staring at me and thought, "use what I have in stock".

But DCC-EX if it works as easy as DCC++ is a no brainer and a fraction of the price of a bells and whistles DCC system. I'd definitely use this for my potential O gauge system.
Tony
 

Richard Gawler

Western Thunderer
Arduinos are nice devices as long as you stay within its framework.
This is a moot point, and I know now it is where I kept going wrong. I would find some example code, and try to add to it. The example code had invariably been created by someone who knew the limitations, and it was me who went blindly off to find myself floored by the string handling (in the C++), and the lack of floating point implementation (in the C) and so on.
 

Stuart Birks

Active Member
There is a trend among programmers that give code examples to give an example that demonstrates as many of the features of a function as possible, making it difficult to extract the simple bit of code that you really want. This is not limited to Arduino code but everything, C, Java, Python and everything else. Just trying to copy a bit of sample code without understanding what is going on is doomed to failure. You need to understand what is going on as there may be header files to include, variables to define and initialise and then know what variables to pass to a function.
This is NOT difficult and easilly witin the abilities of most people BUT it is essential to get an understanding of how things all fit together. Just getting a program to compile does not mean it will run. If it does run it does not mean it will do what you wanted and especially when working with an Arduino the code might be correct but there may be a problem with whatever you have connected it to. There is no quick fix to any of this and if you want to persue using an Arduino (or any of the other micro controllers, they will all have the same problem) then you have to be prepared to spend many frustrating hours puzzling through reference pages. The Arduino language reference Arduino Reference - Arduino Reference is very good at explaining functions but no good at all in helping you decide which one to use in the first place.
You will also find that if you buy various bits of electronics and especially shields (expansion boards that plug directly into an Arduino) then the vendor will often give a link to a library of code to operate their hardware.
That is a long winded way of saying an Arduino is not easy but very rewarding when you finally do get it to do what you want.
To try and reply to a few comments made.
Interrupt processing is straight forward but think hard about do you really need it. Most things can be handled within the normal code loop.
C is famous for its total absesce of string handling and anything to do with strings is difficult, usually ending up with indexing through a character array. Like everything else its ok when you understand it.
If you have a Pentroller or Gaugemaster controller that you like, go for creating an electronic interface. Then the output code from the Arduino is as simple as analogWrite(pin, value);
Personally I can see nothing that I would ever want to communicate with an Android device although I am sure many would disagree with me. Give me a mechanical switch or potentiometer any day.
Finally, understand what an Arduino can and cannot do so you can decide when it is an appropriate solution and when it isn't.
Stuart
 

Richard Gawler

Western Thunderer
Well, I bought an Arduino starter kit during the lockdown so I could have a go at making a self-driving car. Not quite as ambitious as it sounds because it was Magnorail-based, but I did want it to start and stop, accelerate and decelerate, and reproduce oversteer and understeer. I also wanted a dashboard display to show what was going on, with a speedometer and lap counter. And it all worked out fine in the end, sketch attached.

But (a very big but) I found it all incredibly wearying. I finished it feeling the dashboard display only existed for debugging, and the whole thing was really a very laborious script not a properly responsive and portable program. I think, the whole structure of the code should be based on a much smaller loop with interupts arriving from the position sensors (the four reed switches) telling the motor (moving the car) to change speed and the servo (over/understeer) to move the chain.

The code does set up the Arduino to drive a Magnorail motor, and new LCD characters with true descenders, and maybe these are reusable to folk here. It all works nicely, but it ended up too hardware-specific for my liking - it only works because I have arranged the time spent changing chain speed and moving the servo to not interfere with reading the next reed switch.

A part of me says, this would be a lot easier with multiple Arduinos, one for the motor, one for the servo, one checking for switch trips. A person with the necessary experience might see these as objects well within the processing capacity of a single Arduino; and there are I/O pins spare; but somehow the fun just seeped away and I never brought myself to do the scenics.

Clyde Humphries, the proprietor of Magnorail Oz, made some videos using my footage which are quite fun, for example:

Whether the Arduino was or wasn't the appropriate approach to this, well I'm not sure; but I suspect it could be, in more experienced hands. This was my first go with C and C++ . . . so I have had a go, but never again confused.png

Edited as underlined to try to clarify.
 

Attachments

  • MagnorailProcessor_v0.8.ino
    38.5 KB · Views: 3
Last edited:

Richard Gawler

Western Thunderer
Finally, understand what an Arduino can and cannot do so you can decide when it is an appropriate solution and when it isn't.

I think "ability" comes into the assessment too. I found it a doddle to find the code to make an Arduino move a servo, and straightforward to adapt the code to suit my own servo. The display was easy too. Controlling the motor speed while keeping the motor quiet was harder, as was detecting the various reed switches and the control button. But making the code non-blocking so it could respond to these five inputs and update its various outputs in a timely manner, while keeping track of everything which was going on, just felt like standing at the bottom of a vertical cliff looking upwards. Hence my thinking of my code as being like a rather lengthy script.

My gut feeling is, a microcontroller was a better choice than something with an operating system for me, because it took me back to the 6800 and the assembler I learnt about at university and I had something from there to build on. But maybe you would nudge the new user, especially the hobbyist wanting to make something original (not a rework of someone else's project), towards a different platform or a different language?
 
Last edited:

Stuart Birks

Active Member
That is very impressive and you clearly have some programming experience and ability. Near the start of this thread Timbersurf gave a link to Rudys Model Railway channel here Rudys Model Railway It is the best introduction to Arduino programming I have come across. You mention the problem with functions blocking the code. In episode 13 he describes the use of the timer function millis() which can be used to prevent functions locking the program. You also say you have problems with motor noise from the PWM signal which is very common. Did you try just running it on simple DC just varying the voltage. It might have been a simpler solution. If you want to move the servo in a gentler way there is a library providing a function for variable speed servo movement. I used this on my signal arm movement for the arm bounce movement.
An Arduino is a good platform for this type of work. I have zero experience of other platforms so I would not be able to suggest an alternative.
 
Top