Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active December 10, 2024 20:04
Show Gist options
  • Save bboyho/f541c95605bbc69cfb8b31f8c63d9a87 to your computer and use it in GitHub Desktop.
Save bboyho/f541c95605bbc69cfb8b31f8c63d9a87 to your computer and use it in GitHub Desktop.
/*
An I2C based LED Stick
Modified by Ho Yun "Bobby" Chan
By: Ciara Jekel
SparkFun Electronics
Date: June 11th, 2018
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/products/14783
This example changes the color of the 10 LEDs baseed on the input from the Serial Monitor.
By uncommenting out CycleRainbow(100); it also makes the LED Stick smoothly change through
the colors of the rainbow.
*/
#include <Wire.h>
#include "Qwiic_LED_Stick.h" // Click here to get the library: http://librarymanager/All#SparkFun_Qwiic_LED_Stick
LED LEDStick; //Create an object of the LED class
int input_color = 0;
void setup() {
Wire.begin();
//Wire.setClock(400000); // Set clock speed to be the fastest for better communication (fast mode)
Serial.begin(115200);
//Start up communication with the LED Stick
if (LEDStick.begin() == false) {
Serial.println("Qwiic LED Stick failed to begin. Please check wiring and try again!");
while (1)
;
}
LEDStick.setLEDColor(0, 0, 0);
LEDStick.setLEDBrightness(1);
Serial.println("Qwiic LED Stick ready!");
}
void loop() {
//CycleRainbow(100);
if (Serial.available()) { // If data comes in from serial monitor, send it out to XBee
input_color = Serial.read(); //read user input
Serial.println(input_color);
// do something different depending on the input value:
switch (input_color) {
case 49: // if ASCII CHARACTER `1` IS SENT
LEDStick.setLEDColor(255, 0, 0); //RED
break;
case 50: // if ASCII CHARACTER `2` IS SENT
LEDStick.setLEDColor(255, 255, 0); //YELLOW
break;
case 51: // if ASCII CHARACTER `3` IS SENT
LEDStick.setLEDColor(0, 255, 0); //GREEN
break;
case 52: // if ASCII CHARACTER `4` IS SENT
LEDStick.setLEDColor(0, 255, 255); //CYAN
break;
case 53: // if ASCII CHARACTER `5` IS SENT
LEDStick.setLEDColor(255, 0, 255); //BLUE
break;
case 54: // if ASCII CHARACTER `6` IS SENT
LEDStick.setLEDColor(255, 0, 255); //MAGENTA
break;
case 55: // if ASCII CHARACTER `7` IS SENT
LEDStick.setLEDColor(255, 255, 255); //WHITE
break;
default:
LEDStick.setLEDColor(0, 0, 0);
break;
}
delay(100);
}
}
//Cycle through the rainbow with all LEDs the same color
void CycleRainbow(int delayTime) {
//will repeat for each color from red to yellow
for (byte g = 0; g < 255; g++) {
//Set all LEDs to max red with green increasing each repetition
LEDStick.setLEDColor(255, g, 0);
delay(delayTime);
}
//will repeat for each color from yellow to green
for (byte r = 255; r > 0; r--) {
//Set all LEDs to max green with red decreasing each repetition
LEDStick.setLEDColor(r, 255, 0);
delay(delayTime);
}
//will repeat for each color from green to cyan
for (byte b = 0; b < 255; b++) {
//Set all LEDs to max green with blue increasing each repetition
LEDStick.setLEDColor(0, 255, b);
delay(delayTime);
}
//will repeat for each color from cyan to blue
for (byte g = 255; g > 0; g--) {
//Set all LEDs to max blue with green decreasing each repetition
LEDStick.setLEDColor(0, g, 255);
delay(delayTime);
}
//will repeat for each color from blue to magenta
for (byte r = 0; r < 255; r++) {
//Set all LEDs to max blue with red increasing each repetition
LEDStick.setLEDColor(r, 0, 255);
delay(delayTime);
}
//will repeat for each color from magenta to red
for (byte b = 255; b > 0; b--) {
//Set all LEDs to max red with blue decreasing each repetition
LEDStick.setLEDColor(255, 0, b);
delay(delayTime);
}
LEDStick.LEDOff();
//LEDStick.setLEDColor(0, 0, 0);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment