/* The following program "IR_Test" is used to evaluate the performance/ * limitations of using IR (Infrared) to detect model trains * The design is a forerunner of a WiFi project so uses an ESP8266 but * an Arduino could just as easily be used. * ______________ ____________ * | |______________3.3V_| ESP8266 | * | IR Sensor | | | * | Module | | |------------- * | | | | USB * | | | | Arduino IDE * | | | | Supplies 5V * | | | |------------- * | | | | * | |OUT-------------D1-| | on ESP8266 * | | | | * | |______________ Gnd_| | * |____________| |___________| * * A IR Sensor is mounted between two sleepers of a test section of track and two * trucks moved across the Tx and Rx and the "voltage" observed on the serial * monitor of the Arduino IDE. */
#define FLASH_ON digitalWrite(BLUE_LED,LOW); //note negative logic#define FLASH_OFF digitalWrite(BLUE_LED,HIGH);int BLUE_LED = D4; //use for debugging/evaluating performance
#define DELTA_TIME 100 //frequency of updateslong int start_time; //start time of delay loop
enum state {wait4train, wait4end} train_state;int delay_count;#define delay4false 10 //false exits due to couplings - no reflectionvoid setup( ){ Serial.begin(115200); //baud rate serial monitor Serial.print("\Testing IR Sensor as a train detector"); pinMode(BLUE_LED,OUTPUT); train_state = wait4train; FLASH_OFF}/* Read the digital voltage every cycle * The digital will be 0 for the train present * 1 for no train */void loop() { if (millis( ) -start_time > DELTA_TIME){ switch (train_state) { case wait4train : if (digitalRead(D1) ==0) //wait for train to arrive { FLASH_ON train_state = wait4end; delay_count = 0; } break; case wait4end : //Serial.print(digitalRead(D1)); //profile train delay_count++; Serial.print(delay_count); Serial.print(','); //test exit if (digitalRead(D1) == 0) //detected train so restart loop { delay_count = 0;} else if (delay_count >= delay4false) { train_state = wait4train; //ready for next train delay_count = 0; FLASH_OFF //train passed } break; default : Serial.print('?'); //error } start_time = millis( ); } }