How to control something with shadow

As you probably have seen people making stuffs which controlled by lights, like street lights are controlled by sun light and some robot cars that follows lights only. But in this post I want to share something which may seems odd but might be interesting because it controlled by shadow.
Ad

How does it work?
Well, it is ultimately controlled by light actually. Because the sketch below first measure the brightness of the light, and then when you set a certain measurement that if brightness of the light goes below that level it will turn on the led and that's where shadow comes in.

Any object that blocks the light and make it less bright on the photoresistor, we see as a shadow and photoresistor sees as less bright light.
Ad

The sketch

    
    int photocellPin1 = 1;     // the cell and 10K pulldown are connected to a1
    int photocellReading1;     // the analog reading from the sensor divider
    int led1 = 2;          // connect LED to pin D2

    int photocellPin2 = 2;    
    int photocellReading2;
    int led2 = 3;

    int photocellPin3 = 3;    
    int photocellReading3;
    int led3 = 4;

    int photocellPin4 = 4;    
    int photocellReading4;
    int led4 = 5;

    int photocellPin5 = 5;    
    int photocellReading5;
    int led5 = 6;


    void setup() {
      pinMode(led1, OUTPUT);
      pinMode(led2, OUTPUT);
      pinMode(led3, OUTPUT);
      pinMode(led4, OUTPUT);
      pinMode(led5, OUTPUT);

      Serial.begin(9600);  
    }
    
    void pca() {
      photocellReading1 = analogRead(photocellPin1);      
     
if (photocellReading1 <=500){
    digitalWrite(led1, HIGH);
  }
  else {
    digitalWrite(led1, LOW);
  } 
}

    void pcb() {
      photocellReading2 = analogRead(photocellPin2);            
if (photocellReading2 <=500){
    digitalWrite(led2, HIGH);
  }
  else {
    digitalWrite(led2, LOW);
  } 
}

    void pcc() {
      photocellReading3 = analogRead(photocellPin3);            
if (photocellReading3 <=500){
    digitalWrite(led3, HIGH);
  }
  else {
    digitalWrite(led3, LOW);
  } 
}

    void pcd() {
      photocellReading4 = analogRead(photocellPin4);            
if (photocellReading4 <=500){
    digitalWrite(led4, HIGH);
  }
  else {
    digitalWrite(led4, LOW);
  } 
}

    void pce() {
      photocellReading5 = analogRead(photocellPin5);            
if (photocellReading5 <=500){
    digitalWrite(led5, HIGH);
  }
  else {
    digitalWrite(led5, LOW);
  } 
}

void loop() {
pca();
pcb();
pcc();
pcd();
pce();
delay(50);
}

Note:
Remember because this catch hast to get the measurement from 5 different photoresistor so I got rid of the print code that has increased the speed. You might have to use it at least for one to set the appropriate number. So here is the code
......
Serial.print("First photocell reading = ");
      Serial.println(photocellReading1);     // the raw analog reading
.......
just copy the code between dots and paste it right under the photocellReading1 = analogRead(photocellPin1);   and then reupload the sketch.



Ad


No comments:

Post a Comment



Newly posted:
Reason why rich getting richer and poor getting poorer