Arduino NANO Code Question

Arduino NANO Code Question

It appears I have a conflict between two statements.

pinMode(gAddresses[i].arduinoPin, OUTPUT);

pinMode(outPin4, OUTPUT);

Both point to port A0 of the Nano.
Each are in a different Void Loop
void loop(void) {
loopDCC(); // pinMode(gAddresses[i].arduinoPin, OUTPUT);
loop3(); // pinMode(outPin4, OUTPUT);
When I run the loops separately the operation function correctly.
When running them normally it appears the “ pinMode(outPin4, OUTPUT);”
will run but not the “ pinMode(gAddresses[i].arduinoPin, OUTPUT);”

Is there a work around for this?
Thank you

// DCC_stationary_decorder_R4test.ino
//BMK 09-14-2021
// configured pinout is for a NANO

// Accessory Decoder -
// Connecting DCC Interface to an Arduino | DCC Interfaces
// Simple DCC Interface Sketch… | DCC Interfaces
// Let's learn together - DCC Decoder! (DCC model r

#include <NmraDcc.h>
#include <Wire.h>

// constants won't change. They're used here to set pin numbers:
int inPin3 = 3; // the number of the input pin
int outPin4 = A0; // the number of the output pin

int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 2000; // the debounce time, increase if the output flickers

// variables will change:
int buttonState = 0;

void setup()
// initialize the LED pin as an output:
pinMode(outPin4, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(inPin3, INPUT_PULLUP);

Serial.println(F("Accessory Decoder -"));

//void loop()
void loop(void) {

typedef struct
int address;
uint8_t arduinoPin;

// this is designed for 2 addresses, if you want more decoder functions, increase this to 3 for 3, 5 for 5, etc etc etc
DCCAccessoryAddress gAddresses[2];

NmraDcc Dcc;
uint16_t lastAddr = 0xFFFF;
uint8_t lastDirection = 0xFF;

// Decoder Init
void ConfigureDecoder()

// this is address number 0 on the serial monitor
gAddresses[0].address = 250; //switch address can only be 3 numbers
gAddresses[0].arduinoPin = A0; //orginal code A6... can only be an input for a nano
// Arduino pinout referance a NANO

// this is address number 1 on the serial monitor
gAddresses[1].address = 251; //switch address can only be 3 numbers
gAddresses[1].arduinoPin = A1; //orginal code A7... can only be an input for a nano
// Arduino pinout referance a NANO

// if you wanted more, make sure the array on line 22 is bigger, and start adding lines as follows
//gAddresses[2].address = 252;
//gAddresses[2].arduinoPin = A2;

//gAddresses[3].address = 253;
//gAddresses[3].arduinoPin = A3;

//gAddresses[3].address = 254;
//gAddresses[3].arduinoPin = A4;

// the above are commented out, and just for example only

// set the pin for output
for (int i = 0; i < (int)(sizeof(gAddresses) / sizeof(gAddresses[0])); i++)
pinMode(gAddresses[i].arduinoPin, OUTPUT);

// This function is called whenever a normal DCC Turnout Packet is received
void notifyDccAccTurnoutOutput(uint16_t Addr, uint8_t Direction, uint8_t OutputPower)
Serial.print("notifyDccAccTurnoutOutput: ");
Serial.print(Addr, DEC); //DCC address
Serial.print(Direction, DEC); // 1 = "C" or Closed and 0 = "t" or through
Serial.println(OutputPower, HEX); //1 = Ouput HIGH ... 0 = Output LOW
// example: notifyDccAccTurnoutOutput: 200,0,0

for (int i = 0; i < (sizeof(gAddresses) / sizeof(DCCAccessoryAddress)); i++)
if ((Addr == gAddresses[i].address) && ((Addr != lastAddr) || (Direction != lastDirection)) && OutputPower)
lastAddr = Addr;
lastDirection = Direction;

  Serial.print(F("Activating Decoder Address "));
  Serial.println(i, DEC);

  if (Direction)
    Serial.print(F("Turning Accessory On : "));
    Serial.println(gAddresses[i].arduinoPin, DEC);
    digitalWrite(gAddresses[i].arduinoPin, HIGH);
    //Output pin ON
    Serial.print(F("Turning Accessory Off : "));
    Serial.println(gAddresses[i].arduinoPin, DEC);
    digitalWrite(gAddresses[i].arduinoPin, LOW);
    // Output Pin OFF


void setupDCCDecoder()
Serial.println(F("Setting up DCC Decorder..."));

// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up, 2, 1);
// * ExtIntNum - Interrupt number of the pin. Use digitalPinToInterrupt(ExtIntPinNum).
// * ExtIntPinNum - Input pin number.
// * EnablePullup - Set true to enable the pins pullup resistor.

// Call the main DCC Init function to enable the DCC Receiver

// Configure the Decoder

void loop3(void) {
reading = digitalRead(inPin3);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
state = HIGH;

time = millis();


digitalWrite(outPin4, state);

previous = reading;

void loopDCC(void)

// You MUST call the NmraDcc.process() method frequently from the Arduino loop() function for correct library operation

Please edit that code to put it in code tags so we can read it.

@DBRR_BrianK, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

I doubt that :wink: What is more likely to happen is that loop3() overrides the DCC. In loop3() you unconditionally write the state to the pin, regardless if the button is pressed or not. So if the DCC code sets the output e.g. HIGH but state equals LOW (and button did not become pressed), you override the DCC.

The solution is probably (I can't test it) to only update the pin in loop3() when the button becomes pressed.

On AVR based boards (like te Nano), you can read back the state of a pin with digitalRead(). This might be useful if e.g. the DCC code sets the pin HIGH and you want to manually toggle it back.

You don't have a conflict between the pinMode statements. These statements are executed only once when starting your sketch, and both set the same pin to OUTPUT. So at the end the pin is an OUTPUT.

Your conflict is the digitaWrite to that pin. Both - your input signal and you DCC signal set the same pin to HIGH or LOW. What do you want to achive by that?

Thank you
Brian Krupicka