Notifications
Clear all

4 Way Crossing Polarity

62 Posts
5 Users
1 Likes
18.8 K Views
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@john40131

I'm happy it helps.

Be sure to check everything's right with the serial debugging before relying on it though...there has been an intensive use of Ctrl-C/Ctrl-V !

Note the #define / #ifdef (or #ifndef for NOT defined) - #else -#endif technique : that can help you keeping a bunch of debugging code ready to be activated/removed by un/commenting a single #define.

You'll notice the BIG difference in program size and data size if you just compile with/without the #define !

Eric


   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

@zeferby

Thanks again, will build up but just found I need 15 relays so not enough room on 160 x 100 vero so ordered some more will certainly build as much as I can and use LEDs to simulate on breadboard after relay switches, I think I should have got an Arduino 16 way relay bank it would have been easier...

Ahhh just noticed would need 2 as they are SPDT I need DPDT relays..so will use the ones I have ...

John

This post was modified 4 years ago by John40131

   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

@zeferby

Just as a matter of interest I found these 2 videos which could be a backup plan or at least another usage somewhere on layout ...

...

Mega not arrived yet so just building relay boards..

John


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

Hi @john40131

Yes, shift registers are a valid option as well.

 

I've been learning about DCC and especially the DCC++ implementation.  Wow ! DCC++ is really an impressive work and the code is VERY nice ! I've also found a french web site from railroad arduinists/modelists who wrote a lot of explanations and developped a bunch of variations from- and addons to- DCC++ : https://www.locoduino.org/ (sorry, that's in french ! ?)

Meanwhile I received my new I2C toys : several PCF8574 I2C IO expander modules, a pair of Adafruit TCA9548A modules (I2C switch with 8x I2C buses and 3.3v/5v level shifting) and some MCP23017 modules (16bits I2C IO expanders) to play with...

I began testing the IoAbstraction library (available in the Arduino IDE with Manage Libraries) which gives an abstract "flat space" of your arduino pins + I2C expanders pins / Shift registers pins : pin range 0-99 is by default reserved for the "real" arduino pins (so pins <arduino max.pin>+1 to 99 are "dead"), while you add "abstract pins" at convenient numbers from 100 and above for I2C expanders / shift registers.

I used a small test rig I have with an Uno, a DHT22 temp/humidity sensor + an LCD20x4 (wired on the I2C bus) and I added 1x PCA9685 and 2x PCF8574 modules to the I2C bus, 2 pots, 2 LEDs, 2 push buttons to test this library.  For the moment everything is going well and the inputs are all nicely responsive (but the PCA9685 is still unused).

Here is my current test +

sketch :

  • the 2x 8574 and the arduino pins are all mapped to "abstract pins"
  • each 8574 module has a push button on its own physical pin 0 and a LED on pin 1 (toggled on/off by the corresponding button, with a repeat rate)
  • 2 pots on Arduino A0/A1 enable adjusting frequency for the built-in LED blinking and the DHT22 readings, which are written to the LCD
  • the 9685 is still unused
  • Changing a #define in the code enables either using interrupts (pins 2 and 3 on my Uno for the two 8574s) for the 8574s, or the basic 20ms-polling of all switches
  • if using interrputs, the INT pin of the 8574's must be connected to Arduino pins 2 and 3
  • recurring tasks are scheduled with the library's taskManager

I'll soon make some space to put together a bigger test rig with some servos attached to the PCA9685, and LEDs, switches and IR sensors on PCF8574's...

Edit: and maybe I'll add some relays as well...

Eric


   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

@zeferby

That sounds great, I have still got to play around with the PCA9685 connecting with 7 servos I have for point control, which will try and lockin with the polarity switching, plus the Scissor crossing I can finish as I just got some cheap second hand point ... just so much wiring and building gonna be at least another month for i can close this section and start on the next one...

Have a look at the code for DCC Base Station from Github, also this works with JMRi which is a free download and that whoever wrote the code is awsome, a lot of people use JMRi to control their trains ....

This post was modified 4 years ago by John40131

   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

Hi Eric, sorry for long delay in response with the sketches that you were so kind to do for me.

I have been doing a lot of Hardware work when I got the chance.

I did downloaded all 3 sketches but cannot find on the forum any more, but I am having a great deal of problems getting to work with all switches the #def NOHARD and def DEBUG I wasnt sure how far to go with uncommenting.

The 3rd sketch had a compile error.

I would love to use your sketches but my inexperience in programming is making it hard to find were the problem is,  I have a Mimic board setup with all points in Red and polarity shown in Blue/Yellow and reversed on other side of board as you can see, this is reading the outputs from the Mega  so I went back to my original sketch which I have working in a fashion obviously it does not have the what if senario built in but at least I can debug some of the hardware.

