Skip to content

Instantly share code, notes, and snippets.

@atomjar
Last active October 5, 2021 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomjar/249e73f9a6717128d18dd1dfd77124cc to your computer and use it in GitHub Desktop.
Save atomjar/249e73f9a6717128d18dd1dfd77124cc to your computer and use it in GitHub Desktop.
#include <FastLED.h>
#include <Wire.h>
#include <SparkFun_APDS9960.h>
// LEDs
#define NUM_LEDS 248
#define DATA_PIN8 8
#define DATA_PIN9 9
CRGB leds[NUM_LEDS];
// Sensor
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint8_t proximity_data = 0;
void setup() {
// Reduce clock freq
Wire.setClock(10000);
// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("------------------------------------"));
Serial.println(F("APDS-9960 - ProximitySensor"));
Serial.println(F("------------------------------------"));
// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
// Adjust the Proximity sensor gain
if ( !apds.setProximityGain(PGAIN_2X) ) {
Serial.println(F("Something went wrong trying to set PGAIN"));
}
// Start running the APDS-9960 proximity sensor (no interrupts)
if ( apds.enableProximitySensor(false) ) {
Serial.println(F("Proximity sensor is now running"));
} else {
Serial.println(F("Something went wrong during sensor init!"));
}
// Set up LEDs
FastLED.addLeds<WS2812B, DATA_PIN8, GRB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, DATA_PIN9, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(100);
// Set max power draw (v, amps)
FastLED.setMaxPowerInVoltsAndMilliamps(5, 14000);
}
void loop() {
// Read the proximity value
if ( !apds.readProximity(proximity_data) ) {
Serial.println("Error reading proximity value");
} else {
Serial.print("Proximity: ");
Serial.println(proximity_data);
}
// Sensor Variables
uint8_t speed = proximity_data * .35;
uint8_t color_red = proximity_data * 5;
uint8_t color_green = proximity_data * .5;
// LED Animations
uint8_t sinBeat1 = beatsin8(speed, 0, NUM_LEDS - 1, 0, 0);
uint8_t sinBeat2 = beatsin8(speed * .5, 0, NUM_LEDS - 1, 0, -90);
uint8_t sinBeat3 = beatsin8(speed * .75, 0, NUM_LEDS - 1, 0, 180);
uint8_t sinBeat4 = beatsin8(speed * .90, 0, NUM_LEDS - 1, 0, -270);
leds[sinBeat1] = CRGB(color_red, color_green, 110);
leds[sinBeat2] = CRGB(color_red, color_green, 110);
leds[sinBeat3] = CRGB(color_red, color_green, 110);
leds[sinBeat4] = CRGB(color_red, color_green, 110);
blur1d(leds, NUM_LEDS, 70);
fadeToBlackBy(leds, NUM_LEDS, 20);
FastLED.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment