153 lines
4.7 KiB
C++
153 lines
4.7 KiB
C++
#include <Arduino.h>
|
|
#include <FastLED.h>
|
|
#include <SoftwareSerial.h>
|
|
#include <SerialTransfer.h>
|
|
|
|
#define RELAY_PIN 4
|
|
#define LED_PIN 9 // the pin which attaches to the neopixel data pin
|
|
#define COLOR_ORDER RGB // sets the color order in which the LEDs are designed for
|
|
#define CHIPSET WS2812 // the chipset that the neopixels use
|
|
#define NUM_LEDS 82 // how many leds are being adderessed
|
|
|
|
#define BRIGHTNESS 200 // sets the overall brightness of the leds
|
|
#define FRAMES_PER_SECOND 60 // how fast should the led animation render
|
|
#define COOLING 55 // defines the level at which the lighting effect fades before a new "flame" generates
|
|
#define SPARKING 120 // defines the rate of flicker which we will see from the flame animation
|
|
|
|
bool gReverseDirection = true; // set this to true if you need to invert the animation direction
|
|
|
|
CRGB leds[NUM_LEDS]; // sets up an array that we can manipulate to set/clear led data.
|
|
uint8_t ledsB[NUM_LEDS];
|
|
|
|
CRGBPalette16 currentPalette; // sets a variable for CRGBPalette16 which allows us to change this value later
|
|
CRGBPalette16 gPal;
|
|
|
|
struct I2cRxStruct {
|
|
//byte mode;//, hue, saturation, bright;
|
|
byte mode, r, g, b;
|
|
//uint32_t RGB;
|
|
};
|
|
I2cRxStruct rxData;
|
|
|
|
SerialTransfer myTransfer;
|
|
|
|
bool newRxData = false;
|
|
|
|
// I2C control stuff
|
|
#include <Wire.h>
|
|
|
|
const byte thisAddress = 9; // these need to be swapped for the other Arduino
|
|
const byte otherAddress = 8;
|
|
|
|
//=================================
|
|
void showNewData();
|
|
void receiveEvent(int numBytesReceived);
|
|
|
|
const byte rxPin = 3;
|
|
const byte txPin = 2;
|
|
SoftwareSerial lampSerial(rxPin, txPin);
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println("\nStarting I2C Slave demo\n");
|
|
pinMode(RELAY_PIN, OUTPUT);
|
|
digitalWrite(RELAY_PIN, HIGH);
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
//pinMode(LED_PIN, OUTPUT);
|
|
|
|
lampSerial.begin(19200);
|
|
myTransfer.begin(lampSerial);
|
|
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);//.setCorrection( TypicalLEDStrip );
|
|
//FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
|
|
// FastLED.setBrightness( BRIGHTNESS ); // sets the brightness to the predetermined levels
|
|
// for(int i = 0; i < NUM_LEDS; i++){
|
|
// leds[i] = CRGB(16, 16, 16);
|
|
// ledsB[i] = 100;
|
|
// }
|
|
// FastLED.show();
|
|
}
|
|
|
|
void loop() {
|
|
static uint32_t cRun = millis();
|
|
static bool powerOn = false;
|
|
uint8_t rn;
|
|
// if(cRun + 1000 < millis()){
|
|
// digitalWrite(LED_PIN, !digitalRead(LED_PIN));
|
|
// cRun = millis();
|
|
|
|
// }
|
|
// return;
|
|
if(myTransfer.available()){
|
|
uint16_t recSize = 0;
|
|
recSize = myTransfer.rxObj(rxData, recSize);
|
|
Serial.print(rxData.mode);
|
|
Serial.print("-");
|
|
Serial.print(rxData.r, 16);
|
|
Serial.print("-");
|
|
Serial.print(rxData.g, 16);
|
|
Serial.print("-");
|
|
Serial.println(rxData.b, 16);
|
|
newRxData = true;
|
|
}
|
|
|
|
random16_add_entropy(random());
|
|
//randomSeed(analogRead(A1));
|
|
// // this bit checks if a message has been received
|
|
if (newRxData == true) {
|
|
if(rxData.mode > 0){
|
|
if(digitalRead(RELAY_PIN)){
|
|
digitalWrite(RELAY_PIN, LOW);
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
Serial.println("Power ON");
|
|
cRun = millis();
|
|
}
|
|
}
|
|
else{
|
|
if(!digitalRead(RELAY_PIN)){
|
|
digitalWrite(RELAY_PIN, HIGH);
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
Serial.println("Power OFF");
|
|
powerOn = false;
|
|
}
|
|
}
|
|
if(cRun + 500 < millis()){
|
|
if (rxData.mode != 2) showNewData();
|
|
Serial.println("Show LED");
|
|
powerOn = true;
|
|
newRxData = false;
|
|
//cRun = millis();
|
|
}
|
|
}
|
|
if((rxData.mode == 2) && powerOn){
|
|
if(cRun + 100 < (millis())){
|
|
cRun = millis();
|
|
for(int i = 0; i < NUM_LEDS; i++){
|
|
rn = random8(100);
|
|
ledsB[i] += int((rn - ledsB[i]) / 3.0);
|
|
leds[i] = CRGB(int(2.55 * ledsB[i]), int(0.5 * ledsB[i]), 0);
|
|
//CHSV(HUE_ORANGE, 100, uint8_t(ledsB[i]));//
|
|
}
|
|
FastLED.show(); // display this frame
|
|
}
|
|
}
|
|
// if(cRun + 1000 < millis()){
|
|
// digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN));Serial.println(digitalRead(RELAY_PIN));
|
|
// cRun = millis();
|
|
// }
|
|
}
|
|
|
|
void showNewData() {
|
|
for(int i = 0; i < NUM_LEDS; i++){
|
|
//leds[i].setRGB(rxData.RGB >> 16, rxData.RGB << 16 >> 24, rxData.RGB << 24 >> 24);
|
|
// Serial.print("Led "); Serial.print(i);
|
|
// Serial.print("\tR:"); Serial.print(rxData.r, 16);
|
|
// Serial.print(" G:"); Serial.print(rxData.g, 16);
|
|
// Serial.print(" B:"); Serial.println(rxData.b, 16);
|
|
leds[i].setRGB(rxData.r, rxData.g, rxData.b);
|
|
//leds[i].setHSV(rxData.hue, rxData.saturation, rxData.bright);
|
|
}
|
|
FastLED.setBrightness(255);
|
|
FastLED.show(); // display this frame
|
|
}
|
|
|