Jump to content
 

Arduino Applications and Programs


Simond
 Share

Recommended Posts

  • RMweb Premium
7 hours ago, melmerby said:

I think some of you are over thinking this.

...

 

 

I think this comes with the territory for some of us here :-)

 

A working traverser is always going to be an appealing project to many people, and the concept will inspire fresh ideas. I wish you all success with your own solution.

 

- Richard.

Link to post
Share on other sites

  • RMweb Premium
2 hours ago, Donw said:

 

 If has been a break in the mains supply the DCC system will go off too. I assume that the decoder will restart. I would expect the decoder to revert to a default position not remember which way it was set. This is fine with a turnout you just re-operate it. But if it has a loco on it when the power out occurs the loco should be in the centre track and would not be an issue i will either go to one or the other side  according to the default. If it is the wrong side you just re-operate it.

This is no different to me turning off the power and turning it back on. (or indeed a short which trips the DCC supply)

I can assure you the decoder does know which way it was set (as do the NCE Switch-8s which control my Tortoises).

Both of them are powered straight from the DCC and, I presume, use NVM in the PIC to store the settings.

 

Link to post
Share on other sites

  • RMweb Gold
4 hours ago, melmerby said:

This is no different to me turning off the power and turning it back on. (or indeed a short which trips the DCC supply)

I can assure you the decoder does know which way it was set (as do the NCE Switch-8s which control my Tortoises).

Both of them are powered straight from the DCC and, I presume, use NVM in the PIC to store the settings.

 

 

I have an early Len one which I thought did return to default I must check it could just be my habit of restoring switches as a signalman would put levers back after a move.

Don

  • Like 1
Link to post
Share on other sites

Sorry, promised this last week.  Time flies.


 

hope it’s of help.  If you care to look in Paul’s thread on Western Thunder, there’s some more details.  Paul purchased a couple of ready-made stepper-drive-leadscrew-and-guide assemblies from the w3.  They had a carriage sliding on an extrusion with ball bearing guides.

 

atb

Simon

 

moor_street_traversers_28may19.ino

Link to post
Share on other sites

  • RMweb Premium

All gone Snafu today:cry:

Got all the features working on a breadboard so transferred it to the (final) stripboard layout.

Checked out the wiring, all was OK but motor was turning but very slowly

Tried another Stepper Motor. Still not working correctly.

 

So transferred back to breadboard, still no go.

Changed the TMC2208, same.

Changed the Nano, same.

Head scratching time.

 

EDIT

N.B. I have gone to the basic sketch from Github and still running very slowly:scratchhead:

 

 

Edited by melmerby
Link to post
Share on other sites

  • RMweb Premium

Following the head scratching it all returned to normal but I don't know what the problem was!:scratchhead:

Anyway all functions proven so conmmited to the strip board where everything is still working OK but I need some logic to control just when certain things happen.


In plain language I want this

If (switch1 is LOW & direction is LOW) OR (switch2 is LOW & direction is HIGH)
Then Enable equals LOW (- LOW is enabled, HIGH is OFF)
Else Enable equals HIGH

I have set out the operation like below but however at no time does the enable go low:

 

