Jump to content
 

Arduino Applications and Programs


Simond
 Share

Recommended Posts

Ok, having been the programming equivalent of a mupppet, I now have my initial programs working, with a lead length of about 400mm. I’ll spare me my blushes, and spare you the gory details, but I put a “for” loop in the wrong place. :(

 

post-20369-0-09880900-1547410078_thumb.jpg

 

What you can see atop the typically chaotic bench is a Mega, connected to the lappy on the left, which is simulating the switches & buttons on the control panel, and a 16 relay board with a Nano-clone fitted under the veroboard at the near end. You can see 4 lit red LEDs showing channels 1 & 2 are on, ie the relays are energised, thus the Tortoises would be reversed.

 

The Mega and the Nano are connected by the pink & white wires (i2c) and the black. The nano is currently plugged into the other lappy, which is powering it, and providing a serial monitor to enable me find my mistakes!

 

Next step, create a long, three-core wire to test comms over the necessary distance. At this point, I’ve just spotted an opportunity... there’s a 5V regulated supply fitted to the veroboard, and it has the capacity to power the Mega when it’s installed in the control panel, that saves a few quid and a bit of faff in the control panel itself. Should have though of that earlier, still better late than never :)

 

Ok, quick & dirty test wire made, about 600mm longer than the umbilical

 

post-20369-0-62972300-1547412198_thumb.jpg

 

Plugged in, and working...

 

post-20369-0-01230200-1547412324_thumb.jpg

 

This is not an ideal test, as the capacitance of the real cable will be higher than the three loose wires, but it gives me sufficient confidence to proceed with the i2c approach, particularly knowing that the Tx/Rx serial fallback will still be feasible if it’s not reliable.

 

Right, Next step, get the second Nano working with the MOS modules.

 

post-20369-0-99335400-1547412785_thumb.jpg. These were another cheapo eBay purchase, looked like less hassle than building my own. They’ve got a couple of amps capacity each and will be used as individual dimmer channels.

 

Progress...

Best

Simon

  • Like 3
Link to post
Share on other sites

It’s strange. I mean, computers are really, really good at doing the same thing over and over again, predictable, reliable, boring even, so, if it worked last week why, oh why doesn’t it work today?

 

I have a genuine Nano, and it will not load a sketch, I get the “avrdude: ser_open(): can't open device "\\.\COMx": Access is denied.” error. I see the posting on this topic on the Arduino website has been read 237657 times, which suggests I’m not the first to suffer this irritation. It’s staggering that with nigh on a quarter of a million problems and there is no “a), b), c)” fix.

 

From what I have read, it appears to be a windows issue, whoopee. Perhaps the computer updated itself.

 

I have checked and rechecked, and changed COM ports, restarted the computer, updated drivers, checked & confirmed that the Nano will boot on another laptop, and generally got to the point of throwing it all in the bin and taking up knitting or football. It’s bloody annoying.

 