The  mimic panel input is Via an Opto coupler for Red LED's and pins to link to another board for Servo's, a CD4069 between the 2 sets of Blue/Yellow LEDs, I will output to the Servo Mega to switch relays to operate servo's thats why I isolated this section, the servo's  do work great but I think I need to switch off servo after movement so dont get hot, most of the hardware is done so its just a matter of track cutting for isolation on top.

I will update you on more progress but its going to difficult to do to much before Xmas.

I hope you have a Merry Christmas and a Happy New Year..

Regards John

Point control and Mimic panel

 


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 
Posted by: @john40131

Hi Eric, sorry for long delay in response with the sketches that you were so kind to do for me.

No worry John. It took me a bit of time to refresh my memory about these sketches but i just re-read them now.

Posted by: @john40131

I did downloaded all 3 sketches but cannot find on the forum any more

I think you are refering to >>this post linked here<< ?

Posted by: @john40131

The 3rd sketch had a compile error.

Hmm, that's true. Weird, two lines are missing a true function call argument.  I corrected this, and removed 2 unused constants (mainDelay1 and mainDelay2).

By the way the first line of "owners" listed in void dumpAllStates() should read 1A..1D instead of 1A..4D and I corrected that also.

Posted by: @john40131

but I am having a great deal of problems getting to work with all switches the #def NOHARD and def DEBUG I wasnt sure how far to go with uncommenting.

DEBUG_CROSSING :

The line #define DEBUG_CROSSING enables all the blocs of code that initialize the serial port and ouput a lot of debug messages to the Arduino IDE Serial Monitor, like this one :

#ifdef DEBUG_CROSSING
Serial.println(F("==========================================================================================="));
Serial.println(F("entering loop"));
delay(1000);
#endif //DEBUG_CROSSING

For this DEBUG_CROSSING  symbol, if you comment that #define line, no debug output should be sent to the serial monitor (and the program size will be much lower)

CROSSING_NOHARD :

I need the line #define CROSSING_NOHARD because I don't have the required hardware (relays etc...), and also to run the sketch on an Arduino Uno, which does not even have enough pins, so I could still emulate the system and have the serial outputs.

If CROSSING_NOHARD is defined as above, then in the following code block, the digitalWrite is removed from the sketch and the Serial.println is included instead.

If CROSSING_NOHARD is NOT defined, then the Serial.println is removed and the digitalWrite is included in the sketch.

 // Set turn point
#ifdef CROSSING_NOHARD
Serial.println(F("no digitalWrite to turn points"));
#else //CROSSING_NOHARD
digitalWrite(point1, TURN_POS_LEFT);
#endif //CROSSING_NOHARD

 

But actually there is a bug here : 

If CROSSING_NOHARD is defined as above, but DEBUG_CROSSING is NOT defined, then we try to write output to the serial monitor while the Serial port has never been initialized...

So :

I also inserted a few lines in the debugged version attached here, with a new NEEDS_SERIAL symbol used to initialize Serial whenever at least one of the two symbols CROSSING_NOHARD and/or DEBUG_CROSSING is defined :

#ifdef DEBUG_CROSSING
#define NEEDS_SERIAL
#endif
#ifdef CROSSING_NOHARD
#define NEEDS_SERIAL
#endif

and in void setup() :

#ifdef NEEDS_SERIAL //NEEDS_SERIAL is defined if DEBUG_CROSSING and/or CROSSING_NOHARD defined
Serial.begin(9600);
#endif //NEEDS_SERIAL

 

Here is the debugged script :

 

So if you try this sketch on real hardware :

  • make sure the pin numbers are #define'd correctly !
  • you can comment the line #define CROSSING_NOHARD
  • you can also cleanup the code if you want, by editing all the #ifdef CROSSING_NOHARD...#else...#endif blocks to keep only the contents of the "else" side

 

Example cleanup for real hardware usage :

The original block

// Set Frogs polarity
#ifdef CROSSING_NOHARD
Serial.println(F("no digitalWrite to frogs"));
#else //CROSSING_NOHARD
digitalWrite(frog4A, FROG_POLARITY_LEFT);
digitalWrite(frog3B, FROG_POLARITY_LEFT);
digitalWrite(frog2C, FROG_POLARITY_LEFT);
digitalWrite(frog1D, FROG_POLARITY_LEFT);
#endif //CROSSING_NOHARD

can be edited into :

