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)
17 {
18  this->pin = pin;
19  this->msg = msg;
20  this->current = 0;
21 } // CurrentMonitor::begin
22 
23 boolean CurrentMonitor::checkTime()
24 {
25  if(millis( ) - sampleTime < CURRENT_SAMPLE_TIME) // no need to check current yet
26  return(false);
27  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
28  return(true);
29 } // CurrentMonitor::checkTime
30 
31 void CurrentMonitor::check()
32 {
33  if (this->pin == 255)
34  return;
35  this->current = (float) (analogRead(this->pin) * CURRENT_SAMPLE_SMOOTHING + this->current * (1.0-CURRENT_SAMPLE_SMOOTHING)); // compute new exponentially-smoothed current
36  int signalPin = DCCppConfig::SignalEnablePinProg;
37  if (signalPin == 255)
38  signalPin = DCCppConfig::SignalEnablePinMain;
39 
40  // current overload and Prog Signal is on (or could have checked Main Signal, since both are always on or off together)
41  if (this->current > CURRENT_SAMPLE_MAX && digitalRead(signalPin) == HIGH)
42  {
43  if (DCCppConfig::SignalEnablePinProg != 255)
44  digitalWrite(DCCppConfig::SignalEnablePinProg, LOW); // disable both Motor Shield Channels
45  if (DCCppConfig::SignalEnablePinMain != 255)
46  digitalWrite(DCCppConfig::SignalEnablePinMain, LOW); // regardless of which caused current overload
47  INTERFACE.print(this->msg); // print corresponding error message
48  }
49 } // CurrentMonitor::check
50 
51 long int CurrentMonitor::sampleTime=0;
52