Library/Class: LDR_Overload_Class
Include File: <LDR_Overload_Class>
Constructor: LDR_Overload_class(pin) Pin where sensor is wired. Filter 1 second by default
LDR_Overload_Class(pin,secs) Secs is the delay of the filter. 1 second by default
Public Methods: begin( ) Initialises pin as output pulled high
train_over_sensor( ) Returns 1 for train present
Abstract: This page enhances (overloads) the C++ Class/Library developed in the page LDR_Sensor_Class to program the filter delay which by default is one second.
Target Audience: The target audience is programmers interested in Cpp overloading. In this case the same constructor but with a different number of arguments. The target hardware is a model train so model train enthusiasts can use the sample code.
Keywords: C++, C++ Class, C++ Method, C++ Instance, C++Library, C++ Overload, C++ Polymorphism, LDR, ESP8266, Arduino IDE, Arduino UNO, Arduino NANO
Required Components: ESP8266, Arduino NANO or UNO, 4 LDR sensors, 4 three aspect signals or 4 RED, 4 AMBER and 4 RED Leds, 4 1k ohm resistors, 4 10k ohm trim pots. Project was tested with both an ESP and Arduino)
Required Library: LDR_Sensor_Class: Code to be enhanced.
The LDR_Sensor_Class developed a C++ class to monitor a single LDR sensor. This class contained a public method/function train_over_sensor( ) that gave a "1" output when the train was over a LDR sensor. The special feature of this method was the 1 second filter to account for false readings when the train couplings, for example were over the sensor.
This page will allow the user to program the filter for different times.
1. For readers interested in C++ programs this HTML page/project demonstrates overloading the LDR_Sensor_Class developed in a previous page.
2. For model railway enthusiasts it enhances the LDR_Sensor_Class developed in a previous page. The code is written in C++.
This project uses the same circuit as used by the LDR_Sensor_Class.
The LDR sensor mounted on a railway sleeper.
One test circuit although an Arduino was also used.
The ESP8266 will read the output of the LDR sensor module on pin D0. The circuit was also tested using pin 3 of an Arduino NANO. Other projects used the developed library with an UNO verifying the validity of the developed class/library.
If readers are building on the page LDR_sensor_class and have the three files LDR_Sensor_Class.ino, LDR_Sensor_Class.h and LDR_Sensor_Class.cpp in the directory IDR_Sensor_Class they should open the sketch/program LDR_Sensor_Class. All three files should come up in the same window.
For readers who are starting from scratch they should create a new sketch/program IR_Sensor_Class.
1.create a new file and copy/paste the file LDR_Sensor_Class.ino. See LDR_Sensor_Class_code
2. create two new tabs (use inverted triangle on IDE near top right) with labels LDR_Sensor_Class.cpp and LDR_Sensor_Class.h
In either case confirm that everything compiles/verifies.
Save the project as LDR_Constructor_Class.
Rename the class/library/constructor where it occurs in all three files. I chose to rename to LDR_Overload_Class to be consistent with these pages.
That is edit-->find LDR_Sensor_Class then replace with LDR_Overload_Class. Use replace and find to step through each file.
The application, test, target or client program is shown below. When the constructor LDR_Constructor_Class sensor1( ) has only one argument this program is identical to the code presented in the page LDR_Sensor_Class
In the example using the NANO the sensor has been wired to pin A0 and the delay set to 5 seconds.
The header file defines a Class. The Class will define functions (methods) and variables that are available to users (Public) and those that the uses are not allowed to use (Private). The modified code is shown below. It will contain one extra line; the constructor with two arguments : LDR_Overload_Class(int pin, int delay_time)
#ifndef LDR_Overload_Class_h #define LDR_Overload_Class_hNote in the private area of the header file is a long variable delay4false that is initialised to 1000msec. The new constructor will change this value.
The next step is to generate the implementation file LDR_Constructor_Class.cpp to implement the functions/methods declared in the header file.
In this example there is a new constructor with two arguments: the pin as before and the delay_time. When two arguments are given in the test/application/client code the compiler will find this version of the constructor. In this case the private variable that was initially set to 1000ms is now over ridden and set by the argument delay_time (Multiplied by 1000 since the argument is in seconds while delay4false is in milli seconds).
The full implementation code is
#include "LDR_Overload_Class.h"