:(

Simon

Link to post
Share on other sites

It’s also bloody confusing. I have just uploaded a sketch to the Nano, using the other laptop, which flashes the led on pin 13, and runs through a loop for (int x =0; x<= 255; x++) { which simultaneously serial prints x, and digital writes x to a PWM output. This works exactly as expected on the other laptop. The MOSFET module is plugged into the PWM output and has a red led to indicate its function.

 

Most confusingly, it works on the first laptop too. I have plugged the usb lead out of the second laptop and into the first, and I can watch the numbers changing on the serial monitor in time with the pwm controlled led, and the flashing led. SO THE COM PORT IS WORKING. This is beyond doubt. It also confirms that the usb lead isn’t faulty.

 

But I can’t load the program from the first laptop to the Nano, which suggests to me that there is either a driver problem (unlikely given the functioning COM port) or an issue with the IDE (seems improbable, but struggling to see a better alternative).

 

Growl, snarl, grumble, complain, whinge, whine, mutter...

Link to post
Share on other sites

Took the opportunity to update the Arduino IDE to 1.8.8...

 

Right. That fixed that. Wasted evening, but now working. I have no idea why something changed, my suspicion is that the laptop updated its os, and something somewhere didn’t link up. Ho hum.

 

Maybe tomorrow the second slave will get written, then I can try i2c over the long wires to two slaves and if that works, I’ll have a go at the turntable.

 

At least I can go to bed with a smile!

Best

Simon

Edited by Simond
  • Like 1
Link to post
Share on other sites

  • RMweb Premium

Worth checking you are using the correct Nano definition in the IDE. There have been changes to the bootloader so now there is an option in the later IDE versions to select "... with old bootloader" for used with older Arduino boards.

 

Cheers

Dave

Link to post
Share on other sites

It is perfectly possible to have several versions of the Arduino IDE on the same PC if it is necessary to maintain compatibility with older programs you have written. Unfortunately the Arduino developers don't pay much attention to backwards compatibility.

 

...R

Link to post
Share on other sites

Not much to show but now have the dimmer slave working.

 

(Not the more stupid unpaid worker, the Nano that will control the lighting brightness...)

 

Tomorrow, I might get the master program reworked to drive both, but I’m imagining that I might spend more time sorting out a relatively unimportant serial simulation of the switches than doing them properly, so it would probably be wise to simply install the Mega in the control panel, and connect the actual switches, buttons, bells, whistles etc.

 

No going back at that point...

 

Probably Saturday.

 

A bientot

Simon

Link to post
Share on other sites

We have a control panel... well, nearly...

 

post-20369-0-22741700-1547935056_thumb.jpeg

 

The Mega has been installed by the simple expedient of trimming the transparent plastic base, and cutting a hole in the back panel for the USB socket. The Mega base was then screwed down to the base of the housing, and the Mega clipped back in.

 

The rainbow ribbon cables came from a roll I bought when doing the Greater Windowledge, reuse, recycle, repurpose... there is a bundle of wires which I left soldered to the 25-way D connector, tied up.

 

The ones that are used are

 

4 wires for the Lenz X-bus. These go to a socket on the back of the unit, and another on the inside. My LH100 will be plugged into the inside one, and it’s cable will be stored within the panel, as the LH100 is just about permantly mounted on the panel.

 

4 wires for the i2c connections, +5V, GND, SDA & SCL. These are connected to the appropriate pins on the Mega.

 

4 wire, 2 pairs, which will bring the programming track outputs onto the back of the panel. I was sure I had a pair of RS 4mm sockets in the box, but I can’t find them, so will have to order a pack. The programming track is the headshunt and set-back siding along the front of the layout. It’s fed from a relay interlocked with the crossover that links it to the rest of the layout, so if the switch is set, and the points are correct, it switches from normal feed to the programming feed P&Q.

 

The other three blocks of cable to the Mega are

 

6+GND for the turntable buttons, (small circuit board lying lower left in the lower half of the panel)

8+GND for the point switches, and

6 for the Lighting switch (2 ways), the Programming relay switch, the Programming active LED, and a 2-colour LED to indicate that the turntable is, or is not, aligned with the desired exit road. These share the point switch ground.

 

Need to get the 2-colour LED, and swap it into the bezel of one of the red ones.

 

All the switches and LEDs are common ground, so the Mega will be set to Input PullUp. I’ll debounce the buttons but doubt it’s necessary on the switches.

 

12 wires will handle 8 point motors, 5 lighting channels, a six position turntable, and the DCC X bus, and allow me to plug the rolling road in too.

 

post-20369-0-56388800-1547935105_thumb.jpeg

 

The front panel will acquire a track diagram, laminated. I may replace the aluminium with some 3mm pvc, as it’s got quite a few extra holes in now. I need to make a carrier for the small circuit board with 6 push buttons, I’ll laser it from 3mm acrylic, so the buttons can be mounted on the back of the front panel. I’m going to turn up some plastic spacers to pull the switches back into the panel so it looks neater.

 

I’ll also 3D print a holder for the LH100 to mount on the side of the unit, rather than on top as it has been for the last ten years or so. Move with the times!

 

post-20369-0-28377700-1547935125_thumb.jpeg

 

Back panel, with 25-way D connector, USB socket, 5-pin DIN socket for X bus, remote reset button for the Mega, and two holes for the missing 4mm sockets.

 

I’ll test it and program the Mega over the next few days, but a pleasing chunk of progress today, I think.

 

Atb

Simon

Link to post
Share on other sites

Rock & roll!

 

post-20369-0-30591000-1548455296_thumb.jpg

 

It works on the points relays and the lighting controller.

 

You can see that levers 3 & 7 are pulled and the lights on the relays (in pairs) on 3 & 7 are out - I will reverse that so the relays are on when the levers are pulled, but that’s a trivial change.

 

You can also see a dim red led on the red board next to my celebratory whisky glass (it is Burns night) which is a result of the light switch, (middle right on the panel) being pulled.

 

That is an on-off-on 3-position switch. In the upper position, pulling a point lever selects a lighting channel to adjust, which is done using the turntable 9 o’clock and 3 o’clock buttons to dim and brighten the lights.

 

Both the points and lighting are working via the 2-metre umbilical from the control panel, so the i2c is ok, so far.

 

Next step is to re-do the turntable program, to incorporate the i2c comms, and to integrate the “release” function which stops it whining & buzzing.

 

But not til next week, off to Brzzl tomorrow, and the BOGG show on Sunday.

 

Best

Simon

  • Like 1
Link to post
Share on other sites

Nice to see progress.

 

Rather than going to all the trouble to make a neat control panel I prefer to put all the switches on a PC screen. 

 

Makes it much easier to correct my inevitable mistakes and changes of mind :)

 

...R

Link to post
Share on other sites

Thanks Robin

 

That would certainly be a solution, but I like levers (aka switches) and I spend my days looking at computer screens!

 

I established this morning that it all works without a computer connected.

 

Best

Simon

Link to post
Share on other sites

  • 2 weeks later...

A4899 driver data sheet here 

 

http://html.alldatasheet.com/html-pdf/338780/ALLEGRO/A4988/2974/10/A4988.html

 

current state of play;

 

* i2c comms working to and from three slaves via 2 metre umbilical.

 

* point control working with relays, inverted sense of levers, so the relays are not energised unless levers are pulled.  Have established how to control Tortoises with a single Arduino pin, will buy the bits and prototype it when they arrive.  Relays are fine, but the board is huge, and the current consumption rather higher that the Tortoises they control!

 

* lighting dimmers - 5 independantly adjustable channels working.  Bit clunky.  Development required.

 

* turntable - it was already working, but needed a little tweaking.  I’ve got the comms working both ways, so the panel tells the turntable what to do, and then polls it for its position until it “arrives” at the demanded road.  

 

I now need to get to get the sleep/disable functionality working to stop the whistling/whining, and to modify the base program to include the comms functionality.  Hope to have that installed before the weekend.

 

I was looking at the i2c control panel and display on the Arduino turntable thread, which might offer a less clunky way of managing adjustments to lighting and turntable road positions.

 

https://www.rmweb.co.uk/community/index.php?/topic/78578-dcc-controlled-peco-turntable-project-using-a-arduino-uno/&amp;do=findComment&amp;comment=3024245

 

I also need to get the relay for the program track switch over sorted.  Currently electrically interlocked with the points, might simply move to a software interlock.  

 

All in all, it’s been quite fun, though I’d like to do a bit of modelling for a change, but I can’t stop this til it’s done!

 

best

Simon

Edited by Simond
  • Craftsmanship/clever 1
Link to post
Share on other sites

Houston, we have a problem...

 

it should be 19912 steps to complete a single turn, given the gearbox and a 1.8 degree step motor.

 

it is, plus or minus.   However, plus or minus; I seem to have a distribution of the number of steps from around 19895 to 19920 or so.

 

I'm measuring it using my phototransistor and the piece of white plasticard glued under the bridge, which is my “zero point”.  I wrote a quickie sketch to count the steps for a turn, and repeat 9 times.  

 

i can post the code if anyone’s interested, but I don’t think it’s important.  If anyone can suggest any systemic reasons why this variation might occur, I’d be very interested to discuss them, and experiment to find out more!

 

Naively, perhaps, I rather thought I’d get the same number of steps over and over again.

 

til tomorrow,

Simon

Edited by Simond
Link to post
Share on other sites

8 hours ago, Simond said:

Houston, we have a problem...

 

it should be 19912 steps to complete a single turn, given the gearbox and a 1.8 degree step motor.

 

it is, plus or minus.   However, plus or minus; I seem to have a distribution of the number of steps from around 19895 to 19920 or so.

 

I'm measuring it using my phototransistor and the piece of white plasticard glued under the bridge, which is my “zero point”.  I wrote a quickie sketch to count the steps for a turn, and repeat 9 times.  

 

i can post the code if anyone’s interested, but I don’t think it’s important.  If anyone can suggest any systemic reasons why this variation might occur, I’d be very interested to discuss them, and experiment to find out more!

 

Naively, perhaps, I rather thought I’d get the same number of steps over and over again.

 

 

 

 

Slight changes in ambient lighting causing the phototransistor to trigger a bit earlier or later than the previous time?

 

Link to post
Share on other sites

Thanks Duncan,

 

I don’t think changes in ambient lighting would affect it, although I did ponder whether the “edge” was consistent.

 

It rotates 10 turns in around three minutes to do the test, so fairly quick.  The telly was on, but it’s a fairly big room, so I doubt that it would influence the situation.  Similarly workbench light, and room lights were on throughout.  

 

Easy to check though, I’ll graph the sensor output vs the step number.  Further reports during the day!

 

ta

Simon

 

 

  • Like 1
Link to post
Share on other sites

Thanks Duncan,

 

I don’t think changes in ambient lighting would affect it, although I did ponder whether the “edge” was consistent.

 

It rotates 10 turns in around three minutes to do the test, so fairly quick.  The telly was on, but it’s a fairly big room, so I doubt that it would influence the situation.  Similarly workbench light, and room lights were on throughout.  

 

Easy to check though, I’ll graph the sensor output vs the step number.  Further reports during the day!

 

ta

Simon

 

 

Link to post
Share on other sites

Well, Duncan, you might have been close to, if not actually on the money here...  graphing the signal from the light sensor reveals that it’s anything but smooth & consistent.  

 

I didn’t manage to catch a “switching point” (set as the value dropping below 300) but the “non-switching” seems to average around 990 with excursions to below 900, per the attached picture of a plot.

 

5EF657E3-AD1D-450C-A3FB-A42BA0DECCF0.jpeg.8a0fadb370ea1db42e51f2f5a077a62c.jpeg

 

the timebase is not defined, but it’s directly related to the step pulses going to the motor.  I don’t think there’s a mains hum on it (or if it is, it’s pretty small) but the overall noise is significant.  Time to debounce the signal methinks.

 

more soon

Simon

  • Like 1
Link to post
Share on other sites

Ha!

 

Duncan, I’m a Mech Eng, so the whole thing is a work of fantasy !  

 

Simon’s law states that mechanical problems are fixed by software, software issues are resolved in electronics, and of course, circuit problems are sorted with mechanical fixes...

 

I too concluded that smoothing was likely to be more effective than denouncing, and searched the bits boxes.  I found a capacitor and fitted it, I think it was a 47u which killed the noise down to plus/minus one unit, so that sorted that out.

 

I also reviewed my implementation of the current limit, and realised that I’d maybe got it wrong, so I fixed that, and then played with microstepping.  

 

Setting the microstepping to 16ths should give me around (200 * 16* 99.51 =) 318432 steps per rev, but I’ve still got some variation.  From 10 rotations, I got a max of 318571, a min of 318275, an average of 318421.7 and fwiw a standard deviation of 99.  

 

Given that it can’t go further in a step than it should, (can it?) the actual steps per turn must be the minimum that I’ve counted (or fewer) so the implication is that it’s losing the thick end of 300 steps on some rotations.  Don’t know why. 

 

And I don’t know why the counted minimum is fewer that the theoretical minimum!

 

thanks for the help!

 

Simon

 

Btw, this is the motor, 

 

https://www.active-robots.com/3329-0-42sth38-nema-17-bipolar-stepper-with-99-51-1-gearbox.html

Edited by Simond
  • Like 1
Link to post
Share on other sites

Philosophical question, with practical implications.

 

How fast can the A4988 driver go?  I understand that this depends on the motor, the load on the motor, and the applied voltage, and I’ve read “35rpm” somewhere in the various online posts as a typical response.

 

if this is the case, 35rpm, with 200 steps per turn, is 7000 steps per minute, which is 116 Hz.  The inverse of this is 8.6ms per step

 

Assuming an equal division between “HIGH” and “LOW” states, the stepdelay should therefore exceed 4.3ms

 

i suspect I’ve been trying to drive my turntable motor rather quicker than 35rpm, given the 99.51:1 gearing.  Accepting a slower rate of rotation might be good.  Drew (Castle of this parish) did give me an estimate of the time to do a 180 degree turn with a 12” to the foot scale Pendennis Castle on the 12” scale t/t at Didcot, which I noted in the comments at the head of my sketch.  I shall review when I get back from work, to which I must now depart.

 

comments most welcome!

best

Simon

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...