//LDR_Test.ino
/* The following program "LDR_Test" is used to evaluate the performance/* limitations of using LDRs 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.* * A LDR is mounted on a sleeper of a test section of track and two* trucks moved across the LDR and the "voltage" observed on the serial* monitor of the Arduino IDE.* The potentiometer should be adjusted for optimum performance. * ideally a solid HIGH and solid LOw*/
#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 STATIC 0#if STATIC #define DELTA_TIME 500 //frequency of updates#else #define DELTA_TIME 100#endif long int start_time; //start time of delay loop
void setup( ){ Serial.begin(115200); //baud rate serial monitorSerial.print("\Testing LDR as a train detector"); pinMode(BLUE_LED,OUTPUT);}/* Read the analog and digital votage every 500ms* The analog is 10 bits so the reading willl be >0 and < 1024* The digital will be 0 for the LDR in the open (No train)* threshold < 512 analog reading* 1 for the LDR covered (train)* threshold > 512 analog reading* Vary potentiometer for different results */void loop() {if (millis( ) -start_time > DELTA_TIME){FLASH_ON#if STATICSerial.print("Analog Output from LDR Sensor: ");Serial.print(analogRead(A0));Serial.print(" Digital Output: ");Serial.println(digitalRead(D0));delay(100); //gives a pretty solid flash#elseSerial.print(digitalRead(D0));#endifFLASH_OFF start_time = millis( ); } }