Skip to content
Snippets Groups Projects
i2cSlave_with_LEDs.ino 6.29 KiB
Newer Older
//Materials used to produce this sketch:
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Receives data as an I2C/TWI slave device
// Created 29 March 2006 with publci domain
//Fast LED
//https://github.com/FastLED/FastLED
//based on the ColorPalette and the Fire2012WithPalette examples.

//ALl these resources were used by Patricio Ordonez for the development of Hexcillator 2018


#include<Wire.h>

//library for LEDs
#include "FastLED.h"

// number of LEDs in our system
Patricio S Ordoñez P's avatar
Patricio S Ordoñez P committed
#define NUM_LEDS 42
#define DATA_PIN 3 // defines which digital pin to connect the strip data lead
#define BRIGHTNESS  200
// Define the array of leds
CRGB leds[NUM_LEDS];
CRGBPalette16 currentPalette;
TBlendType    currentBlending;
#define UPDATES_PER_SECOND 100

bool gReverseDirection = false;

#define COLOR_ORDER GRB
#define CHIPSET     WS2811
CRGBPalette16 gPal;

//capactitive reading variable
byte value;


  Wire.begin(9);                // join i2c bus with address #9
  Wire.onReceive(receiveEvent); // register event
  Wire.onRequest(requestEvent); // register event
  Serial.begin(9600);           // start serial for output at baud rate 9600
  FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(  BRIGHTNESS );

  currentPalette = RainbowColors_p;
  currentBlending = LINEARBLEND;
  static uint8_t startIndex = 0;
  startIndex = startIndex + 5; /* motion speed */
  uint8_t brightness = 255;

  /*x is the variable receiving the electrode being touched from 0 to 11. If no electrode is touched
    We receive a value of 15 which will be used as the default mode for the LED animation.
  */
  if (x == 0) {
    //the following method takes colour index, the number of the LED the palette start from and the LED where it finishes.
    FillLEDsFromPaletteColors( startIndex, 35, 42);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 1) {
    FillLEDsFromPaletteColors( startIndex, 35, 42);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 2) {
    FillLEDsFromPaletteColors( startIndex, 28, 35);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 3) {
    FillLEDsFromPaletteColors( startIndex, 28, 35);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 4) {
    FillLEDsFromPaletteColors( startIndex, 21, 28);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 5) {
    FillLEDsFromPaletteColors( startIndex, 21, 28);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 6) {
    FillLEDsFromPaletteColors( startIndex, 0, 6);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 7) {
    FillLEDsFromPaletteColors( startIndex, 0, 6);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 8) {
    FillLEDsFromPaletteColors( startIndex, 7, 14);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 9) {
    FillLEDsFromPaletteColors( startIndex, 7, 14);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 10) {
    FillLEDsFromPaletteColors( startIndex, 14, 21);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 11) {
    FillLEDsFromPaletteColors( startIndex, 14, 21);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }


  //This is the default mode of the LEDs, that is when no Electrode has been touched

  if (x == 15) {
    //fire
    random16_add_entropy( random());
    Fire2012WithPalette(); // run simulation frame, using palette colors

    FastLED.show(); // display this frame
    FastLED.delay(1000 / 60);


    static uint8_t hue = 0;
    hue++;
    CRGB darkcolor  = CHSV(hue, 255, 192); // pure hue, three-quarters brightness
    CRGB lightcolor = CHSV(hue, 128, 255); // half 'whitened', full brightness
    gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);

  Serial.println(x);


}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    // Serial.print(c);         // print the character
  }
  x = Wire.read();    // receive byte as an integer
  //Serial.println(x);         // print the integer
}
void requestEvent() {
  Wire.write(value); // respond with message of 6 bytes
  // as expected by master
//additional arguments ledStart and ledEnd added to target the required leds when an electrode is touched
void FillLEDsFromPaletteColors( uint8_t colorIndex, int ledStart, int ledEnd)
{
  uint8_t brightness = 255;

  for ( int i = ledStart; i < ledEnd; i++) {
    leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
    colorIndex += 3;
  }
}


//Fire is the effect used for the default mode when x = 15


#define COOLING  55

// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire.  Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 120


void Fire2012WithPalette()
  // Array of temperature readings at each simulation cell
  static byte heat[NUM_LEDS];

  // Step 1.  Cool down every cell a little
  for ( int i = 0; i < NUM_LEDS; i++) {
    heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  }

  // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  for ( int k = NUM_LEDS - 1; k >= 2; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  }

  // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  if ( random8() < SPARKING ) {
    int y = random8(7);
    heat[y] = qadd8( heat[y], random8(160, 255) );
  }

  // Step 4.  Map from heat cells to LED colors
  for ( int j = 0; j < NUM_LEDS; j++) {
    // Scale the heat value from 0-255 down to 0-240
    // for best results with color palettes.
    byte colorindex = scale8( heat[j], 240);
    CRGB color = ColorFromPalette( gPal, colorindex);
    int pixelnumber;
    if ( gReverseDirection ) {
      pixelnumber = (NUM_LEDS - 1) - j;
    } else {
      pixelnumber = j;
    leds[pixelnumber] = color;
  }