I've written a simple sketch to detect an object passing in front of a light sensor (LDR in the code). When the sensor detects an object it moves from the "scanning" to "object detected" function, and when the object clears the sensor the program runs the "warning" function, pauses, then returns to the "scanning" function.
int LDR=0;
int LDRValue = 0;
int previousLDRValue=255;
int previousLDRValue2=0;
int light_sensitivity = 200;
void setup() {
{
Serial.begin(9600);
}
}
void loop() {
scanning();
}
void scanning() {
LDRValue = map(analogRead(LDR), 0, 1023, 0, 255); //reads the ldr's value through LDR which we have set to Analog inut 0 "A0"
Serial.println(LDRValue); //prints the LDR values to serial monitor
Serial.println("reversing");
delay(100); //This is the speed at which LDR sends value to Arduino
if (LDRValue < light_sensitivity) {
if (previousLDRValue > light_sensitivity) {
object_detected();
}
}
previousLDRValue = LDRValue; //save light sensor state
}
void object_detected() {
LDRValue = map(analogRead(LDR), 0, 1023, 0, 255); //reads the ldr's value through LDR which we have set to Analog inut 0 "A0"
Serial.println(LDRValue); //prints the LDR values to serial monitor
Serial.println("object detected");
delay(100); //This is the speed at which LDR sends value to Arduino
if (LDRValue > light_sensitivity) {
if (previousLDRValue2 < light_sensitivity) {
warning();
}
}
else {
object_detected();
}
previousLDRValue2 = LDRValue; //save light sensor state
}
void warning() {
Serial.println("warning");
delay(3000); //pause
scanning(); //go back to scanning
}
This all works fine.... once. After the "warning" function, the arduino goes back to the scanning function, like it should. Then it detects the object again, like it should. But once the sensor is cleared, it does not display the warning again - it goes straight back to the scanning function.
I'm really scratching my head here. I think I've missed something really obvious. Any ideas?
Try this to aid in finding out where the problem is.
I used LDRValue = 100 and the code got into infinite loop due to recursive code - see NOTE.
That will cause problems.
I need to add some more "break points " to emulate problem as you described it. Stand by.
void scanning() {
LDRValue = map(analogRead(LDR), 0, 1023, 0, 255); //reads the ldr's value through LDR which we have set to Analog inut 0 "A0"
force to known level
LDRValue = 205;
Serial.println(LDRValue); //prints the LDR values to serial monitor
Serial.println("reversing");
delay(100); //This is the speed at which LDR sends value to Arduino
so why the delay here? you already read the sensor
if (LDRValue < light_sensitivity) {
if (previousLDRValue > light_sensitivity) {
object_detected();
}
}
previousLDRValue = LDRValue; //save light sensor state
for(;;);
}
Recursive when LDRValue = 100
void object_detected() {
LDRValue = map(analogRead(LDR), 0, 1023, 0, 255); //reads the ldr's value through LDR which we have set to Analog inut 0 "A0"
Serial.println(LDRValue); //prints the LDR values to serial monitor
Serial.println("object detected");
delay(100); //This is the speed at which LDR sends value to Arduino
if (LDRValue > light_sensitivity) {
if (previousLDRValue2 < light_sensitivity) {
warning();
}
}
else {
object_detected();
Recursive when LDRValue = 100
}
previousLDRValue2 = LDRValue; //save light sensor state
for(;;);
}
is there a simple way to add negative pulse detection length into this code?
What do you mean by "negative pulse detection length"? The Arduino can not handle negative voltage, so, by definition, negative pulses are not possible. Measuring the length of something that doesn't exist won't take long.