Guest User

Untitled

a guest
Nov 15th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiUdp.h>
  3. #include <FastLED.h>
  4.  
  5. //set up WiFi
  6. const char* ssid = *****
  7. const char* password = ****
  8. const int localPort = 8888;
  9.  
  10. WiFiUDP udp;
  11.  
  12. //set up LEDs
  13. FASTLED_USING_NAMESPACE
  14.  
  15. #define DATA_PIN    13
  16. //#define CLK_PIN   4
  17. #define LED_TYPE    WS2812B
  18. #define COLOR_ORDER GRB
  19. #define NUM_LEDS    144
  20.  
  21. CRGB leds[NUM_LEDS];
  22.  
  23. #define BRIGHTNESS          255
  24. #define FRAMES_PER_SECOND  120
  25.  
  26.  
  27. //received potentiometer inputs from WiFi connection
  28. int inputs[4];
  29.  
  30. int paletteIndex = 0;
  31.  
  32. CRGBPalette256 currentPalette;
  33. TBlendType    currentBlending;
  34.  
  35.  
  36. void setup() {
  37.  
  38.   //set up LEDs
  39.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  40.  
  41.   // set master brightness control
  42.   FastLED.setBrightness(BRIGHTNESS);
  43.  
  44.   currentPalette = LavaColors_p;
  45.  
  46.   delay(2000);
  47.  
  48.   Serial.begin(115200);
  49.   delay(10);
  50.  
  51.   // Connect to Wi-Fi
  52.   Serial.println();
  53.   Serial.print("Connecting to ");
  54.   Serial.println(ssid);
  55.   WiFi.begin(ssid, password);
  56.  
  57.   while (WiFi.status() != WL_CONNECTED) {
  58.     delay(500);
  59.     Serial.print(".");
  60.   }
  61.   Serial.println("WiFi connected");
  62.  
  63.   // Start UDP
  64.   udp.begin(localPort);
  65. }
  66.  
  67.  
  68. void loop() {
  69.   receiver();
  70.  
  71.   fill_palette(leds,NUM_LEDS,paletteIndex,255/NUM_LEDS,currentPalette,255,LINEARBLEND);
  72.  
  73.   fadeToBlackBy(leds,NUM_LEDS,1);
  74.  
  75.   paletteChooser(inputs[0]);
  76.  
  77.   EVERY_N_MILLISECONDS(10){
  78.     paletteIndex += inputs[1];
  79.   }
  80.  
  81.   // send the 'leds' array out to the actual LED strip
  82.   FastLED.show();
  83.  
  84.   // insert a delay to keep the framerate modest
  85.   FastLED.delay(1000/FRAMES_PER_SECOND);
  86.  
  87. }
  88.  
  89. void paletteChooser(int j){
  90.  
  91.   if (j == 0){
  92.     currentPalette = OceanColors_p;
  93.     currentBlending = LINEARBLEND;
  94.   }
  95.  
  96.   if (j == 1){
  97.     currentPalette = LavaColors_p;
  98.     currentBlending = LINEARBLEND;
  99.   }
  100.  
  101.   if (j == 2){
  102.     currentPalette = CloudColors_p;
  103.     currentBlending = LINEARBLEND;
  104.   }
  105.  
  106.   if (j = 3){
  107.     currentPalette = ForestColors_p;
  108.     currentBlending = LINEARBLEND;
  109.   }
  110. }
  111.  
  112.  
  113. void receiver(){
  114.   int packetSize = udp.parsePacket();
  115.  
  116.   if (packetSize) {
  117.     // Allocate buffer for incoming packet
  118.     char incomingPacket[255];
  119.     udp.read(incomingPacket, packetSize);
  120.     incomingPacket[packetSize] = '\0';
  121.  
  122.     // Parse the received string back into an array of integers
  123.     char *token = strtok(incomingPacket, ",");
  124.     for (int i = 0; i < 4; i++) {
  125.       inputs[i] = atoi(token);
  126.       token = strtok(NULL, ",");
  127.     }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment