Created
January 23, 2020 00:35
-
-
Save natendaben/e2595c4e3cde23b61aca7abb7aef11a9 to your computer and use it in GitHub Desktop.
Smooth color transitions with Neopixels and RFID tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <MFRC522.h> | |
#define SS_PIN 53 //SDA Pin | |
#define RST_PIN 5 | |
MFRC522 mfrc522(SS_PIN, RST_PIN); // Instance of the mfrc522 class | |
#include <FastLED.h> //FastLED library | |
#define FIRST_STRIP_NUM_LEDS 5 //ONLY FOR FIRST LED STRIP | |
#define SECOND_STRIP_NUM_LEDS 10 //ONLY FOR FIRST LED STRIP | |
#define FIRST_DATA_PIN 22 | |
#define SECOND_DATA_PIN 24 | |
CRGB first_leds[FIRST_STRIP_NUM_LEDS]; //array for storing led colors | |
CRGB second_leds[SECOND_STRIP_NUM_LEDS]; | |
// ~~~~~~~~~~~~~~~~~~~~~~ DEFINE COLORS ~~~~~~~~~~~~~~~~~~~~~~ | |
// Desert - Gold / brown | |
CRGB desert = CHSV(40,255,255); | |
// Underwater - Deep blues / dark | |
CRGB water = CHSV(170,255,255); | |
// Rainforest - Deep greens / dark | |
CRGB forest = CHSV(105,255,255); | |
// Grassland - Light greens / yellows | |
CRGB grass = CHSV(73,255,255); | |
// Beach - Light blues / tans | |
CRGB beach = CHSV(130,255,255); | |
CRGB targetColor; | |
int switchPin; | |
void setup() { | |
//Initialize | |
Serial.begin(9600); | |
SPI.begin(); // Init SPI bus | |
mfrc522.PCD_Init(); // Init MFRC522 | |
FastLED.setBrightness(50); | |
FastLED.addLeds<NEOPIXEL, FIRST_DATA_PIN>(first_leds, FIRST_STRIP_NUM_LEDS).setCorrection(TypicalLEDStrip); | |
FastLED.addLeds<NEOPIXEL, SECOND_DATA_PIN>(second_leds, SECOND_STRIP_NUM_LEDS).setCorrection(TypicalLEDStrip); | |
Serial.println("RFID reading UID"); | |
delay(3000); | |
pinMode(A14, INPUT); | |
} | |
void loop() { | |
Serial.println(analogRead(A14)); | |
switchPin = analogRead(A14); | |
if (switchPin < 900){ | |
fadeTowardColor( first_leds, SECOND_STRIP_NUM_LEDS, CRGB::Red, 5); | |
fadeTowardColor( second_leds, SECOND_STRIP_NUM_LEDS, CRGB::Black, 5); | |
FastLED.show(); | |
FastLED.delay(10); | |
} else { | |
fadeTowardColor( first_leds, SECOND_STRIP_NUM_LEDS, CRGB::Green, 5); | |
FastLED.show(); | |
if ( mfrc522.PICC_IsNewCardPresent()){ | |
if ( mfrc522.PICC_ReadCardSerial()){ | |
if (mfrc522.uid.uidByte[0] == 0x19 && mfrc522.uid.uidByte[1] == 0xE8 && mfrc522.uid.uidByte[2] == 0xB3 && mfrc522.uid.uidByte[3] == 0xB0) { | |
targetColor = desert; | |
} | |
if (mfrc522.uid.uidByte[0] == 0xC9 && mfrc522.uid.uidByte[1] == 0xFA && mfrc522.uid.uidByte[2] == 0x21 && mfrc522.uid.uidByte[3] == 0xB3) { | |
targetColor = water; | |
} | |
if (mfrc522.uid.uidByte[0] == 0xF9 && mfrc522.uid.uidByte[1] == 0xEC && mfrc522.uid.uidByte[2] == 0xC4 && mfrc522.uid.uidByte[3] == 0xB1) { | |
targetColor = forest; | |
} | |
if (mfrc522.uid.uidByte[0] == 0xC9 && mfrc522.uid.uidByte[1] == 0xB1 && mfrc522.uid.uidByte[2] == 0xB6 && mfrc522.uid.uidByte[3] == 0xB0) { | |
targetColor = grass; | |
} | |
if (mfrc522.uid.uidByte[0] == 0x29 && mfrc522.uid.uidByte[1] == 0x8E && mfrc522.uid.uidByte[2] == 0xAD && mfrc522.uid.uidByte[3] == 0xB0) { | |
targetColor = beach; | |
} | |
Serial.print("Tag UID:"); | |
for (byte i = 0; i < mfrc522.uid.size; i++) { | |
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); | |
Serial.print(mfrc522.uid.uidByte[i], HEX); | |
} | |
Serial.println(); | |
mfrc522.PICC_HaltA(); | |
delay(1000); | |
} | |
} else { | |
} | |
// fade all existing pixels toward bgColor by "5" (out of 255) | |
fadeTowardColor( second_leds, SECOND_STRIP_NUM_LEDS, targetColor, 5); | |
FastLED.show(); | |
FastLED.delay(10); | |
} | |
} | |
// Helper function that blends one uint8_t toward another by a given amount | |
void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount) | |
{ | |
if( cur == target) return; | |
if( cur < target ) { | |
uint8_t delta = target - cur; | |
delta = scale8_video( delta, amount); | |
cur += delta; | |
} else { | |
uint8_t delta = cur - target; | |
delta = scale8_video( delta, amount); | |
cur -= delta; | |
} | |
} | |
// Blend one CRGB color toward another CRGB color by a given amount. | |
// Blending is linear, and done in the RGB color space. | |
// This function modifies 'cur' in place. | |
CRGB fadeTowardColor( CRGB& cur, const CRGB& target, uint8_t amount) | |
{ | |
nblendU8TowardU8( cur.red, target.red, amount); | |
nblendU8TowardU8( cur.green, target.green, amount); | |
nblendU8TowardU8( cur.blue, target.blue, amount); | |
return cur; | |
} | |
// Fade an entire array of CRGBs toward a given background color by a given amount | |
// This function modifies the pixel array in place. | |
void fadeTowardColor( CRGB* L, uint16_t N, const CRGB& bgColor, uint8_t fadeAmount) | |
{ | |
for( uint16_t i = 0; i < N; i++) { | |
fadeTowardColor( L[i], bgColor, fadeAmount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment