Skip to content

Commit

Permalink
Fixed bug in short-circuit detection for MEGA
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DccPlusPlus committed Dec 20, 2015
1 parent a21c978 commit 26b345f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DCCpp_Uno/CurrentMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CurrentMonitor::CurrentMonitor(int pin, char *msg){
boolean CurrentMonitor::checkTime(){
if(millis()-sampleTime<CURRENT_SAMPLE_TIME) // no need to check current yet
return(false);
sampleTime=millis();
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
return(true);
} // CurrentMonitor::checkTime

Expand Down
7 changes: 6 additions & 1 deletion DCCpp_Uno/CurrentMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ Part of DCC++ BASE STATION for the Arduino
#include "Arduino.h"

#define CURRENT_SAMPLE_SMOOTHING 0.01
#define CURRENT_SAMPLE_TIME 10
#define CURRENT_SAMPLE_MAX 300

#ifdef ARDUINO_AVR_UNO // Configuration for UNO
#define CURRENT_SAMPLE_TIME 10
#else // Configuration for MEGA
#define CURRENT_SAMPLE_TIME 1
#endif

struct CurrentMonitor{
static long int sampleTime;
int pin;
Expand Down

0 comments on commit 26b345f

Please sign in to comment.