Back to Parent

#include <Arduino_APDS9960.h>
// int ledState = LOW;
int val = 0;  // variable to store the read value
int relayPin = D2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial)
    ;
  //initialize APDS9960 library
  if (!APDS.begin()) {
    Serial.println("Error initializing APDS9960 sensor!");
  }

  //set LEDs to behave as OUTPUT, turn all off
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:

  // read the proximity if available
  if (APDS.proximityAvailable()) {
    //set int proximity to value detected
    int proximity = APDS.readProximity();
    //print value to serial monitor
    Serial.println(APDS.readProximity());

    //object far?
    if (proximity > 244) {
      analogWrite(LEDR, 255);  //255 = off
      analogWrite(LEDB, 255);
      analogWrite(LEDG, proximity);

      Serial.println("false");  // Switch Relay Off (NC)
      digitalWrite(relayPin, LOW);
    }

    //object medium?
    else if (proximity > 240 && proximity <= 244) {
      analogWrite(LEDR, 255);
      analogWrite(LEDB, proximity);
      analogWrite(LEDG, proximity);

      Serial.println("false");  // Switch Relay Off (NC)
      digitalWrite(relayPin, LOW);
      delay(1000);
      Serial.println("true");  // Switch Relay On (NO)
      digitalWrite(relayPin, HIGH);
      delay(1000);
    }

    //object close?
    else if (proximity > 225 && proximity <= 240) {
      analogWrite(LEDR, proximity);
      analogWrite(LEDB, 255);
      analogWrite(LEDG, proximity);

      Serial.println("false");  // Switch Relay Off (NC)
      digitalWrite(relayPin, LOW);
      delay(500);
      Serial.println("true");  // Switch Relay On (NO)
      digitalWrite(relayPin, HIGH);
      delay(750);
    }

    //object warning close?
    else if (proximity > 150 && proximity <= 225) {
      analogWrite(LEDR, 100);
      analogWrite(LEDB, 255);
      analogWrite(LEDG, proximity);

      Serial.println("false");  // Switch Relay Off (NC)
      digitalWrite(relayPin, LOW);
      delay(75);
      Serial.println("true");  // Switch Relay On (NO)
      digitalWrite(relayPin, HIGH);
      delay(100);
    }

    //object danger close?
    else if (proximity > 50 && proximity <= 150) {
      analogWrite(LEDR, 25);
      analogWrite(LEDB, 255);
      analogWrite(LEDG, proximity);

      Serial.println("false");  // Switch Relay Off (NC)
      digitalWrite(relayPin, LOW);
      delay(50);
      Serial.println("true");  // Switch Relay On (NO)
      digitalWrite(relayPin, HIGH);
      delay(50);
    }

    //object too close
    else {
      analogWrite(LEDR, 0);
      analogWrite(LEDB, 255);
      analogWrite(LEDG, 255);

      Serial.println("true");  // Switch Relay On (NO), no current open loop
      digitalWrite(relayPin, HIGH);
    }
  }
}
Click to Expand

Content Rating

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

0