Distance sensor
Last updated
Last updated
#include <Arduino.h>
#include <FastLED.h>
#include <Wire.h>
#include "SparkFun_VCNL4040_Arduino_Library.h"
#include "config.h"
VCNL4040 vcnl;
void setup(){
pinMode(BUTTON_PIN, INPUT);
pinMode(ADC_ENABLE_PIN, OUTPUT);
pinMode(ADC_PIN, INPUT);
analogReadResolution(12);
digitalWrite(ADC_ENABLE_PIN, HIGH);
WiFi.mode(WIFI_OFF);
FastLED.addLeds<APA102, APA102_SDI_PIN, APA102_CLK_PIN, BGR>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
delay(50);
set_fastled(CRGB::Blue);
Wire.begin(SDA_PIN, SCL_PIN);
if(vcnl.begin() == false){
printf("Device not found. Please check wiring.\r\n");
set_fastled(CRGB::Red);
while(1); //Freeze!
}
}
unsigned long led_timer = millis();
int hue1 = 0, hue2 = 0;
int brightness = 255;
unsigned long t_sensor = 500;
void loop() {
if(digitalRead(BUTTON_PIN) == 1){
set_fastled(CRGB::Red);
delay(1000);
esp_deep_sleep_start();
}
if(millis() >= t_sensor + 100){
t_sensor = millis();
unsigned int proxValue = vcnl.getProximity();
printf("Proximity: %i\r\n", proxValue);
}
if(millis() >= led_timer + 15){
led_timer = millis();
set_fastled(CHSV(hue1, 255, brightness), CHSV(hue2, 255, brightness));
hue1 = (hue1 + 1)%255;
hue2 = (hue1 + 127)%255;
}
}