DCCpp
This is the library version of a program for Arduino to control railroading DCC devices.
CurrentMonitor.cpp
1 /**********************************************************************
2 
3 CurrentMonitor.cpp
4 COPYRIGHT (c) 2013-2016 Gregg E. Berman
5 
6 Part of DCC++ BASE STATION for the Arduino
7 
8 **********************************************************************/
9 
10 #include "DCCpp_Uno.h"
11 #include "CurrentMonitor.h"
12 #include "Comm.h"
13 
15 
16 void CurrentMonitor::begin(int pin, const char *msg, float inSampleMax)
17 {
18  this->pin = pin;
19  this->msg = msg;
20  this->current = 0;
21  this->currentSampleMax = inSampleMax;
22 } // CurrentMonitor::begin
23 
25 {
26  if(millis( ) - sampleTime < CURRENT_SAMPLE_TIME) // no need to check current yet
27  return(false);
28  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
29  return(true);
30 } // CurrentMonitor::checkTime
31 
33 {
34  if (this->pin == UNDEFINED_PIN)
35  return;
36 
37  this->current = (float)(analogRead(this->pin) * CURRENT_SAMPLE_SMOOTHING + this->current * (1.0 - CURRENT_SAMPLE_SMOOTHING)); // compute new exponentially-smoothed current
38 
39  int signalPin = DCCppConfig::SignalEnablePinProg;
40  if (signalPin == UNDEFINED_PIN)
41  signalPin = DCCppConfig::SignalEnablePinMain;
42 
43  // current overload and Programming Signal is on (or could have checked Main Signal, since both are always on or off together)
44  if (this->current > this->currentSampleMax && digitalRead(signalPin) == HIGH)
45  {
47  INTERFACE.print(this->msg); // print corresponding error message
48 #if !defined(USE_ETHERNET)
49  INTERFACE.println("");
50 #endif
51  }
52 } // CurrentMonitor::check
53 
55 
static void powerOff()
Definition: DCCpp.cpp:560
static long int sampleTime
static boolean checkTime()
float currentSampleMax
const char * msg
void begin(int pin, const char *msg, float inSampleMax = 300)