Back to Parent

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

int servoPin = 2; 
Servo Servo1; 

int howlongfaraway = 0;
int d = 100;
bool on = false;

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

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

  Servo1.attach(servoPin); 
  Servo1.write(80);
  
}
void loop(){ 
  Servo1.write(80);
  if (howlongfaraway >= 2000) {
    if(!on){
      Servo1.write(130); 
      delay(200);
      on = true;
      howlongfaraway = 0;
    }
  }

  if (APDS.proximityAvailable()) {
    // read the proximity
    // - 0   => close
    // - 255 => far
    // - -1  => error
    int proximity = APDS.readProximity();


    if (proximity > 200) {
      howlongfaraway += d;
    }
    if (proximity <= 200) {
      if(on) {
        Servo1.write(130); 
        delay(200);
        on = false;
      }
      howlongfaraway = 0;
    }

    Serial.println(howlongfaraway);
  }

  delay(d);

}
Click to Expand

Content Rating

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

0