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 "Arduino.h"
11 
12 #ifdef ARDUINO_ARCH_AVR
13 
14 #include "DCCpp_Uno.h"
15 #include "CurrentMonitor.h"
16 #include "Comm.h"
17 
19 
20 void CurrentMonitor::begin(int pin, const char *msg, float inSampleMax)
21 {
22  this->pin = pin;
23  this->msg = msg;
24  this->current = 0;
25  this->currentSampleMax = inSampleMax;
26 } // CurrentMonitor::begin
27 
28 boolean CurrentMonitor::checkTime()
29 {
30  if(millis( ) - sampleTime < CURRENT_SAMPLE_TIME) // no need to check current yet
31  return(false);
32  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
33  return(true);
34 } // CurrentMonitor::checkTime
35 
36 void CurrentMonitor::check()
37 {
38  if (this->pin == UNDEFINED_PIN)
39  return;
40 
41  this->current = (float)(analogRead(this->pin) * CURRENT_SAMPLE_SMOOTHING + this->current * (1.0 - CURRENT_SAMPLE_SMOOTHING)); // compute new exponentially-smoothed current
42 
43  int signalPin = DCCppConfig::SignalEnablePinProg;
44  if (signalPin == UNDEFINED_PIN)
45  signalPin = DCCppConfig::SignalEnablePinMain;
46 
47  // current overload and Programming Signal is on (or could have checked Main Signal, since both are always on or off together)
48  if (this->current > this->currentSampleMax && digitalRead(signalPin) == HIGH)
49  {
51  DCCPP_INTERFACE.print(this->msg); // print corresponding error message
52 #if !defined(USE_ETHERNET)
53  DCCPP_INTERFACE.println("");
54 #endif
55  }
56 } // CurrentMonitor::check
57 
58 long int CurrentMonitor::sampleTime=0;
59 
60 #endif
static void powerOff(bool inMain = true, bool inProg = true)
Definition: DCCpp.cpp:561