-
-
Save bonbombs/ec820dfd15519ba2675ddf0d45f7666f to your computer and use it in GitHub Desktop.
Lights up NeoPixel LED strip when it gets data from the Raspberry Pi via I2C
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 <Wire.h> | |
#include <Adafruit_NeoPixel.h> | |
#include <math.h> | |
#define PIN_A 6 //Strip #1 | |
#define PIN_B 9 //Strip #2 | |
#define PIN_LED 8 //Onboard LED | |
#define SLAVE_ADDRESS 0x04 //I2C address | |
Adafruit_NeoPixel onboard = Adafruit_NeoPixel(1, PIN_LED, NEO_GRB + NEO_KHZ800); | |
Adafruit_NeoPixel strip_one = Adafruit_NeoPixel(10, PIN_A, NEO_GRB + NEO_KHZ800); | |
Adafruit_NeoPixel strip_two = Adafruit_NeoPixel(5, PIN_B, NEO_GRB + NEO_KHZ800); | |
Adafruit_NeoPixel strips[2] = { | |
strip_one, | |
strip_two | |
}; | |
int number = 0; | |
int state = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
onboard.begin(); | |
onboard.show(); | |
strip_one.begin(); | |
strip_one.show(); | |
//Begin transmission of data between Pi and FLORA | |
Wire.begin(SLAVE_ADDRESS); | |
Wire.onReceive(receiveData); | |
Wire.onRequest(sendData); | |
} | |
void loop() {} | |
void receiveData(int byteCount) { | |
while(Wire.available()) { | |
strip_one.show(); | |
//Get the number of wireless devices in the area | |
//Divide by 2 since we only have half of our LEDs working | |
number = floor(Wire.read() / 2); | |
//Print for debug | |
Serial.print("LEDS to light: "); | |
Serial.println(number); | |
int even = 0; | |
int odd = 9; | |
//TODO: Make it so that LEDs start displaying from bottom to top per strip | |
for(int i = 0; i < number; i++) { | |
int depth; | |
if(i % 2 != 0) { | |
depth = odd--; | |
} | |
else { | |
depth = even++; | |
} | |
Serial.print("LED #: "); | |
Serial.println(depth); | |
//Display white for now | |
strip_one.setPixelColor(depth, strip_one.Color(255, 255, 255)); | |
strip_one.show(); | |
delay(50); | |
} | |
//Clear pixels that did not get lit up this iteration | |
for(int j = even; j < 5; j++) { | |
strip_one.setPixelColor(j, strip_one.Color(0, 0, 0, 0)); | |
strip_one.show(); | |
} | |
for(int j = odd; j >= 5; j--) { | |
strip_one.setPixelColor(j, strip_one.Color(0, 0, 0, 0)); | |
strip_one.show(); | |
} | |
} | |
} | |
void sendData() { | |
Wire.write(number); | |
} | |
// Fill the dots one after the other with a color | |
void colorWipe(uint32_t c, uint8_t wait) { | |
for(uint16_t i=0; i<onboard.numPixels(); i++) { | |
onboard.setPixelColor(i, c); | |
onboard.show(); | |
delay(wait); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment