Back to Parent

#include <Arduino_APDS9960.h>
#include <Servo.h>


int servoPin = D7;
bool running = false;
Servo servo;


void setup() {
 Serial.begin(9600);


 // this is like pinMode for a servo!
 servo.attach(servoPin);


 while (!Serial);
 if (!APDS.begin()) {
   Serial.println("Error initializing APDS-9960 sensor!");
 }
}


void loop() {
 // check if a proximity reading is available
 if (APDS.proximityAvailable()) {
   // read the proximity
   // - 0   => close
   // - 255 => far
   // - -1  => error
   int proximity = APDS.readProximity();
  
   // print value to the Serial Monitor
   Serial.println(proximity);


   // far
   if(proximity>240){
     digitalWrite(LEDB,HIGH);
     // if on -> TURN OFF
     if(running==true){
       servo.write(180);
       delay(400);
       servo.write(150);
       delay(400);
       // servo.write(180);
       // delay(300);
       // servo.write(100);
       running = false;
     }
   }
   // close -> TURN ON
   else{
     if(running==false){
       digitalWrite(LEDB,LOW);
       servo.write(180);
       delay(400);
       servo.write(150);
       running = true;
     }
   }
 }
 // wait a bit before reading again
 delay(100);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0