// set the LEDs pins as outputs
pinMode(LEDR,OUTPUT);pinMode(LEDG,OUTPUT);pinMode(LEDB,OUTPUT);// turn all the LEDs off
digitalWrite(LEDR,HIGH);digitalWrite(LEDG,HIGH);digitalWrite(LEDB,HIGH);
unsignedlongcurrentMillis=millis();// check if a proximity reading is available
if(APDS.proximityAvailable()){// read the proximity
// - 0 => close
// - 255 => far
// - -1 => error
intproximity=APDS.readProximity();
if(proximity>150){if(currentMillis-previousMillis>=intervalLong){previousMillis=currentMillis;// if the LED is off turn it on and vice-versa:
if(ledState==LOW){ledState=HIGH;}else{ledState=LOW;}// set the green LED with the ledState of the variable and turn off the rest
digitalWrite(LEDG,ledState);digitalWrite(LEDR,HIGH);digitalWrite(LEDB,HIGH);}}
elseif(proximity>50&&proximity<=150){if(currentMillis-previousMillis>=intervalMed){previousMillis=currentMillis;// if the LED is off turn it on and vice-versa:
if(ledState==LOW){ledState=HIGH;}else{ledState=LOW;}// set the blue LED with the ledState of the variable and turn off the rest
digitalWrite(LEDB,ledState);digitalWrite(LEDR,HIGH);digitalWrite(LEDG,HIGH);}}
else{if(currentMillis-previousMillis>=intervalShort){previousMillis=currentMillis;// if the LED is off turn it on and vice-versa:
if(ledState==LOW){ledState=HIGH;}else{ledState=LOW;}// set the blue LED with the ledState of the variable and turn off the rest
digitalWrite(LEDR,ledState);digitalWrite(LEDB,HIGH);digitalWrite(LEDG,HIGH);}}// print value to the Serial Monitor
Serial.println(proximity);}}
#include<Arduino_APDS9960.h>intledState=LOW;unsignedlongpreviousMillis=0;constlongintervalLong=1000;constlongintervalMed=500;constlongintervalShort=100;voidsetup(){Serial.begin(9600);while(!Serial);if(!APDS.begin()){Serial.println("Error initializing APDS9960 sensor!");}// set the LEDs pins as outputs
pinMode(LEDR,OUTPUT);pinMode(LEDG,OUTPUT);pinMode(LEDB,OUTPUT);// turn all the LEDs off
digitalWrite(LEDR,HIGH);digitalWrite(LEDG,HIGH);digitalWrite(LEDB,HIGH);}voidloop(){unsignedlongcurrentMillis=millis();// check if a proximity reading is available
if(APDS.proximityAvailable()){// read the proximity
// - 0 => close
// - 255 => far
// - -1 => error
intproximity=APDS.readProximity();if(proximity>150){if(currentMillis-previousMillis>=intervalLong){previousMillis=currentMillis;// if the LED is off turn it on and vice-versa:
if(ledState==LOW){ledState=HIGH;}else{ledState=LOW;}// set the green LED with the ledState of the variable and turn off the rest
digitalWrite(LEDG,ledState);digitalWrite(LEDR,HIGH);digitalWrite(LEDB,HIGH);}}elseif(proximity>50&&proximity<=150){if(currentMillis-previousMillis>=intervalMed){previousMillis=currentMillis;// if the LED is off turn it on and vice-versa:
if(ledState==LOW){ledState=HIGH;}else{ledState=LOW;}// set the blue LED with the ledState of the variable and turn off the rest
digitalWrite(LEDB,ledState);digitalWrite(LEDR,HIGH);digitalWrite(LEDG,HIGH);}}else{if(currentMillis-previousMillis>=intervalShort){previousMillis=currentMillis;// if the LED is off turn it on and vice-versa:
if(ledState==LOW){ledState=HIGH;}else{ledState=LOW;}// set the blue LED with the ledState of the variable and turn off the rest
digitalWrite(LEDR,ledState);digitalWrite(LEDB,HIGH);digitalWrite(LEDG,HIGH);}}// print value to the Serial Monitor
Serial.println(proximity);}}