Advertisement
pleasedontcode

**LED Control** rev_01

Feb 24th, 2025
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **LED Control**
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2025-02-24 20:39:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Agregar otra secuencia de leds q donde 16 leds de */
  21.     /* una tira led se prendan y apaguen en verde cada 5 */
  22.     /* segundos solo si por comunicacion serial se */
  23.     /* recibio un 2, si no se recibe solo activa la */
  24.     /* primer secuencia */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Adafruit_NeoPixel.h>  //https://github.com/adafruit/Adafruit_NeoPixel
  31. #include <FastLED.h>    //https://github.com/FastLED/FastLED
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs();
  37. void activateSecondSequence();
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t led_WS2812B_DIN_PIN_D2        = 2;
  41. #define LED_PIN 6 // This pin definition conflicts with the existing pin definition for WS2812B
  42. #define LED_COUNT 18
  43. #define MAX_INTENSITY 150
  44. #define NUM_LEDS_ON 4
  45. #define SECOND_SEQUENCE_LED_COUNT 16 // Number of LEDs in the second sequence
  46. #define SECOND_SEQUENCE_DELAY 5000 // Delay for the second sequence
  47.  
  48. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  49. /***** used to store raw data *****/
  50. bool    led_WS2812B_DIN_PIN_D2_rawData      = 0;
  51.  
  52. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  53. /***** used to store data after characteristic curve transformation *****/
  54. float   led_WS2812B_DIN_PIN_D2_phyData      = 0.0;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); // Initialize NeoPixel strip
  58.  
  59. // Additional variables for the second LED sequence
  60. unsigned long previousMillis = 0; // Store the last time the second sequence was updated
  61. bool secondSequenceActive = false; // Flag to check if the second sequence should be active
  62.  
  63. void setup(void)
  64. {
  65.     // put your setup code here, to run once:
  66.     pinMode(led_WS2812B_DIN_PIN_D2,  OUTPUT);
  67.     strip.begin(); // Initialize the NeoPixel strip
  68.  
  69.     // Apagar todos los LEDs al inicio
  70.     for (int i = 0; i < LED_COUNT; i++) {
  71.         strip.setPixelColor(i, strip.Color(0, 0, 0));
  72.     }
  73.     delay(500);
  74.     strip.show();
  75.    
  76.     for (int i = 0; i < LED_COUNT; i++) {
  77.         if (i >= 4) {
  78.             strip.setPixelColor(i, strip.Color(60, 30, 200));
  79.         }
  80.     }
  81.     strip.show(); // Mostrar los cambios
  82.  
  83.     // Esperar un momento para que se apaguen todos los LEDs completamente
  84.     delay(5000);
  85.     Serial.begin(9600); // Start serial communication
  86. }
  87.  
  88. void loop(void)
  89. {
  90.     // put your main code here, to run repeatedly:
  91.     updateOutputs(); // Refresh output data
  92.  
  93.     // Check for serial input
  94.     if (Serial.available() > 0) {
  95.         int incomingByte = Serial.read();
  96.         if (incomingByte == '2') {
  97.             secondSequenceActive = true; // Activate second sequence if '2' is received
  98.         } else {
  99.             secondSequenceActive = false; // Deactivate second sequence for any other input
  100.         }
  101.     }
  102.  
  103.     // Handle the first LED sequence
  104.     static int offset = 4; // Start at the fifth LED
  105.     static int fadeValue = 0;
  106.     static int direction = 1;
  107.  
  108.     // Encender los LEDs en el rango deseado
  109.     for (int i = offset; i < offset + NUM_LEDS_ON; i++) {
  110.         int index = i % LED_COUNT;
  111.         if (index >= 4) { // Evitar encender los primeros cuatro LEDs al reiniciar la secuencia
  112.             int brightness = (i == offset || i == offset + 5 || i == offset + 10 || i == offset + 15) ? MAX_INTENSITY : 0; // Encender solo ciertos LEDs
  113.             strip.setPixelColor(index, strip.Color(0, brightness, 0));
  114.         }
  115.     }
  116.  
  117.     // Suavizado del cambio del Ășltimo LED al primero
  118.     fadeValue += direction;
  119.     if (fadeValue >= MAX_INTENSITY || fadeValue <= 0) {
  120.         direction *= -1;
  121.     }
  122.  
  123.     // Establecer la intensidad del Ășltimo LED
  124.     if ((offset + NUM_LEDS_ON) % LED_COUNT >= 4) { // Evitar encender los primeros cuatro LEDs al reiniciar la secuencia
  125.         strip.setPixelColor((offset + NUM_LEDS_ON) % LED_COUNT, strip.Color(0, fadeValue, 0));
  126.     }
  127.  
  128.     strip.show();
  129.     delay(50); // Ajusta este valor para la velocidad del desplazamiento
  130.  
  131.     // Actualizar el desplazamiento
  132.     offset = (offset + 1) % LED_COUNT;
  133.  
  134.     // Handle the second LED sequence
  135.     if (secondSequenceActive) {
  136.         unsigned long currentMillis = millis();
  137.         if (currentMillis - previousMillis >= SECOND_SEQUENCE_DELAY) {
  138.             previousMillis = currentMillis; // Save the last time the second sequence was updated
  139.             activateSecondSequence(); // Activate the second LED sequence
  140.         }
  141.     }
  142. }
  143.  
  144. void updateOutputs()
  145. {
  146.     digitalWrite(led_WS2812B_DIN_PIN_D2, led_WS2812B_DIN_PIN_D2_rawData);
  147. }
  148.  
  149. void activateSecondSequence() {
  150.     // Turn on the second sequence of LEDs in green
  151.     for (int i = 0; i < SECOND_SEQUENCE_LED_COUNT; i++) {
  152.         strip.setPixelColor(i, strip.Color(0, 255, 0)); // Set color to green
  153.     }
  154.     strip.show(); // Show the changes
  155.     delay(SECOND_SEQUENCE_DELAY); // Keep them on for the defined delay
  156.     // Turn off the second sequence
  157.     for (int i = 0; i < SECOND_SEQUENCE_LED_COUNT; i++) {
  158.         strip.setPixelColor(i, strip.Color(0, 0, 0)); // Turn off LEDs
  159.     }
  160.     strip.show(); // Show the changes
  161. }
  162.  
  163. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement