Eunice Yuan_Skills Dev III
Made by yuany4
Made by yuany4
A push button toggles the Neopixel on and off, with an LED indicator displaying the switch status. When the Neopixel is off, a yellow LED lights up, indicating that the Neopixel is offline.
Created: November 11th, 2024
A push button toggles the Neopixel on and off, with an LED indicator showing the switch status. When the Neopixel is off, a yellow LED lights up to indicate it is offline. When the Neopixel is online, it lights up green by default. However, if the startMeeting action is triggered, the Neopixel will change to red.
I've connected the yellow LED to pin D1 and the push button, acting as the switch, to pin D3 in my circuit. I added this manual switch because I realized the Neopixel is too bright when it's on, so I wanted the ability to turn it off manually. The Neopixel is connected to pin MO, ensuring the code functions properly.
#include "Particle.h"
#include <neopixel.h>
SYSTEM_MODE(AUTOMATIC);
// Pin assignments
int ledPin = D1; // LED pin
int buttonPin = D3; // Pushbutton pin
// NeoPixel setup
#define PIXEL_PIN SPI
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// Prototypes for local build, ok to leave in for Build IDE
void cyclePixels(uint8_t wait);
int startMeeting(String command); // Cloud function to start the meeting (change to red)
void fadeToRed(); // Function to fade pixels to red
unsigned long buttonPressTime = 0; // Keep track of how long the button is pressed
bool stripActive = true; // To track if the strip is active or dismissed
bool buttonPressed = false; // To track if the button was already pressed
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Button input with pull-up
pinMode(ledPin, OUTPUT); // LED output
strip.begin(); // Initialize the NeoPixel strip
strip.show(); // Initialize all pixels to 'off'
Particle.function("startMeeting", startMeeting); // Register the function with Particle Cloud
Serial.begin(9600); // Start Serial Monitor for debugging
}
void loop() {
// Check if the button is pressed
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) { // Button is pressed
if (!buttonPressed) {
buttonPressTime = millis(); // Start counting time when button is pressed
buttonPressed = true; // Mark button as pressed
}
// Check if the button has been pressed for 3 seconds
if (millis() - buttonPressTime >= 3000) {
// Toggle the stripActive state
stripActive = !stripActive;
buttonPressed = false; // Reset button pressed state after toggle
buttonPressTime = 0; // Reset the timer
// Update LED and NeoPixel based on the new stripActive state
if (!stripActive) {
strip.clear(); // Turn off all pixels
strip.show();
digitalWrite(ledPin, HIGH); // Turn on LED when NeoPixel turns off
} else {
digitalWrite(ledPin, LOW); // Turn off LED when NeoPixel turns on
fadeToRed(); // Fade to red when the strip is active
}
}
} else { // Button not pressed
buttonPressTime = 0; // Reset the timer if button is released
buttonPressed = false; // Reset button pressed state
// Continue cycling pixels if the strip is active
if (stripActive) {
cyclePixels(500); // Keep cycling the pixels if active
}
}
}
// Cloud function to change the color of the strip to red
int startMeeting(String command) {
Serial.println("startMeeting function called");
// Set all pixels to red
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(255, 0, 0)); // Red color
}
strip.show(); // Make sure the update is applied to the strip
Serial.println("Color set to red");
// Wait for 3 seconds (strip will stay red for this duration)
delay(3000);
// Reset the strip to its previous state (off in this case)
strip.clear(); // Turn off all pixels
strip.show(); // Update the strip
Serial.println("Strip reset to off");
return 0; // Return success
}
// Function to fade the NeoPixel strip to red gradually
void fadeToRed() {
for (int i = 0; i < 256; i++) { // Fade in red from 0 to 255
for (int j = 0; j < strip.numPixels(); j++) {
strip.setPixelColor(j, strip.Color(i, 0, 0)); // Gradually increase red
}
strip.show();
delay(10); // Delay to control the speed of the fade
}
}
void cyclePixels(uint8_t wait) {
static uint8_t pixelIndex = 0; // Keep track of the pixel to light up
// Turn off all pixels
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0); // Set each pixel to off
}
// Light up the current pixel with green
strip.setPixelColor(pixelIndex, strip.Color(0, 255, 0)); // Green color
// Update the strip to show the change
strip.show();
// Wait for the specified time
delay(wait);
// Move to the next pixel. If we're at the last one, reset to the first.
pixelIndex++;
if (pixelIndex >= strip.numPixels()) {
pixelIndex = 0;
}
}
Click to Expand
A push button toggles the Neopixel on and off, with an LED indicator displaying the switch status. When the Neopixel is off, a yellow LED lights up, indicating that the Neopixel is offline.