Skip to content
Snippets Groups Projects
i2cSlave_with_LEDs.ino 3.95 KiB
Newer Older
//This is a midified sketch which receives i2c messages and also implements Fast LEd library 
//to make an led animation based on messages sent from the touchboard


// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include<Wire.h>

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

// How many leds in your strip?
#define NUM_LEDS 18
#define DATA_PIN 3 // defines which digital pin to connect the strip data lead
#define BRIGHTNESS  64
// Define the array of leds
CRGB leds[NUM_LEDS];
CRGBPalette16 currentPalette;
TBlendType    currentBlending;
#define UPDATES_PER_SECOND 100




int x = 0;
void setup() {


  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output

  //Fast LED
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(  BRIGHTNESS );

  currentPalette = RainbowColors_p;
  currentBlending = LINEARBLEND;
}

void loop() {
  static uint8_t startIndex = 0;
  startIndex = startIndex + 5; /* motion speed */
  uint8_t brightness = 255;
    if (x == 0) {
FillLEDsFromPaletteColors( startIndex,6,9);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  if (x == 1) {
FillLEDsFromPaletteColors( startIndex,6,9);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
    if (x == 2) {
FillLEDsFromPaletteColors( startIndex,9,12);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
    if (x == 3) {
FillLEDsFromPaletteColors( startIndex,9,12);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
    if (x == 4) {
FillLEDsFromPaletteColors( startIndex,12,15);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
     if (x == 5) {
FillLEDsFromPaletteColors( startIndex,12,15);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
    if (x == 6) {
FillLEDsFromPaletteColors( startIndex,15,18);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
      if (x == 7) {
FillLEDsFromPaletteColors( startIndex,15,18);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
      if (x == 8) {
FillLEDsFromPaletteColors( startIndex,0,3);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
      if (x == 9) {
FillLEDsFromPaletteColors( startIndex,0,3);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
      if (x == 10) {
FillLEDsFromPaletteColors( startIndex,3,6);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
      if (x == 11) {
FillLEDsFromPaletteColors( startIndex,3,6);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
      if (x == 12) {
FillLEDsFromPaletteColors( startIndex,9,12);
    FastLED.show();
    FastLED.delay(100 / UPDATES_PER_SECOND);
  }
  
  
  
  if (x == 15){
    for(int i = 0; i < NUM_LEDS; i++){
        // Turn the LED on, then pause
  leds[i] = CRGB::Red;
  FastLED.show();
  delay(10);
  // Now turn the LED off, then pause
  leds[i] = CRGB::Black;
  FastLED.show();
  delay(10);
      }
    }
}
// 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 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;
    }
}