#include <TinyWireS.h>
#include <Adafruit_NeoPixel.h>
// I2C address of the slave device
#define SLAVE_ADDRESS 0x04 // Change depending on PCB
// Neopixel pin and configuration
const int neopixelPin = PB4; // Change to the actual Neopixel pin
const int numPixels = 5; // Number of LEDs in the Neopixel strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixels, neopixelPin, NEO_GRB + NEO_KHZ800);
volatile int command = 0;
void setup() {
TinyWireS.begin(SLAVE_ADDRESS); // Initialize I2C communication as a slave with specified address
TinyWireS.onReceive(receiveEvent); // Register the receiveEvent function to handle incoming I2C data
strip.begin(); // Initialize the Neopixel strip
strip.show(); // Show the initial state of the Neopixel strip
}
void loop() {
TinyWireS_stop_check(); // Check for I2C bus activity
// Check the value of the command received from the master
if (command == 1) {
// If command is 1, set the Neopixel color to red
setNeopixelColor(strip.Color(255, 0, 0)); // Red color (RGB format)
} else {
// If command is not 1 (e.g., 0), turn off all Neopixels
strip.clear(); // Clear the Neopixel strip
strip.show(); // Update the Neopixel strip to turn off all LEDs
}
delay(10); // Small delay for stability
}
// Function to handle I2C data reception
void receiveEvent(uint8_t howMany) {
if (howMany >= 1) {
// Read the received data from the I2C bus and store it in the command variable
command = TinyWireS.receive();
}
}
// Function to set the color of the Neopixels
void setNeopixelColor(uint32_t color) {
for (int i = 0; i < numPixels; i++) {
// Set each Neopixel in the strip to the specified color
strip.setPixelColor(i, color);
}
strip.show(); // Update the Neopixel strip to reflect the color changes
}