Skip to content

Commit 26b345f

Browse files
committed
Fixed bug in short-circuit detection for MEGA
The short-circuit code polled the track current every 10 “milliseconds.” But Arduino millis() is based on TIMER-0 settings which are changed in DCC++ Base Station. The actual polling was closer to every 1 millisecond. When Base Station for the Mega for updated to utilize TIMER-3 instead of TIMER-0, the millis() function now really did count milliseconds, and 10 milliseconds between each track-current sampling was too long. Changed the code so that for the Mega, the current sampling time is defined as 1 (instead of 10), representing an actual 1 millisecond as desired.
1 parent a21c978 commit 26b345f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

DCCpp_Uno/CurrentMonitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ CurrentMonitor::CurrentMonitor(int pin, char *msg){
2222
boolean CurrentMonitor::checkTime(){
2323
if(millis()-sampleTime<CURRENT_SAMPLE_TIME) // no need to check current yet
2424
return(false);
25-
sampleTime=millis();
25+
sampleTime=millis(); // note millis() uses TIMER-0. For UNO, we change the scale on Timer-0. For MEGA we do not. This means CURENT_SAMPLE_TIME is different for UNO then MEGA
2626
return(true);
2727
} // CurrentMonitor::checkTime
2828

DCCpp_Uno/CurrentMonitor.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ Part of DCC++ BASE STATION for the Arduino
1313
#include "Arduino.h"
1414

1515
#define CURRENT_SAMPLE_SMOOTHING 0.01
16-
#define CURRENT_SAMPLE_TIME 10
1716
#define CURRENT_SAMPLE_MAX 300
1817

18+
#ifdef ARDUINO_AVR_UNO // Configuration for UNO
19+
#define CURRENT_SAMPLE_TIME 10
20+
#else // Configuration for MEGA
21+
#define CURRENT_SAMPLE_TIME 1
22+
#endif
23+
1924
struct CurrentMonitor{
2025
static long int sampleTime;
2126
int pin;

0 commit comments

Comments
 (0)