My project needs to become a central place to hook up sensors using RJ45 plugs and UTP cable. It also should allow me to control SPI and I2C devices connected to it.
Basic setup is;
N+,
2 bit shift registers.
2 SparkFun Mux/Demux boards.
Some other components that do not have any effect on the above (PWM driver from AdaFruit, a Nokia 5110 display, a RFID reader, etc.).
I'm using the bit shift registers to control the 2 sparkfun boards, that part works as intended.
My code allows me to put in a integer (1-32, it's 16 pins a board) and it enables the appropriate board, and selects the correct port (32 enables board 2, select pin 15).
At that point, I'll look up what type of sensor or piece of hardware is attached, from a list of custom 'port objects'.
Those objects contain a type (analog or digital) and some other information (IN/OUT/SPI/I2C/PWM, a port number, memory value for the last read value, etc.)
I need (want) to initiate a AnalogInput, InputPort or OutputPort at that point, allowing me to read a analog value, read a digital value or write a digital value.
That value will be logged (storage card, webservice and if needed, texted to my phone). If the device attached to the port is a SPI device, and action needs to be taken, it should write true (high) to the same pin, as that port will be used as CS pin.
Since I have 2 SparkFun boards, I need to connect 2 wires (1 wire per board) from the signal pin to a pin on the N+. I want to use the A pins here, as the SparkFun boards allow both analog, digital and data to be connected to the boards.
In code, there is a Thread that runs a loop (no interrupt on the sparkfun boards) to set the port number (that is in a list, enabled and needs to perform 'a' action at that time)
I'll paste some pseudo code to demonstrate what I'm doing.
while(thePortWatcherIsEnabled) { switchExpanderBoard(toNextPortThatIsDigital); Thread.Sleep(10); using (InputPort d = new InputPort(Pins.GPIO_PIN_A1, false, Port.ResistorMode.Disabled)) { d.Read(); } switchExpanderBoard(toNextPortThatIsAnalog); Thread.Sleep(10); using (AnalogInput a = new AnalogInput(Microsoft.SPOT.Hardware.Cpu.AnalogChannel.ANALOG_1)) { a.Read(); } switchExpanderBoard(toNextPortThatIsDigitalAgain); Thread.Sleep(10); using (OutputPort d = new OutputPort(Pins.GPIO_PIN_A1, false)) { d.Write(true); } }
After a few loops of this, the error as mentioned in the start post is thrown, and it continues throwing it at 'random' intervals (sometimes it works, most of the time it doesn't after the first exception is thrown).