void loop() {
  val = digitalRead (DIR_PIN_IN);   // Read direction status from DCC
  step1 = digitalRead (EndStopPin1);// Read status of end stop switch
  step2 = digitalRead (EndStopPin2);// Read status of end stop switch 2
   
  digitalWrite (DIR_PIN_OUT, val);  // Write direction to TMC2208

 

  // determine whether stepper motor driver should be enabled
  if (step1 , LOW &&  (val , HIGH  || (step2 , LOW &&  (val, LOW))))
  {digitalWrite(EN_PIN, LOW)   ;}
  else
  {digitalWrite(EN_PIN, HIGH) ;}

  digitalWrite(step_PIN, !digitalRead(step_PIN));// step motor
  delay(1);

 

N.B. it all compiles correctly.

(I assume the problem is the if..else and the ands and or)

Could someone please look at the sketch and suggest what is wrong with the logic in the loop?

 

Please see full sketch:

TraverserProject9.ino

 

 

 

 

Link to post
Share on other sites

Try moving one of the brackets in the line:

 

if (step1 , LOW &&  (val , HIGH  || (step2 , LOW &&  (val, LOW))))

 

to after HIGH so it looks more like your plain language line :

 

If (switch1 is LOW & direction is LOW) OR (switch2 is LOW & direction is HIGH)

(i'm assuming val is inverted for some reason)

 

thus:

 

if (step1 , LOW &&  (val , HIGH)  || (step2 , LOW &&  (val, LOW)))

 

or belt and braces

if ( ( (step1 , LOW) && (val , HIGH) ) || ( (step2 , LOW) && (val, LOW) ) )

 

(For some reason your .ino project is unavailable)

 

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

  • RMweb Premium

Hi there

I was having great trouble trying to post that, so I'm not surprised it's unavailable.

Everytime time I did a carriage return, for some reason it did it two lines back even though the cursor was flashing where I wanted it!

Then when I tried copy and pasting from one part to another it went totally haywire and shut down.

Even starting again I found it wasn't playing easy.:(

 

Let's try again:

TraverserProject9.ino

 

"val" is the state of the direction, if I force enable down with a line of code, the motor starts stepping, then operating the DCC decoder causes the step direction to change, so that bit is fine.

The DCC input is from a point decoder which after going through an opto coupler gives 0v or 5v depending which way it is thrown

 

 

 

 

 

Edited by melmerby
Link to post
Share on other sites

  • RMweb Gold

You have  if (step1,low.... etc.)   and if (step2,low......)    try if(step1==LOW.......   and if (step2==LOW.........

 

if(!step1)   may work but I cannot remember if LOW equates to zero or just below a set value.   

 

Not quite sure how if interprets the ,  

 

Don

 

Link to post
Share on other sites

  • RMweb Premium
8 hours ago, Donw said:

You have  if (step1,low.... etc.)   and if (step2,low......)    try if(step1==LOW.......   and if (step2==LOW.........

 

if(!step1)   may work but I cannot remember if LOW equates to zero or just below a set value.   

 

Not quite sure how if interprets the ,  

 

Don

 

I've changed the comparisons to "==" rather than a comma  which starts to work but only one movement then it stops.

(I've also changed the "val" to "deck" in case val has some strange effects)

Link to post
Share on other sites

  • RMweb Gold
17 minutes ago, melmerby said:

I've changed the comparisons to "==" rather than a comma  which starts to work but only one movement then it stops.

(I've also changed the "val" to "deck" in case val has some strange effects)

 

Thats because when it moves off the step1 pin no longer is low you need to change something of the code

 

something like this

 

Void loop

 

If (step1== LOW&& deck==HIGH) {

do   digitalWrite(EN_PIN, LOW)   ;

while  (digitalRead (EndStopPin2))    // as long as PIN 2 is High when reaching the far end Pin 2 goes LOW and the stepping stops

}

If (step2==LOW&&deck==LOW) {

do digitalWrite(EN_PIN==HIGH);

while  (digitalRead(EndStopPin1))   // as long a Pn1 is high when reaching the end Pin1 go Low and it stops

}

 

the idea is rom each end it will step towards the other end until that pin goes LOW indicating you have reached the far end.

 

Don 

Link to post
Share on other sites

  • RMweb Premium

I've modified the code to use do........while as you suggest and after a little bug removal, EN_PIN,LOW to EN_PIN==LOW. ( it wont compile otherwise)

Also putting semicolons after each do..while section (again it wouldn't compile without them) it ¾ works.

It travels to the end and stops when the switch operates but wont return when the direction is changed unless you reverse the same switch, the other one has no effect

 

So it looks like it's getting close.

 

 

 

  • Like 1
Link to post
Share on other sites

  • RMweb Gold
13 hours ago, melmerby said:

I've modified the code to use do........while as you suggest and after a little bug removal, EN_PIN,LOW to EN_PIN==LOW. ( it wont compile otherwise)

Also putting semicolons after each do..while section (again it wouldn't compile without them) it ¾ works.

It travels to the end and stops when the switch operates but wont return when the direction is changed unless you reverse the same switch, the other one has no effect

 

So it looks like it's getting close.

 

 

 

 

My bit of code was just rattled off give show you the sort of things you need . Call the ends A and B both sensor cannot be on  so if the direction is set to A  it should keep going until A sensor detects .  It should now stay there until the direction is changed  when it should now travel towards B until that sensor detects it has got there.  I could sit down and work out something but it is really better for you to understand the code if you get stuck give me a shout.

 

Don

  • Like 1
Link to post
Share on other sites

  • RMweb Premium

I spent quite some time with the do...while structure but I still could not get it to work properly.

Probably all to do with braces and semicolons!

So I went back had had another go at the if...else structure and after some more reading up managed to get it going, seemingly correctly:

TraverserProject12.ino

 

Unfortunately it appears it can sometimes power up in a "limbo" state and refuse to start at all, no manner of switch positions/direction state will persuade it to go. Enable remains stubbonly high (OFF).

Looks like it needs some form of initialisation to start it in these circumstances, so more head scratching and reading to do.

 

Edited by melmerby
Link to post
Share on other sites

  • RMweb Gold

The logic of that doesn't make sense to me but I am unfamiliar with the library you are using.

 

Looking at your loop()

 

the first section  reads the status of the End stops  and the Deck position

 

the next section is the if statement  The enable pin will only be set LOW if stop1=Low and Deck position=High or if stop2 is LOW and Deck position is LOW   otherwise the Enable pin is High

 

the last section is a couple of Digital writes and a delay

      the first write gives the deck positon read in section 1 to the stepper motor

     the second seems odd to me you are writing to step pin  the inverse of what you are reading from step pin it the pin an input or an output pin. This should be set to make one step

      the Delay is 1 millisecond hardly anything what is its purpose?

 

the loop repeats.   The questions are how far does it set on the digital write? 

                                  when halfway between the two ends surely both Endstop1 and Endstop2 are both high i.e it is at neither end  so the if statement will not be true and the Enable will be High. So it cannot move from partway across.  I am assuming the the ENDstops go LOW when the tranverser is detected.

 

         Try changing it to 

 

 if  ((stop2 == HIGH &&  deck_position == HIGH)  || (stop1 == HIGH  &&  deck_position== LOW))   That way If deckposition=High  the Enable will be on until is reaches stop2   or if t deckposition=LOW it will be enable until Stop1 is reached  

 

Don

 

Link to post
Share on other sites

  • RMweb Premium

Hi Don

I'm using the gitHub TMC2208 v3.0 library with UART connection, the STEP command was already there in the demo and as the motor turns at a suitable speed with the 1mS setting I left them like that.

I assume the 1mS is the microstep delay, If you reduce it then the motor starts whining but doesn't turn.

 

I have just worked the sketch around the EN & DIR inputs which are logic level inputs to the TMC2208 where LOW enables stepping, DIR just determines the direction.

The switches are changeovers between 0 & 5v, so the logic can be based on whether they are low or high.

 

 

Link to post
Share on other sites

  • RMweb Gold

This would be my approach assuming a LOW read on the ENDPins indicates the traverser is at that end

 

Void loop()

 val = digitalRead (DIR_PIN_IN);   // Read direction status from DCC   assume LOW go from 1 to 2 High goes from 2 to 1

 digitalWrite (DIR_PIN_OUT, val);  // Write direction to TMC2208

If (val== LOW)  {

    // Read status of end stop switch 2

      While  (digitalRead (EndStopPin2==High)) {            

          digitalWrite(EN_PIN, HIGH) ;

         digitalWrite(step_PIN, !digitalRead(step_PIN));// step motor
         delay(1);

         }

    }

   else if (val==High) {

    // Read status of end stop switch 2

      While (digitalRead (EndStopPin1);) {            

          digitalWrite(EN_PIN, HIGH) ;

         digitalWrite(step_PIN, !digitalRead(step_PIN));// step motor
         delay(1);

       }

     }

}

 

Quite simply  if the dcc says go to 2 it checks are we at 2 if not take a step forward then check again etc. till you reach 2  and vice versa to go to 1.

 

Don

  • Like 2
Link to post
Share on other sites

  • RMweb Premium

Hi Don

Thanks for looking at it again.

You are no doubt more knowledgable than I, as I haven't done a lot of Arduino or C++ coding.

Two completed model railway projects prior to this: A DCC decoder for motor driven points and accurate stopping when approaching a bufferstop using VL6180X ToF modules to measure the trains distance.

No compound If....else etc. commands were involved:D

 

I'll have a look at your new suggestion tomorrow.

 

Keith

Link to post
Share on other sites

  • RMweb Premium


Hi Don

I've been trying your latest suggestion for several days and I just can't get it working

ENable is always LOW so the motor is powered but it isn't stepping in any combination of switches or direct.

I assume I have done something wrong, I have this as the full code which compiles nicely:

traverser 13.pdf

 

Link to post
Share on other sites

  • RMweb Gold
13 hours ago, melmerby said:


Hi Don

I've been trying your latest suggestion for several days and I just can't get it working

ENable is always LOW so the motor is powered but it isn't stepping in any combination of switches or direct.

I assume I have done something wrong, I have this as the full code which compiles nicely:

traverser 13.pdf 43.43 kB · 6 downloads

 

 

I dont think you have done something wrong I suspect it is something missing. I am at a disadvantage at not having used a stepper motor.  I have had a brief dip into the Library TMC2208   as typical with these libraries you have a job sorting out just what goes on. I had so much trouble with a Keypad one trying to work out if a key was being held I just wrote my own bit of code to read the key presses which made it easy.

 

A couple of things. Have I understood the End stop detectors  correctly am I right that the End stops set the pin LOW when the traverser is at that end. If that is true you could try setting the ENDstop pins as mode INPUT_PULLUP  this ensure they will be HIGH when not LOW. I would try that first.

The other thing is I notice some cases seem to set a driver current  I dont nnot if there is a default or it is necessary. Try the INPUT_PULLUP  first. If it doesn't do the trick I will look further.

 

Don

Link to post
Share on other sites

  • RMweb Premium
1 hour ago, Donw said:

 

I dont think you have done something wrong I suspect it is something missing. I am at a disadvantage at not having used a stepper motor.  I have had a brief dip into the Library TMC2208   as typical with these libraries you have a job sorting out just what goes on. I had so much trouble with a Keypad one trying to work out if a key was being held I just wrote my own bit of code to read the key presses which made it easy.

 

A couple of things. Have I understood the End stop detectors  correctly am I right that the End stops set the pin LOW when the traverser is at that end. If that is true you could try setting the ENDstop pins as mode INPUT_PULLUP  this ensure they will be HIGH when not LOW. I would try that first.

The other thing is I notice some cases seem to set a driver current  I dont nnot if there is a default or it is necessary. Try the INPUT_PULLUP  first. If it doesn't do the trick I will look further.

 

Don

The end stops have changeover switches connected to V+ and 0V so feed either HIGH or LOW to the Arduino to ensure no floating state. The DIR is also switched between V+ and 0v

The motor driver current is programmed in the "void() setup" section of the sketch.

 

When I started I was using 12v for the motor supply and the motor & TMC2208 were getting quite warm, so I tried reducing the set current from 500mA to 250mA. Not sure it made much difference.

I tried another stepper motor (with higher coil resistance) which was better and also seemed to be more powerful, so I then reduced the motor supply to 5v where it still is.

At 5v it seems just as powerful as at 12v and doesn't get warm, neither does the TMC2208

 

Keith

 

 

Link to post
Share on other sites

  • RMweb Gold
2 hours ago, melmerby said:

The end stops have changeover switches connected to V+ and 0V so feed either HIGH or LOW to the Arduino to ensure no floating state. The DIR is also switched between V+ and 0v

The motor driver current is programmed in the "void() setup" section of the sketch.

 

When I started I was using 12v for the motor supply and the motor & TMC2208 were getting quite warm, so I tried reducing the set current from 500mA to 250mA. Not sure it made much difference.

I tried another stepper motor (with higher coil resistance) which was better and also seemed to be more powerful, so I then reduced the motor supply to 5v where it still is.

At 5v it seems just as powerful as at 12v and doesn't get warm, neither does the TMC2208

 

Keith

 

 

 

I am delving into the specs for the TMC2208. I am a bit confused about all the serial stuff so I am not sure whether it is being controlled by the pins for dir and enable alone or whther there are instruction being sent via serial. Do you have any connections from the TX&Rx on the Arduino to the TMC2208.  It could be that we are confusing two operational modes.  We shall get there I am sure.

 

Don

 

Link to post
Share on other sites

  • RMweb Premium
3 minutes ago, Donw said:

 

I am delving into the specs for the TMC2208. I am a bit confused about all the serial stuff so I am not sure whether it is being controlled by the pins for dir and enable alone or whther there are instruction being sent via serial. Do you have any connections from the TX&Rx on the Arduino to the TMC2208.  It could be that we are confusing two operational modes.  We shall get there I am sure.

 

Don

 

Yes the Tx/Rx are connected as per the recommendations for UART setting.

This sketch evolved from the GitHub Demo where EN, DIR & STEP are controlled via Arduino digital pins. the microstepping and current control, I assume is via the serial settings.

Arduino Tx goes to the serial pin on the TMC2208 via a 1k0 and Arduino Rx directly to the serial pin

The link on the underside of the TMC2208 to enable serial is already made in V3.0 types

Link to post
Share on other sites

  • RMweb Premium

Something that I think needs to be added is code, that after the "while" arguments have finished,  puts EN_PIN=HIGH to turn the driver off.

So far all the various sketches can sometimes leave the EN in LOW state and hence on, even though it's stopped stepping.

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