You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
project-lamp/project-lamp.ino

218 lines
5.4 KiB

3 years ago
#include "FastLED.h"
#include "FastLED_RGBW.h"
#include <IRremote.h>
#define NUM_LEDS 1
#define DATA_PIN 3
3 years ago
#define IR_RECEIVE_PIN 2
#undef ENABLE_LED_FEEDBACK
#define ENABLE_LED_FEEDBACK 0
3 years ago
CRGBW COL_RED (255, 0, 0, 0);
CRGBW COL_GREEN ( 0,255, 0, 0);
CRGBW COL_BLUE ( 0, 0,255, 0);
CRGBW COL_WHITE ( 0, 0, 0,255);
CRGBW COL_SUM (255,255,255,255);
CRGBW COL_AMBER (255,191, 0, 0);
CRGBW COL_LIME (127,255, 0, 0);
CRGBW COL_AZURE ( 0,127,255, 0);
CRGBW COL_ORANGE (255,127, 0, 0);
CRGBW COL_CYAN ( 0,255,255, 0);
CRGBW COL_UV ( 63, 0,255, 0);
CRGBW COL_PEACHY (255, 63, 0, 0);
CRGBW COL_SPRING ( 0,255,127, 0);
CRGBW COL_MAGENTA(255, 0,255, 0);
CRGBW COL_YELLOW (255,255, 0, 0);
CRGBW COL_VIOLET (127, 0,255, 0);
CRGBW COL_ROSE (255, 0,127, 0);
CRGBW desaturate(CRGBW col)
{
return CRGBW(col.red, col.green, col.blue, col.white + 64);
}
struct ColourCode
{
uint32_t code;
uint32_t altCode;
3 years ago
CRGBW colour;
CRGBW altColour;
ColourCode(uint32_t c, uint32_t c2,CRGBW col)
3 years ago
{
code = c;
altCode = c2;
3 years ago
colour = col;
altColour = desaturate(col);
}
ColourCode(uint32_t c, uint32_t c2, CRGBW colA, CRGBW colB)
3 years ago
{
code = c;
altCode = c2;
3 years ago
colour = colA;
altColour = colB;
}
};
const ColourCode colourCodes[] =
{
{0xE619FF00, 0xFB04EF00, COL_RED},
{0xE41BFF00, 0xFA05EF00, COL_GREEN},
{0xEE11FF00, 0xF906EF00, COL_BLUE},
{0xEA15FF00, 0xF807EF00, COL_SUM, COL_WHITE},
{0xE817FF00, 0xF708EF00, COL_AMBER},
{0xED12FF00, 0xF609EF00, COL_LIME},
{0xE916FF00, 0xF50AEF00, COL_AZURE},
{0xBF40FF00, 0xF30CEF00, COL_ORANGE},
{0xB34CFF00, 0xF20DEF00, COL_CYAN},
{0xFB04FF00, 0xF10EEF00, COL_UV},
{0xF50AFF00, 0xEF10EF00, COL_PEACHY},
{0xE11EFF00, 0xEE11EF00, COL_SPRING},
{0xF10EFF00, 0xED12EF00, COL_MAGENTA},
{0xE31CFF00, 0xEB14EF00, COL_YELLOW},
{0xEB14FF00, 0xEA15EF00, COL_VIOLET},
{0xF00FFF00, 0xE916EF00, COL_ROSE}
3 years ago
};
const uint32_t CODE_BRIGHTER = 0xFF00EF00;
const uint32_t CODE_DIMMER = 0xFE01EF00;
const uint32_t CODE_OFF = 0xFD02EF00;
const uint32_t CODE_ON = 0xFC03EF00;
const uint32_t CODE_FLASH = 0xF40BEF00;
const uint32_t CODE_STROBE = 0xF00FEF00;
const uint32_t CODE_FADE = 0xEC13EF00;
const uint32_t CODE_SMOOTH = 0xE817EF00;
3 years ago
const int NUM_COLOURCODES = sizeof(colourCodes) / sizeof(ColourCode);
CRGBW leds[NUM_LEDS];
CRGB *ledsRGB = (CRGB *) &leds[0];
//IRrecv irrecv(IR_RECEIVE_PIN);/
3 years ago
uint8_t brightness = 128;
uint32_t lastCode = 0;
decode_results results;
void colorFill(CRGBW c){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = c;
FastLED.show();
delay(50);
}
}
void colorFill(CRGB c){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = c;
FastLED.show();
delay(50);
}
}
3 years ago
void setup() {
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(ledsRGB, getRGBWsize(NUM_LEDS));
colorFill(COL_SUM);
FastLED.setBrightness(255);
3 years ago
FastLED.show();
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
#if defined(__AVR_ATmega32U4__)
while (!Serial)
; //delay for Leonardo, but this loops forever for Maple Serial
#endif
#if defined(SERIAL_USB) || defined(SERIAL_PORT_USBVIRTUAL)
delay(2000); // To be able to connect Serial monitor after reset and before first printout
#endif
// Just to know which program is running on my Arduino
Serial.println(F("START " __FILE__ " from " __DATE__));
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
Serial.println("Enabling IRin");
// irrecv.enableIRIn(); // Start the receiver/
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
3 years ago
Serial.print(F("Ready to receive IR signals at pin "));
Serial.println(IR_RECEIVE_PIN);
}
3 years ago
void interpretColourCode(uint32_t code)
{
for (int i = 0; i < NUM_COLOURCODES; i++)
{
ColourCode col = colourCodes[i];
if (col.code == code || col.altCode == code)
3 years ago
{
if (code == lastCode)
{
Serial.println(F("Colour code OK - Secondary"));
3 years ago
colorFill(col.altColour);
lastCode = 0;
}
else
{
Serial.println(F("Colour code OK - Primary"));
3 years ago
colorFill(col.colour);
lastCode = code;
}
return;
}
else
{
// Serial.print(code, HEX);
// Serial.print(F(" != "));
// Serial.println(col.code, HEX);
3 years ago
}
}
Serial.println(F("CODE?!?!"));
3 years ago
}
void loop()
{
if (IrReceiver.decode())
3 years ago
{
uint32_t data = IrReceiver.decodedIRData.decodedRawData;
Serial.println(data, HEX);
IrReceiver.resume(); // Receive the next value
switch(data)
3 years ago
{
case CODE_BRIGHTER:
brightness += 32;
FastLED.setBrightness(brightness);
FastLED.show();
Serial.println(F("Brighter"));
3 years ago
break;
case CODE_DIMMER:
brightness -= 32;
FastLED.setBrightness(brightness);
FastLED.show();
Serial.println(F("Dimmer"));
3 years ago
break;
case CODE_OFF:
FastLED.setBrightness(0);
FastLED.show();
Serial.println(F("Off"));
3 years ago
break;
case CODE_ON:
FastLED.setBrightness(brightness);
FastLED.show();
Serial.println(F("On"));
3 years ago
break;
default:
interpretColourCode(data);
Serial.println(F("Set colour"));
3 years ago
}
if (brightness <= 0)
brightness = 255;
3 years ago
}
delay(100);
}