I have code for Light Controller/Saver using LDR/Photo diode which is working perfectly.
Planned to implement same code for Laser Security System, is responding as supposed to be.But What I have observed
if Object crosses Laser & LDR/Photo Diode Quickly.
System failed to detect those quick/short pulse from object.Please help me.
Blink without delay code was also tried but not fruitful
#define ENABLED true
#define DISABLED false
#define relayON HIGH
#define relayOFF LOW
const byte heartbeatLED = 13;
const byte relay = 5;
const byte photoTransistor = 2; //set up as an emitter follower
bool Flag = DISABLED;
byte lastPhotoTransistorState;
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long relayMillis;
unsigned long onMillis;
unsigned long onInterval = 3000ul; //3 seconds
//********************************************^************************************************
void setup ()
{
Serial.begin(9600);
pinMode(heartbeatLED, OUTPUT);
pinMode(relay, OUTPUT);
} //END of setup()
//********************************************^************************************************
void loop ()
{
//********************************* heartbeat TIMER
//is it time to toggle the heartbeatLED (every 500ms)?
if (millis() - heartbeatMillis >= 500ul)
{
//restart this TIMER
heartbeatMillis = millis();
//toggle the heartbeatLED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//********************************* checkSwitches TIMER
//is time to check our switches (every 2ms) ?
if (millis() - switchMillis >= 2)
{
//restart this TIMER
switchMillis = millis();
checkSwitches();
}
//********************************* relay TIMER
//if the relay is ON is it time to turn it OFF ?
if (Flag == ENABLED && millis() - relayMillis >= onInterval)
{
//disable this TIMER
Flag = DISABLED;
digitalWrite(relay, relayOFF);
}
//*********************************
//Other non blocking code goes here
//*********************************
} //END of loop()
//********************************************^************************************************
void checkSwitches()
{
byte currentState;
//********************************* photoTransistor
currentState = digitalRead(photoTransistor);
//did the sensor change state ?
if (lastPhotoTransistorState != currentState)
{
//update to the new state
lastPhotoTransistorState = currentState;
//if we are not timing, is the transistor turned OFF (emitter follower) ?
if (Flag == DISABLED && currentState == LOW)
{
//enable the relay TIMER
Flag = ENABLED;
//reset this TIMER
relayMillis = millis();
digitalWrite(relay, relayON);
}
} //END of this switch
//*********************************
} //END of checkSwitches()