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 
24 boolean CurrentMonitor::checkTime()
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 
32 void CurrentMonitor::check()
33 {
34  if (this->pin == UNDEFINED_PIN)
35  return;
36  this->current = (float) (analogRead(this->pin) * CURRENT_SAMPLE_SMOOTHING + this->current * (1.0-CURRENT_SAMPLE_SMOOTHING)); // compute new exponentially-smoothed current
37  int signalPin = DCCppConfig::SignalEnablePinProg;
38  if (signalPin == UNDEFINED_PIN)
39  signalPin = DCCppConfig::SignalEnablePinMain;
40 
41  // current overload and Prog Signal is on (or could have checked Main Signal, since both are always on or off together)
42  if (this->current > this->currentSampleMax && digitalRead(signalPin) == HIGH)
43  {
44  if (DCCppConfig::SignalEnablePinProg != UNDEFINED_PIN)
45  digitalWrite(DCCppConfig::SignalEnablePinProg, LOW); // disable both Motor Shield Channels
46  if (DCCppConfig::SignalEnablePinMain != UNDEFINED_PIN)
47  digitalWrite(DCCppConfig::SignalEnablePinMain, LOW); // regardless of which caused current overload
48  INTERFACE.print(this->msg); // print corresponding error message
49 #if !defined(USE_ETHERNET)
50  INTERFACE.println("");
51 #endif
52  }
53 } // CurrentMonitor::check
54 
55 long int CurrentMonitor::sampleTime=0;
56