Adaptive LDR Arduino

Hello! I am currently trying something but I don't know if it's possible to do. I want the photoresistor to count whenever it gets dark or when it is casted by a shadow. However, I am trying if it can work even with little light environment? Will it be able to count the darkness that past through the photoresistor?

Here's the code and the arduino circuit using tinkercad

// C++ code
//
int S0 = 13;
int L0 = 3;
int L1 = 2;
int a = 9;
int b = 12;
int c = 11;
int d = 10;
int i = 0;
int current = 0;
int last = 0;
String SD;

void setup()
{
  pinMode(S0, INPUT);
  pinMode(A0, INPUT);
  pinMode(L0, OUTPUT);
  pinMode(L1, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  S0 = digitalRead(13);
  current = analogRead(A0);
  if (S0 == 1) 
  {
    SD = "ON";
    if (current != last)
        {
         	 if (current <= 6)
             {
              	i = i+1; 
             }
          delay(50);
          last = current;
      
     		 if (i > 9)
      		 {
       			i = 9; 
      		 }
        }
  } 
else 
  {
    SD = "OFF";
    if (current != last)
        {
         	 if (current <= 6)
             {
              	i = i-1; 
             }
          delay(50);
          last = current;
      
           	 if (i < 0)
      		 {
       			i = 0; 
      		 }
        }
  }
  delay(10); // Delay a little bit to improve simulation performance
  Serial.print("Switch Status: ");
  Serial.println(SD);
  Display(i);
}

void Display(int i)
    {
  		if (i == 9)
        {
         	i = 9; 
        }
     	if (i == 0) 
		{
		  digitalWrite(d, LOW);
		  digitalWrite(c, LOW);
		  digitalWrite(b, LOW);
		  digitalWrite(a, LOW);
		  digitalWrite(L0, HIGH);
		}
		if (i == 1) 
		{
		  digitalWrite(d, LOW);
		  digitalWrite(c, LOW);
		  digitalWrite(b, LOW);
		  digitalWrite(a, HIGH);
		  digitalWrite(L0, LOW);
		}
		if (i == 2) 
		{
		  digitalWrite(d, LOW);
		  digitalWrite(c, LOW);
		  digitalWrite(b, HIGH);
		  digitalWrite(a, LOW);
		}
		if (i == 3) 
		{
		  digitalWrite(d, LOW);
		  digitalWrite(c, LOW);
		  digitalWrite(b, HIGH);
		  digitalWrite(a, HIGH);
		}
		if (i == 4) 
		{
		  digitalWrite(d, LOW);
		  digitalWrite(c, HIGH);
		  digitalWrite(b, LOW);
		  digitalWrite(a, LOW);
		}
		if (i == 5) 
		{
		  digitalWrite(d, LOW);
		  digitalWrite(c, HIGH);
		  digitalWrite(b, LOW);
		  digitalWrite(a, HIGH);
		}
		if (i == 6) 
		{
		  digitalWrite(d, LOW);
		  digitalWrite(c, HIGH);
		  digitalWrite(b, HIGH);
		  digitalWrite(a, LOW);
		}
		if (i == 7) 
		{
		  digitalWrite(d, LOW);
		  digitalWrite(c, HIGH);
		  digitalWrite(b, HIGH);
		  digitalWrite(a, HIGH);
		}
		if (i == 8) 
		{
		  digitalWrite(d, HIGH);
		  digitalWrite(c, LOW);
		  digitalWrite(b, LOW);
		  digitalWrite(a, LOW);
		  digitalWrite(L0, LOW);
		  digitalWrite(L1, LOW);
		}
		if (i == 9) 
		{
		  digitalWrite(d, HIGH);
		  digitalWrite(c, LOW);
		  digitalWrite(b, LOW);
		  digitalWrite(a, HIGH);
		  digitalWrite(L0, LOW);
		  digitalWrite(L1, HIGH);
		} 
}

I guess you will just have to test it with what you consider "darkness".
Paul

Usually, LDR's aren't very appropriate for this, no. But you'd have to try, as @Paul_KD7HB says.

Errr....what? Darkness passing through a photoresistor...? Sounds pretty sci-fi...

Likely caused by negative energy.
Paul

I didn't really "study" your code but this
if (current != last)
"never works". Or rather, it's almost always true that two analog readings won't be exactly the same.

You usually have to look for a "close" value or greater or less than, etc.

You probably need some hysteresis. The heater in your house is an example - If heat is set to come-on below 70 degrees (F) it might come-on at 69 degrees. Then it stays on until the temperature hits 71, then shuts-off. Then doesn't some back on again until the temperature drops to 69. (It's not always one degree high or low, but that's the idea of hysteresis. )

If you haven't done so, you can run the code from the Analog Read Serial Example to "see" your light & dark readings so you can choose your thresholds.

My bad. What I mean is when the photoresistor reading drops or when a shadow pass through the resistor

I'm also thinking this. I'm thinking that as long as it got the required value, it will count regardless whether it has less light in the environment. I wish I could try this in the physical circuit to see what will happen

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.