Added DCC Decoder logic to move levels

This commit is contained in:
Alex Shepherd
2022-07-24 22:52:45 +12:00
parent 2e1f9098ad
commit 54c56b7cda

View File

@@ -8,12 +8,21 @@
#include <SSD1306Ascii.h> #include <SSD1306Ascii.h>
#include <SSD1306AsciiWire.h> #include <SSD1306AsciiWire.h>
#include <EEPROM.h> #include <EEPROM.h>
#include <NmraDcc.h>
// Define the Arduino Pin to connect to the DCC input signal
#define DCC_PIN 2
// Define the DCC Turnout Address to select the first level = 1
#define DCC_ACCESSORY_DECODER_BASE_ADDRESS 200
// Define the manimus numbr of Levels
#define NUM_LIFT_LEVELS 8 #define NUM_LIFT_LEVELS 8
#define PROGRAM_NAME "Fahrstuhl" #define PROGRAM_NAME "Fahrstuhl"
#define PROGRAM_VERSION "1.0" #define PROGRAM_VERSION "1.1"
// Locate the Persistant State storage EEPROM space well above the DCC Accessory Decoder CV Storage
#define EEPROM_BASE_ADDR 100 #define EEPROM_BASE_ADDR 100
#define EEPROM_VALID_DATA_SIGNATURE 0xA5A5 #define EEPROM_VALID_DATA_SIGNATURE 0xA5A5
// Uncomment the line below to force the EEPROM values to be reset to defaults // Uncomment the line below to force the EEPROM values to be reset to defaults
@@ -21,32 +30,10 @@
#define BUTTON_LONG_PRESS_DELAY 2000 #define BUTTON_LONG_PRESS_DELAY 2000
#if defined (ARDUINO_AVR_NANO) // Setting for Uwe's Fahrstuhl System #define AJS_BOARD_SETTINGS
//#define UWE_BOARD_SETTINGS
// Uncomment the next line to reverse the direction of the stepper movement #if defined(AJS_BOARD_SETTINGS) // Setting for AJS Dev System
//#define REVERSE_STEPPER_DIRECTION
#define HOME_SENSOR_PIN 7
#define STEPPER_PULSE_PIN 4
#define STEPPER_ENABLE_PIN 5
#define STEPPER_DIR_PIN 6
#define STEPPER_MAX_SPEED 2100
#define STEPPER_NORMAL_ACCELERATION 5000
#define STEPPER_MAX_POSITION 30000U // Maximum Steps to allow the stepper to drive Up Saftey mechanism
#define BUTTON_MANUAL 8
#define BUTTON_DOWN 9
#define BUTTON_UP 10
#define BUTTON_STOP_HOME 11
long defaultPositions[NUM_LIFT_LEVELS] = {1000, 4000, 7000, 10000, 13000, 16000, 19000, 22000}; // Default positions
#define OLED_DISPLAY_I2C_ADDRESS 0x3C
#else
//#if defined(ARDUINO_AVR_UNO) // Setting for AJS Dev System
// Uncomment the next line to reverse the direction of the stepper movement // Uncomment the next line to reverse the direction of the stepper movement
#define REVERSE_STEPPER_DIRECTION #define REVERSE_STEPPER_DIRECTION
@@ -59,7 +46,7 @@ long defaultPositions[NUM_LIFT_LEVELS] = {1000, 4000, 7000, 10000, 13000, 16000,
#define STEPPER_MAX_SPEED 2100 #define STEPPER_MAX_SPEED 2100
#define STEPPER_NORMAL_ACCELERATION 5000 #define STEPPER_NORMAL_ACCELERATION 5000
#define STEPPER_MAX_POSITION 30000U // Maximum Steps to allow the stepper to drive Up Saftey mechanism #define STEPPER_MAX_POSITION 300000U // Maximum Steps to allow the stepper to drive Up Saftey mechanism
#define BUTTON_MANUAL A3 #define BUTTON_MANUAL A3
#define BUTTON_DOWN A2 #define BUTTON_DOWN A2
@@ -68,13 +55,41 @@ long defaultPositions[NUM_LIFT_LEVELS] = {1000, 4000, 7000, 10000, 13000, 16000,
long defaultPositions[NUM_LIFT_LEVELS] = {1000, 4000, 7000, 10000, 13000, 16000, 19000, 22000}; // Default positions long defaultPositions[NUM_LIFT_LEVELS] = {1000, 4000, 7000, 10000, 13000, 16000, 19000, 22000}; // Default positions
#define STEPPER_INC_SPEED (STEPPER_MAX_SPEED / 10)
#define OLED_DISPLAY_I2C_ADDRESS 0x3C #define OLED_DISPLAY_I2C_ADDRESS 0x3C
#elif defined (UWE_BOARD_SETTINGS) // Setting for Uwe's Fahrstuhl System
// Uncomment the next line to reverse the direction of the stepper movement
//#define REVERSE_STEPPER_DIRECTION
#define HOME_SENSOR_PIN 7
#define STEPPER_PULSE_PIN 4
#define STEPPER_ENABLE_PIN 5
#define STEPPER_DIR_PIN 6
#define STEPPER_MAX_SPEED 2100
#define STEPPER_NORMAL_ACCELERATION 5000
#define STEPPER_MAX_POSITION 1970000U // Maximum Steps to allow the stepper to drive Up Saftey mechanism
#define BUTTON_MANUAL 8
#define BUTTON_DOWN 9
#define BUTTON_UP 10
#define BUTTON_STOP_HOME 11
long defaultPositions[NUM_LIFT_LEVELS] = {0, 161064, 32500, 483284, 645326, 808041, 1967457, 1130774}; // Default positions
#define STEPPER_INC_SPEED (STEPPER_MAX_SPEED / 2)
#define OLED_DISPLAY_I2C_ADDRESS 0x3C
#else
#error No Board Settings Defined
#endif #endif
SSD1306AsciiWire oled; SSD1306AsciiWire oled;
#define STEPPER_INC_SPEED (STEPPER_MAX_SPEED / 10)
#define STEPPER_MAN_SPEED_CHANGE_MILLIS 5 #define STEPPER_MAN_SPEED_CHANGE_MILLIS 5
#define STEPPER_EMERGENCY_STOP_ACCELERATION 100000 #define STEPPER_EMERGENCY_STOP_ACCELERATION 100000
#define LIFT_LEVEL_NOT_SET -1 #define LIFT_LEVEL_NOT_SET -1
@@ -100,6 +115,9 @@ EncButton2<EB_BTN> btnDown(INPUT, BUTTON_DOWN);
EncButton2<EB_BTN> btnUp(INPUT, BUTTON_UP); EncButton2<EB_BTN> btnUp(INPUT, BUTTON_UP);
EncButton2<EB_BTN> btnStopHome(INPUT, BUTTON_STOP_HOME); EncButton2<EB_BTN> btnStopHome(INPUT, BUTTON_STOP_HOME);
// NMRA DCC Accessory Decoder object
NmraDcc Dcc;
void displayLevel(int newLevel) void displayLevel(int newLevel)
{ {
oled.setCursor(0,0); oled.setCursor(0,0);
@@ -202,6 +220,12 @@ void setup()
btnStopHome.setHoldTimeout(BUTTON_LONG_PRESS_DELAY); btnStopHome.setHoldTimeout(BUTTON_LONG_PRESS_DELAY);
btnManual.setHoldTimeout(BUTTON_LONG_PRESS_DELAY); btnManual.setHoldTimeout(BUTTON_LONG_PRESS_DELAY);
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
Dcc.pin(DCC_PIN, 1);
// 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);
} }
void stepperMoveTo(long newPosition) void stepperMoveTo(long newPosition)
@@ -233,8 +257,32 @@ bool configMode = false;
bool homing = false; bool homing = false;
elapsedMillis lastSpeedChange = 0; elapsedMillis lastSpeedChange = 0;
// This function is called whenever a normal DCC Turnout Packet is received
// The DCC Turnout Address is checked to see if it is within the range used to Select Elevator levels and starts a Move if a new level is selected
void notifyDccAccTurnoutOutput(uint16_t receivedAddress, uint8_t direction, uint8_t outputPower)
{
if((receivedAddress >= DCC_ACCESSORY_DECODER_BASE_ADDRESS) && (receivedAddress < (DCC_ACCESSORY_DECODER_BASE_ADDRESS + NUM_LIFT_LEVELS)))
{
uint8_t newLevel = receivedAddress - DCC_ACCESSORY_DECODER_BASE_ADDRESS;
if(persistentValues.lastLiftLevel != newLevel)
{
persistentValues.lastLiftLevel = newLevel;
long newPos = persistentValues.levelPositions[persistentValues.lastLiftLevel];
stepperMoveTo(newPos);
Serial.print("notifyDccAccTurnoutOutput: Move to Level: "); Serial.print(persistentValues.lastLiftLevel); Serial.print(" Pos: "); Serial.println(newPos);
displayMessageNumber("Mv To: ", persistentValues.lastLiftLevel + 1);
}
}
}
void loop() void loop()
{ {
Dcc.process();
//First check the Home Sensor and stop the motor if going in the down direction //First check the Home Sensor and stop the motor if going in the down direction
homeSensor.tick(); homeSensor.tick();
if(homeSensor.state()) if(homeSensor.state())