SHARE
TWEET

Untitled

a guest Jan 23rd, 2020 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int trig = 9;
  2. const int echo = 10;
  3. long duration;
  4. int distance;
  5.  
  6. int photoSensor = A3;
  7. int photoSensorValue;
  8. int led = 3;
  9.  
  10. void setup(){
  11.   pinMode(trig, OUTPUT);
  12.   pinMode(echo, INPUT);
  13.   pinMode(led, OUTPUT);
  14.   pinMode(photoSensor, INPUT);
  15.   Serial.begin(9600);
  16. }
  17.  
  18. void loop(){
  19.   digitalWrite(trig, LOW);
  20.   delayMicroseconds(5);
  21.  
  22.   digitalWrite(trig, HIGH);
  23.   delayMicroseconds(10);
  24.   digitalWrite(trig, LOW);
  25.  
  26.   duration = pulseIn(echo, HIGH);
  27.   distance = duration*0.034/2;
  28.  
  29.   //DEBUG
  30.   Serial.print("Distance: ");
  31.   Serial.println(distance);
  32.   Serial.print("Photosens: ");
  33.   Serial.println(photoSensorValue);
  34.  
  35.   photoSensorValue = analogRead(photoSensor);
  36.   if(distance >= 150 && photoSensorValue < 40){
  37.     Serial.print("LIGHT");
  38.     digitalWrite(led, HIGH);
  39.     delay(1000);
  40.    
  41.   }else{
  42.     digitalWrite(led, LOW);
  43.     delay(200);
  44.   }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top