4 Way Crossing Polarity  

Page 2 / 2
  RSS

ZeFerby
(@zeferby)
Estimable Member
Joined: 3 months ago
Posts: 131
2019-10-23 12:00 pm  

@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
(@john40131)
Eminent Member
Joined: 6 months ago
Posts: 36
2019-10-23 1:57 pm  

@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 2 months ago by John40131

ReplyQuote
John40131
(@john40131)
Eminent Member
Joined: 6 months ago
Posts: 36
2019-10-27 10:52 am  

@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
(@zeferby)
Estimable Member
Joined: 3 months ago
Posts: 131
2019-10-27 2:05 pm  

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

This post was modified 1 month ago by ZeFerby

Eric


ReplyQuote
John40131
(@john40131)
Eminent Member
Joined: 6 months ago
Posts: 36
2019-10-27 3:17 pm  

@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 1 month ago by John40131

ReplyQuote
John40131
(@john40131)
Eminent Member
Joined: 6 months ago
Posts: 36
2019-12-08 3:14 pm  

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
(@zeferby)
Estimable Member
Joined: 3 months ago
Posts: 131
2019-12-08 9:34 pm  
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 !

This post was modified 22 hours ago by ZeFerby

Eric


ReplyQuote
Page 2 / 2

Please Login or Register