// Set Frogs polarity
digitalWrite(frog4A, FROG_POLARITY_LEFT);
digitalWrite(frog3B, FROG_POLARITY_LEFT);
digitalWrite(frog2C, FROG_POLARITY_LEFT);
digitalWrite(frog1D, FROG_POLARITY_LEFT);

I hope this helps you.

If you want we can arrange an interactive session on Discord, which provides text chat/voice chat/screen sharing : I manage several Discord servers and I can give you an invite code, we'd just have to find a date and time (I'm in France, so currently my timezone is UTC+1).

 

And a Merry Christmas and Happy New Year to you as well !

Eric


   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

@zeferby

Hi Eric, Thanks again for taking the time to help me, I'm a bit busy over next few days with family things but I have downloaded your latest version so will try that when I can and let you know how I get on, I just had chance today to build a Relay interface board so can input from the LED panel and relay switch to the Servo Mega, the LEDs on the Mimic panel are laid out as the track diamond crossing go so the Left LED set show before switching and Right LED set show after each track after switching, I had it working on my sketch in a fashion the only thing I cant figure is switching points 1 through 4 work fine but then when I switch 5 4A should go but all come back to right hand side and happens with any combination so have any 4 thrown shows polarity change but as soon as I switch the 5th all LED's come back to RH side so the Mega outputs are going High on each output pin until the above happens so all are going LOW when 4A should go High unless its the fact I was powering from USB Lead so exceeded output current ...the original sketch of mine did have a few bugs but this is strange, hopefilly can get your working..

Servo Relay panel

 

This post was modified 4 years ago by John40131

   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

Hi Eric, I just uploaded your sketch and I am still having problems, the outputs from Arduino as follows pin 1 is HIGH, pins 2 to 10 are LOW and pins 11 to 15 are LOW, the switch inputs have no effect on outputs and noticed every 10 seconds pin 1 momentary goes LOW that is 1A on polarity, I have included 2 pics from serial monitor pic 1 after switch on, pic 2 after 10 seconds, I have not changed anything on your sketch so far...

Regards John

First Start
After 10 seconds

 


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@john40131

Hello John, I see these "no digitaRead..." and "no digitalWrite..." in your screenshots, so that means you are running it with the #define CROSSING_NOHARD un-commented.

So it is just using a block of code in the main loop() that always emulates some signals instead of reading your actual switches pins, and it always gets the same values every 10 seconds...You can see it at line 226 in rail_crossing_sample2_V2.ino :

#ifdef CROSSING_NOHARD
Serial.println(F("no digitalRead"));
switch1State = TURN_POS_LEFT; // = HIGH
switch2State = TURN_POS_LEFT; // = LOW
switch3State = TURN_POS_RIGHT;
switch4State = TURN_POS_RIGHT;
switch5State = TURN_POS_LEFT; // ! CLASH !
#else //CROSSING_NOHARD
switch1State = digitalRead(track1Switch);
switch2State = digitalRead(track2Switch);
switch3State = digitalRead(track3Switch);
switch4State = digitalRead(track4Switch);
switch5State = digitalRead(track5Switch);
#endif //CROSSING_NOHARD

Also you may want to comment out or reduce the 10 seconds delay at the end of loop() at line 324

 

 

Eric


   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

I have also had a look at the senario I wrote and there are some errors and additions I have made which may make things easier also the plan with track and Point numbers on...

Sorry if it changes everything hope it wont, if it does then I will try and get my sketch to work..

I feel you have done more than enough for me, but its up to you .

Regards John

4 way crossing senario
4 way crossing tracks v2

 


   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

I will have a look at your suggesions tomorrow as I have added extra reply.

Its been a really challenging week all in all, Monday evening CH Boiler failed, Thursday Mornin one of my daughters No 2 who is a child minder had a flat tyre she only picked this Renault Senic 7 seater up on Tuesday 5 month old 19 plate, no spare tyre, daughter No 1 to the rescue, New Boiler Fitted Wednesday, Friday no heating a pipe to a radiator leaking sorted that, what next...LOL

John


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

LOL, now you just need to draft daughter No 3 for sketch proof-reading duty and 2020 will be just fine !

Eric


   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

Well daughter No 3 does work for a big firm of Solicitors in Manchester so she good at typing and I think she does a bit with Python...LOL

I have just expanded on the senario so you can see were there could be conflicts with polarity , have a look and see if your sketch is conforming to this .... its becoming a nightmare... will try a word Document not sure if will accept..

 


   
ReplyQuote
(@john40131)
Member
Joined: 5 years ago
Posts: 95
Topic starter  

I have updated the Word document a bit so you can understand the possible problems ...

 

 

Regards John


   
ReplyQuote
Page 2 / 5