/* Written by: Bartosz Last update: 11th January 2024. This file contains code for Rewise GFG Steelworks Retrofit - Continuous Casting Sector. It includes a button functionality to turn on and off an animation where a set of LEDs turns on and moves forward every few seconds. -------------------------- Wiring Instructions (sensor -> Arduino) -------------------------- LED Strip - Arduino Nano GND -> GND 5V -> 5V DIN -> Pin 6 Button 1 (Stop Button) - Arduino Nano + -> Pin 8 - -> GND */ //------------------ LIBRARIES ------------------ #include #include #include #include #ifdef __AVR__ #include #endif #include //------------------ VARIABLES ------------------ // Variables created for LED Strip #define LED_PIN 6 #define NUM_LEDS 15 #define BRIGHTNESS 100 #define LED_TYPE WS2811 #define COLOR_ORDER GRB CRGB leds[NUM_LEDS]; // Array to hold color data for each LED unsigned long previousMillis = 0; const long interval = 1000; // 2 seconds interval const unsigned int buttonPin = 8; // Pin for the button bool ledsOn = false; // Flag to track the state of LEDs // -------------- COLORS ------------------------ // Choose the intensity values for red, green, and blue (0-255) uint8_t red = 230; uint8_t green = 230; uint8_t blue = 255; //------------------------ SETUP ------------------------ void setup() { delay(1000); // Initial delay for stability FastLED.addLeds(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); FastLED.setBrightness(BRIGHTNESS); // Set the brightness of the LED strip Serial.begin(9600); // Initialize serial communication for debugging pinMode(buttonPin, INPUT_PULLUP); // Set up the button pin as input with internal pull-up resistor } //------------------------ LOOP ------------------------ void loop() { unsigned long currentMillis = millis(); // Get the current time in milliseconds checkButtonState(); // Check the state of the button updateLEDs(); // Update the LEDs based on the button state } //------------------------ CHECK BUTTON STATE ------------------------ void checkButtonState() { int buttonState = digitalRead(buttonPin); // Read the state of the button // If the button is pressed (LOW), toggle the LEDs state if (buttonState == LOW) { delay(350); // Add a small delay for button debouncing ledsOn = !ledsOn; // Toggle the state of the LEDs } } //------------------------ UPDATE LEDs ------------------------ void updateLEDs() { // If LEDs are on and the specified interval has passed since the last animation update if (ledsOn && millis() - previousMillis >= interval) { previousMillis = millis(); // Save the current time for the next interval animateLEDs(); // Call the function to animate LEDs } else if (!ledsOn) { fill_solid(leds, NUM_LEDS, CRGB::Black); // If LEDs are off, set all LEDs to off (black) FastLED.show(); // Show the updated LED colors } } //------------------------ ANIMATE LEDs ------------------------ void animateLEDs() { static int startIndex = 0; // Variable to keep track of the starting index for animation // Turn off the current set of LEDs fill_solid(leds, NUM_LEDS, CRGB::Black); // Create a CRGB object representing the dark color CRGB color = CRGB(red, green, blue); // Move to the next set of LEDs for (int i = startIndex; i < NUM_LEDS; i += 3) { leds[i] = color; // Set the LED on } // Update the start index for the next iteration startIndex = (startIndex + 1) % 3; // Increment the start index, ensuring it wraps around // Show the updated LED colors FastLED.show(); }