DCCpp
This is the library version of a program for Arduino to control railroading DCC devices.
Sensor Struct Reference

#include "Sensor.h"

Public Member Functions

void begin (int snum, int pin, int pullUp)
 
void set (int snum, int pin, int pullUp)
 
boolean isActive ()
 

Static Public Member Functions

static Sensor * get (int snum)
 
static void remove (int snum)
 
static int count ()
 
static void check ()
 
static void load ()
 
static void store ()
 
static void parse (char *c)
 
static Sensor * create (int snum, int pin, int pullUp)
 

Public Attributes

SensorData data
 
boolean active
 
float signal
 
Sensor * nextSensor
 

Static Public Attributes

static Sensor * firstSensor =NULL
 

Detailed Description

DCC++ BASE STATION supports Sensor inputs that can be connected to any Arduino Pin not in use by this program. Sensors can be of any type (infrared, magnetic, mechanical...). The only requirement is that when "activated" the Sensor must force the specified Arduino Pin LOW (i.e. to ground), and when not activated, this Pin should remain HIGH (e.g. 5V), or be allowed to float HIGH if use of the Arduino Pin's internal pull-up resistor is specified. Be careful, don't use Arduino's internal pull-up resistors for external infrared sensors: each sensor must have its own 1K external pull-up resistor.

To ensure proper voltage levels, some part of the Sensor circuitry MUST be tied back to the same ground as used by the Arduino.

The Sensor code below utilizes exponential smoothing to "de-bounce" spikes generated by mechanical switches and transistors. This avoids the need to create smoothing circuitry for each sensor. You may need to change these parameters through trial and error for your specific sensors.

To have this sketch monitor one or more Arduino pins for sensor triggers, first define/edit/delete sensor definitions using one on the variation of the "S" command.

Once all sensors have been properly defined, use the <E> command to store their definitions to EEPROM. If you later make edits/additions/deletions to the sensor definitions, you must invoke the <E> command if you want those new definitions updated in the EEPROM. You can also clear everything stored in the EEPROM by invoking the <e> command.

All sensors defined as per above are repeatedly and sequentially checked within the main loop of this sketch. If a Sensor Pin is found to have transitioned from one state to another, one of the following serial messages are generated:

  • <Q ID> for transition of Sensor ID from HIGH state to LOW state (i.e. the sensor is triggered)
  • <q ID> for transition of Sensor ID from LOW state to HIGH state (i.e. the sensor is no longer triggered)

Depending on whether the physical sensor is acting as an "event-trigger" or a "detection-sensor," you may decide to ignore the <q ID> return and only react to <Q ID> triggers.

Definition at line 115 of file Sensor.h.

Member Function Documentation

void begin ( int  snum,
int  pin,
int  pullUp 
)

Initialize the sensor created by regular C++ instantiation.

Parameters
snumThe numeric ID (0-32767) of the sensor.
pinThe arduino pin number to use for the sensor.
pullUp1 = use internal pull-up resistor for pin, 0 = don't use internal pull-up resistor for pin.
Remarks
Should not be used in command line mode.

Definition at line 24 of file Sensor.cpp.

void set ( int  snum,
int  pin,
int  pullUp 
)

Force the sensor values created by command line.

Parameters
snumThe numeric ID (0-32767) of the sensor.
pinThe arduino pin number to use for the sensor.
pullUp1 = use internal pull-up resistor for pin, 0 = don't use internal pull-up resistor for pin.
Remarks
Should not be used in C++ mode.

Definition at line 53 of file Sensor.cpp.

Sensor * get ( int  snum)
static

Get a particular sensor.

Parameters
snumThe numeric ID (0-32767) of the sensor.
Returns
The found sensor or NULL if not found.
Remarks
Only available when USE_EEPROM or USE_TEXTCOMMAND is defined.

Definition at line 71 of file Sensor.cpp.

void remove ( int  snum)
static

Removes a particular sensor.

Parameters
snumThe numeric ID (0-32767) of the sensor.
Remarks
Only available when USE_EEPROM or USE_TEXTCOMMAND is defined.

Definition at line 78 of file Sensor.cpp.

int count ( )
static

Get the total number of sensors.

Returns
Number of sensors.
Remarks
Only available when USE_EEPROM or USE_TEXTCOMMAND is defined.

Definition at line 110 of file Sensor.cpp.

void check ( )
static

Checks if the associated pin is LOW or HIGH and update the 'signal' field with a pondered value.

Definition at line 120 of file Sensor.cpp.

boolean isActive ( )

Checks if the sensor is activated or not.

Returns
True if the sensor is activated. Otherwise false (of course...).

Definition at line 158 of file Sensor.h.

void load ( )
static

Load the content of this sensor from EEPROM.

Remarks
Only available when USE_EEPROM is defined.

Definition at line 200 of file Sensor.cpp.

void store ( )
static

Store the content of this sensor in EEPROM.

Remarks
Only available when USE_EEPROM is defined.

Definition at line 227 of file Sensor.cpp.

void parse ( char *  c)
static

Do command according to the given command string.

Parameters
cstring to interpret... See syntax in class description.
Remarks
Only available when USE_TEXTCOMMAND is defined.

Definition at line 249 of file Sensor.cpp.

Sensor * create ( int  snum,
int  pin,
int  pullUp 
)
static

Creates a new sensor in the list..

Parameters
snumThe numeric ID (0-32767) of the sensor.
pinThe arduino pin number to use for the sensor.
pullUp1 = use internal pull-up resistor for pin, 0 = don't use internal pull-up resistor for pin.
Remarks
Should not be used in C++ mode.
Only available when USE_TEXTCOMMAND is defined.

Definition at line 281 of file Sensor.cpp.

Member Data Documentation

Sensor * firstSensor =NULL
static

Address of the first object of this class. NULL means empty list of sensors. Do not change it !

Definition at line 116 of file Sensor.h.

SensorData data

Data associated to this output.

Definition at line 117 of file Sensor.h.

boolean active

True if this sensor is active.

Definition at line 118 of file Sensor.h.

float signal

Last read value of this sensor.

Definition at line 119 of file Sensor.h.

Sensor* nextSensor

Address of the next object of this class. NULL means end of the list of sensors. Do not change it !

Definition at line 120 of file Sensor.h.