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, int inSignalPin, const char *msg, float inSampleMax)
21 {
22  this->pin = pin;
23  this->signalPin = inSignalPin;
24  this->msg = msg;
25  this->current = 0;
26  this->currentSampleMax = inSampleMax;
27 } // CurrentMonitor::begin
28 
29 boolean CurrentMonitor::checkTime()
30 {
31  if(millis( ) - sampleTime < CURRENT_SAMPLE_TIME) // no need to check current yet
32  return(false);
33  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
34  return(true);
35 } // CurrentMonitor::checkTime
36 
37 void CurrentMonitor::check()
38 {
39  if (this->pin == UNDEFINED_PIN || this->signalPin == UNDEFINED_PIN)
40  return;
41 
42  this->current = (float)(analogRead(this->pin) * CURRENT_SAMPLE_SMOOTHING + this->current * (1.0 - CURRENT_SAMPLE_SMOOTHING)); // compute new exponentially-smoothed current
43 
44  // current overload and Signal is on
45  if (this->current > this->currentSampleMax && digitalRead(this->signalPin) == HIGH)
46  {
47  digitalWrite(this->signalPin, LOW);
48  DCCPP_INTERFACE.print(this->msg); // print corresponding error message
49 #if !defined(USE_ETHERNET)
50  DCCPP_INTERFACE.println("");
51 #endif
52  }
53 } // CurrentMonitor::check
54 
55 long int CurrentMonitor::sampleTime=0;
56 
57 #endif