11/*
2- 'CheerLightsExample' demonstrates how to use the CheerLights library to fetch and display the current CheerLights color on an Adafruit NeoPixel LED strip.
2+ 'CheerLightsExample' demonstrates how to use the CheerLights Arduino library to fetch and display the current CheerLights color on an Adafruit NeoPixel LED strip.
33
44To learn more about CheerLights, visit https://cheerlights.com.
55*/
66
7- #include < CheerLights.h>
8- #include " secrets.h"
9- #include < Adafruit_NeoPixel.h> // For controlling NeoPixels
7+ // Include the Adafruit NeoPixel library and initialize the strip
8+ #include < Adafruit_NeoPixel.h>
109
1110#define LED_PIN 6
1211#define NUM_LEDS 8
1312
14- CheerLights CheerLights;
1513Adafruit_NeoPixel strip (NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
1614
15+ // Include the correct WiFi library based on the board
16+ #if defined(ESP8266)
17+ #include < ESP8266WiFi.h>
18+ #elif defined(ESP32)
19+ #include < WiFi.h>
20+ #elif defined(ARDUINO_SAMD_MKR1000)
21+ // For Arduino MKR1000 using WiFi101 library
22+ #include < WiFi101.h>
23+ #elif defined(ARDUINO_SAMD_MKRWIFI1010)
24+ // For Arduino MKR WiFi 1010 using WiFiNINA library
25+ #include < WiFiNINA.h>
26+ #elif defined(ARDUINO_AVR_UNO_WIFI_REV2)
27+ // For Arduino Uno WiFi Rev2
28+ #include < WiFiNINA.h>
29+ #elif defined(ARDUINO_ARCH_SAMD)
30+ // For other SAMD boards
31+ #include < WiFiNINA.h>
32+ #else
33+ #include < WiFi.h>
34+ #endif
35+
36+ // Include the CheerLights library and instantiate the CheerLights object
37+ #include < CheerLights.h>
38+ CheerLights CheerLights;
39+
40+ // Include the secrets file that contains the WiFi credentials
41+ #include " secrets.h"
42+
1743unsigned long previousMillis = 0 ;
18- const long interval = 15000 ; // 15 seconds
44+ const long updateInterval = 15000 ;
1945
2046void setup () {
2147 Serial.begin (115200 );
2248 CheerLights.begin (SECRET_SSID, SECRET_PASSWORD);
2349 strip.begin ();
24- strip.show (); // Initialize all pixels to 'off'
50+
51+ // Get the current CheerLights color and set the LEDs to that color
52+ CheerLights.getCurrentColor ();
53+ Serial.print (" Current CheerLights Color: " );
54+ Serial.println (CheerLights.currentColorName ());
55+ setLEDColors (CheerLights.currentRed (), CheerLights.currentGreen (), CheerLights.currentBlue ());
2556}
2657
2758void loop () {
2859 unsigned long currentMillis = millis ();
2960
30- if (currentMillis - previousMillis >= interval) {
61+ // Update the LEDs every updateInterval milliseconds
62+ if (currentMillis - previousMillis >= updateInterval) {
3163 previousMillis = currentMillis;
3264
33- // Get the current CheerLights color
65+ // Get the current CheerLights color and set the LEDs to that color
66+ CheerLights.getCurrentColor ();
3467 Serial.print (" Current CheerLights Color: " );
35- Serial.println (CheerLights.getCurrentColor ());
36-
37- // Show the color as a hex value
38- Serial.print (" Color Hex: 0x" );
39- Serial.println (CheerLights.showColorHex (), HEX);
40-
41- // Show the RGB values
42- uint8_t red = CheerLights.showRed ();
43- uint8_t green = CheerLights.showGreen ();
44- uint8_t blue = CheerLights.showBlue ();
45-
46- Serial.print (" Red: " );
47- Serial.print (red);
48- Serial.print (" Green: " );
49- Serial.print (green);
50- Serial.print (" Blue: " );
51- Serial.println (blue);
52-
53- // Show the color name
54- Serial.print (" CheerLights Color Name: " );
55- Serial.println (CheerLights.showColorName ());
56-
57- // Set all pixels to the CheerLights color using the RGB values
58- for (int i = 0 ; i < NUM_LEDS; i++) {
59- strip.setPixelColor (i, strip.Color (red, green, blue));
60- }
61- strip.show ();
68+ Serial.println (CheerLights.currentColorName ());
69+ setLEDColors (CheerLights.currentRed (), CheerLights.currentGreen (), CheerLights.currentBlue ());
6270 }
63-
64- // Other non-blocking code can go here
6571}
72+
73+ void setLEDColors (uint8_t red, uint8_t green, uint8_t blue) {
74+ for (int i = 0 ; i < NUM_LEDS; i++) {
75+ strip.setPixelColor (i, strip.Color (red, green, blue));
76+ }
77+ strip.show ();
78+ }
0 commit comments