Arduino NANO Code Question
It appears I have a conflict between two statements.
pinMode(gAddresses[i].arduinoPin, OUTPUT);
and
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 - www.dccinterface.com
// https://www.dccinterface.com/how-to/connecting-interface-arduino/
// Simple DCC Interface Sketch… | DCC Interfaces
//https://www.dccinterface.com/how-to/arduino-dcc-interface-accessory-decoder/
// Let's learn together - DCC Decoder! (DCC model railway with Arduino 3) - YouTube
#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.begin(9600);
Serial.println(F("Accessory Decoder - www.dccinterface.com"));
Serial.println(F("Initializing...."));
setupDCCDecoder();
}
//void loop()
//*****************************
void loop(void) {
loopDCC();
loop3();
}
//*****************************
typedef struct
{
int address;
uint8_t arduinoPin;
}
DCCAccessoryAddress;
// 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(',');
Serial.print(Direction, DEC); // 1 = "C" or Closed and 0 = "t" or through
Serial.print(',');
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);
break;
//Output pin ON
}
else
{
Serial.print(F("Turning Accessory Off : "));
Serial.println(gAddresses[i].arduinoPin, DEC);
digitalWrite(gAddresses[i].arduinoPin, LOW);
break;
// 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
Dcc.pin(0, 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
Dcc.init(MAN_ID_DIY, 10, CV29_ACCESSORY_DECODER | CV29_OUTPUT_ADDRESS_MODE, 0);
// Configure the Decoder
ConfigureDecoder();
}
//********************************
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;
else
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
Dcc.process();
}