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?
The intended way is by using separate arduino inputs, however I use a Nano for the DCC++ base, and needed at least 12 inputs for hall magnetic sensors, so I added a 4067 multiplexer and modified the DCC++ code to incorporate a small library. It gives me 16 inputs using only 5 pins of the Arduino. http://www.ebay.co.uk/itm/CD74HC406...-Board-Module-Arduino-Precise-M-/152306067781
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?
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
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
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?
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(!)
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?
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
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/
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).
example of code : https://playground.arduino.cc/Code/I2CPortExpander8574 ready made modules on eBay : http://www.ebay.fr/itm/PCF8574-PCF8...module-for-Arduino-Raspberry-Pi-/263088028642 first of the list, just try with pcf8574 pcs if you wish some of them http://www.ebay.fr/itm/PCF8574-PCF8...le-I-O-Expansion-Modules-I2C-B1-/263244261117 1,69$ea. (with in/out connectors) If you have difficulties to identify the I2C address, just use https://playground.arduino.cc/Main/I2cScanner