DCC++ and multiple inputs

Erik84750 May 18, 2017

  1. Erik84750

    Erik84750 TrainBoard Member

    78
    40
    4
    Do I have to wire every single sensor input to a separate Arduino input? Or is there a way around with shift registers, or any other means?
     
  2. Jimbo20

    Jimbo20 TrainBoard Member

    60
    59
    3
  3. Erik84750

    Erik84750 TrainBoard Member

    78
    40
    4
    That is what I am looking for! How and where do you modify the code?

    I would like to experiment with your idea: can you maybe provide some input please?

    What do you think of the use of shift registers?
     
  4. Jimbo20

    Jimbo20 TrainBoard Member

    60
    59
    3
    I chose the multiplexer as I had used them before. It's late here now, but I'll dig out some code for you tomorrow evening.... zzzzzzz
    Jim
     
  5. Erik84750

    Erik84750 TrainBoard Member

    78
    40
    4
    Thank you!
     
  6. Jimbo20

    Jimbo20 TrainBoard Member

    60
    59
    3
    This is what I did (NOTE that you will not be able to use any other pins for sensor input without further changes to the code)

    HARDWARE:
    I used a 4067 mux module similar to this:
    http://www.ebay.co.uk/itm/CD74HC406...-Board-Module-Arduino-Precise-M-/152306067781

    And wired it as follows:

    4067module s0-s3 > pins d9-d6 of the nano (these are the 'address' lines of the mux (0 - 15))
    4067module SIG > pin d4 of the Arduino (this is used as data in)
    VCC to 5v supply
    GND to ground

    Sensors were wired to C0 - C15 as required. I only used the first 12 for my hall magnetic sensors each with a suitable pull up resistor.

    SOFTWARE:
    Then I downloaded the following 4067 library and put the 2 files CD40HC67.CPP and CD40HC67.H in the DCC++ project folder. They should then appear in the tabs of the Arduino IDE.

    https://github.com/waspinator/CD74HC4067
    Then I modified the DCC++ project in the following places (I hope I've remembered them all!)

    In the main project tab I added the line (indicated)
    Code:
    #include "Config.h"
    #include "Comm.h"
    #include "CD74HC4067.h" <<<<<<<<<<<<<
    #include "Outputs.h"
    Then in Sensor.cpp I made the following changes:
    Code:
    #include "DCCpp_Uno.h"
    #include "Sensor.h"
    #include "EEStore.h"
    #include <EEPROM.h>
    #include "Comm.h"
    #include "CD74HC4067.h" //jws  <<<<<<<<<<<<
    CD74HC4067 mux(9,8,7,6);  //mux4067 library address pins jws <<<<<<<<<<<
    .
    .
    .
    .
     for(tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
         pinMode(4,INPUT);  // always pin 4 (using mux) <<<<<<<<<<<<<<
         mux.channel(tt->data.pin); // set mux channel  <<<<<<<<<<<<<<
     //jws modified following for mux                        <<<<<<<<<<<<<<
       // tt->signal=tt->signal*(1.0-SENSOR_DECAY)+digitalRead(tt->data.pin)*SENSOR_DECAY; <<<<<<<<
    tt->signal=tt->signal*(1.0-SENSOR_DECAY)+digitalRead(4)*SENSOR_DECAY; //sensor input is always pin 4 <<<<<<<<<<<<<<<
    .
    .
    .
    .
        if(!tt->active && tt->signal<0.5){
    
     tt->active=false;
      tt->signal=1;
      //jws comment out following 2 lines - not needed for mux    <<<<<<<<<<<<<
      //pinMode(pin,INPUT);         // set mode to input             <<<<<<<<<<<<<<
      //digitalWrite(pin,pullUp);   // don't use Arduino's internal pull-up resistors for external infrared sensors --- each sensor must have its own 1K external pull-up resistor  <<<<<<<<<<<<<<
    
      if(v==1)...
    
    Then to define sensors I used commands <S0 0 0>, <S1 1 0> , <S2 2 0> etc to <S15 15 0> as required and saved to eeprom with <E>. Check them with <S>.

    The DCC++ controller will then continuously cycle round all the defined sensors and if any change state it will output a message e.g. <Q12> if sensor 12 becomes covered or <q12> if sensor 12 becomes uncovered.

    I think that's everything! Good luck.

    Jim
     
  7. Erik84750

    Erik84750 TrainBoard Member

    78
    40
    4
    Thanks!
    If I understand correctly this is specifically for Pro Mini or Nano; because other pins will not be able to be used for inputs this would disable quite a few pins on Uno or Mega?
     
  8. Jimbo20

    Jimbo20 TrainBoard Member

    60
    59
    3
    I use a Nano, but the pin count is the same as a Uno; The Mega has lots more. If you want to use the other spare pins for input, the code would need to be tweaked a bit more perhaps along the lines of (not true code!):

    Code:
    If tt->data.pin  <16 then use mux read
    else
    use original read tt->data.pin -16
    Then to use a mux sensor you address pins 0 - 15 and for 'real' pin sensors you add 16 on to the real pin number.

    Jim

    Edit: amended after a rethink(!)
     
    Last edited: May 19, 2017
  9. Erik84750

    Erik84750 TrainBoard Member

    78
    40
    4
    Hi, I am afraid that at the moment this is above my programming skills: I will need to get acquianted with the DCC++ code first.

    I ordered a mux board, but I find them relatively expensive: 1USD/pce, while straight shift registers are 10 to 15 pcs for 1USD.

    Also: the mux is an analog chip (hence the cost), where you do not use its analog capabilities (only the digital HIGH or LOW signals): why that choice?
     
  10. Jimbo20

    Jimbo20 TrainBoard Member

    60
    59
    3
    As I posted before, I have used these modules before on other projects and had one spare.

    If I was manufacturing the PCBs myself, then maybe I would consider cheaper alternatives, possibly by using a shift register, but for me in the UK, a ready made module is the same price (shift register or Mux) for low volumes, and as you say the Mux will handle analog signals too, so is more versatile.

    Jim
     
  11. Michel Goyard

    Michel Goyard TrainBoard Member

    10
    6
    2
    If you still want to experiment, you can test PCF8574 modules. They work with I2C (2pins on Arduino, A4 & A5) :
    https://playground.arduino.cc/Main/PCF8574Class (plus some libraries and exemples available with google).
    I2C is not a rundondent protocol, so place capacitors all along the supply chain to avoid electrical noise.
    See your prefered dealer for these modules or google them.
    The other way is to modify the Dave Bodnar throttle (or others) in order to send accessory commands over the DCC (rails) and use an accessory decoder like the one of Rudy Boer :
    https://rudysmodelrailway.wordpress...o-attiny-dcc-accessory-and-function-decoders/
     

    Attached Files:

    Erik84750 likes this.
  12. Erik84750

    Erik84750 TrainBoard Member

    78
    40
    4
    Interesting information. Now find a way to use PCF8574 ic's in an Arduino setup.
    I would use the actual ic's and make a pc that fits them (€0.15/pce, real bargain).
     
  13. Michel Goyard

    Michel Goyard TrainBoard Member

    10
    6
    2

Share This Page