Skip to content

Instantly share code, notes, and snippets.

@mcordingley
Created October 26, 2019 03:46
Show Gist options
  • Save mcordingley/4de9cb94b888df4ea0731c8c4347b724 to your computer and use it in GitHub Desktop.
Save mcordingley/4de9cb94b888df4ea0731c8c4347b724 to your computer and use it in GitHub Desktop.
Code for my Flora-enabled backpack that has a NeoPixel ring, an SI1145 sensor, and a LSM303 sensor.
#include <Adafruit_NeoPixel.h>
#include <Adafruit_SI1145.h>
Adafruit_NeoPixel pixelCircle = Adafruit_NeoPixel(12, 9, NEO_GRBW + NEO_KHZ800);
Adafruit_SI1145 lightSensor = Adafruit_SI1145();
void setup() {
pixelCircle.begin();
pixelCircle.setBrightness(50);
pixelCircle.show();
lightSensor.begin();
}
void loop() {
float index = lightSensor.readUV() / 100.0;
if (index >= 1.0) {
displayUVIndexOnStrip(index, &pixelCircle);
delay(100);
} else {
showRainbowOnStrip(&pixelCircle, millis());
delay(50);
}
}
void displayUVIndexOnStrip(float index, Adafruit_NeoPixel* strip) {
unsigned int pixelCount = strip->numPixels();
unsigned int pixelsAtFullBrightness = min(static_cast<unsigned int>(index), pixelCount);
unsigned int i = 0;
float indexRemainder = index - pixelsAtFullBrightness;
strip->clear();
for (; i < pixelsAtFullBrightness; i++) {
strip->setPixelColor(i, 0xff0088);
}
if (i < pixelCount) {
strip->setPixelColor(
i,
Adafruit_NeoPixel::Color(
static_cast<uint8_t>(255.0 * indexRemainder),
0,
static_cast<uint8_t>(128.0 * indexRemainder)
)
);
}
strip->show();
}
void showRainbowOnStrip(Adafruit_NeoPixel* strip, unsigned long time) {
unsigned int pixelCount = strip->numPixels();
unsigned long hueStepSize = 65536L / pixelCount;
unsigned long timeBasedHueOffset = static_cast<unsigned long>((65536L / 5.0) * (time / 1000.0));
unsigned long hue;
for (unsigned int i = 0; i < pixelCount; i++) {
hue = (timeBasedHueOffset + i * hueStepSize) % 65536L;
strip->setPixelColor(i, strip->gamma32(strip->ColorHSV(hue)));
}
strip->show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment