writing a sketch to run an LED strip when a signal is recieved

I have been trying to code my Arduino Uno to run a command from FastLED when a signal is received at the port. I have successfully run the LED display when I upload the basic sketch, but I want to add to this sketch so that the display is activated by a signal sent from a GUI with processing. Here is what I have for my sketch so far:

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 150

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 12

// Define the array of leds
CRGB leds[NUM_LEDS];



void setup() {
  Serial.begin(57600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  LEDS.setBrightness(85);
}

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(150);
  }
}


void loop() {
  if (Serial.available() > 0) {
    char val = Serial.read();
    if (val == '1') {
      static uint8_t hue = 0;
      Serial.print("x");
      // First slide the led in one direction
      for (int i = 0; i < NUM_LEDS; i++) {
        // Set the i'th led to red
        leds[i] = CHSV(160, 255, 255);
        // Show the leds
        FastLED.show();
        // now that we've shown the leds, reset the i'th led to black
        // leds[i] = CRGB::Black;
        fadeall();
        // Wait a little bit before we loop around and do it again
        delay(100);

      }

    }
    Serial.print("x");
  }
}

I know that the arduino recieves the signal when the button is pressed because a light on the board lights up, but the code doesn’t run, What should I do to solve this problem.

Thanks

Why not print val so you actually know what has been received?

Steve

KevinAgnew:
I know that the arduino recieves the signal when the button is pressed because a light on the board lights up, but the code doesn’t run, What should I do to solve this problem.

The led will light up regardless of what is received (it actually lights up at wrong baud rates as well)
yes Slipstick’s suggestion is a good plan.
btw this:

if (val == '1') {
      static uint8_t hue = 0;
      Serial.print("x");
      // First slide the led in one direction
      for (int i = 0; i < NUM_LEDS; i++) {
        // Set the i'th led to red
        leds[i] = CHSV(160, 255, 255);
        // Show the leds
        FastLED.show();
        // now that we've shown the leds, reset the i'th led to black
        // leds[i] = CRGB::Black;
        fadeall();
        // Wait a little bit before we loop around and do it again
        delay(100);

      }

    }

looks like it was a function before now it is cluttering up loop()

I have coded an LED strip to light up when I press a button on a processing GUI and turn off when a second button is pushed, but once turned off I can’t turn it back on again. What can I do to solve this problem? Below is my code for the Arduino:

#include <FastLED.h>

// define number of leds in strip
#define NUM_LEDS 150

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 12

// Define the array of leds
CRGB leds[NUM_LEDS];
boolean state = false;
void setup() {

  pinMode(3, OUTPUT);   //Set pin as an output
  Serial.begin(9600);   //Start serial communication @9600 bps


}

void loop() {

  if (Serial.available()) {

    char val = Serial.read();

    if (val == 't') {
      { Serial.begin(57600);
        Serial.println("resetting");
        LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
        LEDS.setBrightness(85);


        for (int i = 0; i < NUM_LEDS; i++) {
          leds[i].nscale8(150);
        }
      }

      {
        static uint8_t hue = 0;
        Serial.print("x");
        // First slide the led in one direction
        for (int i = 0; i < NUM_LEDS; i++) {
          // Set the i'th led to red
          leds[i] = CHSV(hue++, 255, 255);
          // Show the leds
          FastLED.show();
          // now that we've shown the leds, reset the i'th led to black
          // leds[i] = CRGB::Black;
          ;
          // Wait a little bit before we loop around and do it again
          delay(0.1);

          Serial.print("x");
        }
      }
    } else {
      state = false;
      {
        Serial.begin(57600);
        Serial.println("resetting");
        LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
        LEDS.setBrightness(84);
      }

      {
        for (int i = 0; i < NUM_LEDS; i++) {
          leds[i].nscale8(250);
        }
      }

      {
        static uint8_t hue = 0;
        Serial.print("x");
        // First slide the led in one direction
        for (int i = 0; i < NUM_LEDS; i++) {
          // Set the i'th led to red
          leds[i] = CHSV(hue++, 0, 0);
          // Show the leds
          FastLED.show();
          // now that we've shown the leds, reset the i'th led to black
          // leds[i] = CRGB::Black;

          // Wait a little bit before we loop around and do it again
          delay(10);
        }
        Serial.print("x");
        ;
      }
    }
  }

 {

    if (Serial.available()) {

      char val = Serial.read();
      if (val == 'r') {
        { Serial.begin(57600);
          Serial.println("resetting");
          LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
          LEDS.setBrightness(85);


          for (int i = 0; i < NUM_LEDS; i++) {
            leds[i].nscale8(150);
          }
        }

        {
          static uint8_t hue = 0;
          Serial.print("x");
          // First slide the led in one direction
          for (int i = 0; i < NUM_LEDS; i++) {
            // Set the i'th led to red
            leds[i] = CHSV(hue++, 0, 0);
            // Show the leds
            FastLED.show();
            // now that we've shown the leds, reset the i'th led to black
            // leds[i] = CRGB::Black;
            ;
            // Wait a little bit before we loop around and do it again
            delay(100);
          }
          Serial.print("x");

        }


      }
    }
  }
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

char val = Serial.read();

    if (val == 't') {
      { Serial.begin(57600);
        Serial.println("resetting");

Why do you keep calling Serial.begin and with a different baud rate than in setup()?

What is the baud rate of the Processing program?I

delay(0.1);

Delay will not work with a float data type. You could use delayMicroseconds (100).

Does calling serial have something to do with my problem? I am new to programming arduino so I figured I had to have it before all functions that require it. My baud rate 115200 In Processing after I press the lights off button I can't turn the LED strip back on, but the lights on button can be pressed multiple times lighting the strand up again and again. How can I change the code so I can turn the lights back on after having turned them off?

You only need Serial.begin() once in your program. In setup is where it usually goes.

You say that the Processing baud rate is 115200, but in the Arduino program you have 9600 and 57800. Put

Serial.begin(115200);

in setup() and only in setup().

Until you get serial communication working right, the rest of the improperly posted program* does not matter much.

Serial.println("resetting");

To where are you printing? The Processing program?

Since you did not post in code tags the forum software strips out every[i](led array indexes) in the code, making it impossible to compile to check further. I also note some extra curly brackets and at least one orphan semicolon (;).

Do you see how the forum software set part of your code to italics. That is because you did not post the code properly. Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and **format the code with the IDE autoformat tool* (crtl-t or Tools, Auto Format) before posting code. Autoformat should help to show where the extra brackets and semicolon are.

        LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
        LEDS.setBrightness(84);

Also this belongs only in setup().

#include <FastLED.h>

// define number of leds in strip
#define NUM_LEDS 150

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 12

// Define the array of leds
CRGB leds[NUM_LEDS];
boolean state = false;
void setup() {

  pinMode(3, OUTPUT);   //Set pin as an output
  Serial.begin(9600);   //Start serial communication @9600 bps


}

void loop() {

  if (Serial.available()) {

    char val = Serial.read();

    if (val == 't') {
      {
        LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
        LEDS.setBrightness(85);


        for (int i = 0; i < NUM_LEDS; i++) {
          leds[i].nscale8(150);
        }
      }

      {
        static uint8_t hue = 0;
        Serial.print("x");
        // First slide the led in one direction
        for (int i = 0; i < NUM_LEDS; i++) {
          // Set the i'th led to red
          leds[i] = CHSV(hue++, 255, 255);
          // Show the leds
          FastLED.show();
          // now that we've shown the leds, reset the i'th led to black
          // leds[i] = CRGB::Black;
          ;
          // Wait a little bit before we loop around and do it again
          delay(0.1);

          Serial.print("x");
        }
      }
    } else {
      state = false;
      {
        Serial.begin(57600);
        Serial.println("resetting");
        LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
        LEDS.setBrightness(84);
      }

      {
        for (int i = 0; i < NUM_LEDS; i++) {
          leds[i].nscale8(250);
        }
      }

      {
        static uint8_t hue = 0;
        Serial.print("x");
        // First slide the led in one direction
        for (int i = 0; i < NUM_LEDS; i++) {
          // Set the i'th led to red
          leds[i] = CHSV(hue++, 0, 0);
          // Show the leds
          FastLED.show();
          // now that we've shown the leds, reset the i'th led to black
          // leds[i] = CRGB::Black;

          // Wait a little bit before we loop around and do it again
          delayMicroseconds(100);
        }
        Serial.print("x");
        ;
      }
    }
  }
  {

    if (Serial.available()) {

      char val = Serial.read();
      if (val == 'r') {
        {
          LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
          LEDS.setBrightness(85);


          for (int i = 0; i < NUM_LEDS; i++) {
            leds[i].nscale8(150);
          }
        }

        {
          static uint8_t hue = 0;
          Serial.print("x");
          // First slide the led in one direction
          for (int i = 0; i < NUM_LEDS; i++) {
            // Set the i'th led to red
            leds[i] = CHSV(hue++, 0, 0);
            // Show the leds
            FastLED.show();
            // now that we've shown the leds, reset the i'th led to black
            // leds[i] = CRGB::Black;
            ;
            // Wait a little bit before we loop around and do it again
            delay(100);
          }
          Serial.print("x");
        }
      }
    }
  }
}

I tried putting

        LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
        LEDS.setBrightness(84);

only in the setup and when I did the GUI didn’t work at all.

Also I am unsure how to answer the questions about printing.

The latest code that you posted still has Serial.begin(9600). Is Processing still set to 115200 baud?

The serial input basics thread may help you.

I have programmed the strip to have a cylon effect using FastLED. I am trying to get the string of lights to light up at a very fast speed, but I am having trouble. It seems that the more strands I connect the slower the effect gets. What can I do to solve this problem?

Here is my code:

#include <FastLED.h>

// define number of leds in strip
#define NUM_LEDS 300

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 12

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(57600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  LEDS.setBrightness(85);
}

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(200);
  }
}

void loop() {
  static uint8_t hue = 0;
  Serial.print("x");
  // First slide the led in one direction
  for (int i = 0; i < NUM_LEDS; i++) {
    // Set the i'th led to red
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again units of miliseconds
    delay(.001);
  }
  Serial.print("x");

}

I am also having trouble lighting up all four strips. I can light up to about half of the fourth strip, but if code it to light up the entire four strips I get the error message: Cannot read property ‘length’ of undefined

I am trying to create a GUI to light up my LED strip at several different speeds. I would like to be able to turn on the cylon effect at several different speeds. So far I have been successful in programming the GUI to do the cylon effect at several different speeds, but I haven’t been successful in making the cylon effect loop.

Here is the code for the GUI:

#include <FastLED.h>

// define number of leds in strip
#define NUM_LEDS 450

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 12


// Define the array of leds
CRGB leds[NUM_LEDS];
boolean state = false;
void setup() {

  pinMode(3, OUTPUT);   //Set pin as an output
  Serial.begin(9600);   //Start serial communication @9600 bps

}

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(150);
  }
}

void loop () {

  if (Serial.available()) {

    char val = Serial.read();

    if (val == 't') {
      {
        LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
        LEDS.setBrightness(85);


        for (int i = 0; i < NUM_LEDS; i++) {
          leds[i].nscale8(150);
        }
      }

      {
        static uint8_t hue = 0;
        Serial.print("x");
        // First slide the led in one direction
        for (int i = 0; i < NUM_LEDS; i++) {
          // Set the i'th led to red
          leds[i] = CHSV(0, 255, 255);
          // Show the leds
          FastLED.show();
          // now that we've shown the leds, reset the i'th led to black
          // leds[i] = CRGB::Black;
          fadeall();
          // Wait a little bit before we loop around and do it again
          delay(1);

          Serial.print("x");
        }
      }
    }
  }


  {

    if (Serial.available()) {
      char val = Serial.read();
      if (val == 'r') {
        {
          LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
          LEDS.setBrightness(85);
          for (int i = 0; i < NUM_LEDS; i++) {
            leds[i].nscale8(150);
          }
        }
        {
          static uint8_t hue = 0;
          Serial.print("x");
          // First slide the led in one direction
          for (int i = 0; i < NUM_LEDS; i++) {
            // Set the i'th led to red
            leds[i] = CHSV(160, 255, 255);
            // Show the leds
            FastLED.show();
            // now that we've shown the leds, reset the i'th led to black
            // leds[i] = CRGB::Black;
            fadeall();
            // Wait a little bit before we loop around and do it again
            Serial.print("x");
          }
        }
      }
    }
  }
}

Here is the code for just the cylon effect:

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 150

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 12

// Define the array of leds
CRGB leds[NUM_LEDS];


void setup() {
  Serial.begin(57600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  LEDS.setBrightness(84);
}

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(150);
  }
}

void loop() {
  static uint8_t hue = 0;
  Serial.print("x");
  // First slide the led in one direction
  for (int i = 0; i < NUM_LEDS; i++) {
    // Set the i'th led to red
    leds[i] = CHSV(160, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(100);
  }

  Serial.print("x")

;}

What can I do to loop the effect in my GUI code?

What is your power supply rating?

why use delay(.001); when there is delaymicros()? f you want to delay 1mS then its delay(1).

Where do you define led strip 2 ,3, 4?

Idahowalker: Where do you define led strip 2 ,3, 4?

You don't need to if it is one long strip that he has connected together.

why at all a Delay

cyclone use good hue sat and val

void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { LED[i].nscale8(250); } }

void cyclon(){
  static uint8_t hue = 0;
  // First slide the led in one direction
  for(int i = 0; i < NUM_LEDS; i++) {
    // Set the i'th led to red 
    LED[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show(); 
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(10);
      
  }
  // Now go in the other direction.  
  for(int i = (NUM_LEDS)-1; i >= 0; i--) {
    // Set the i'th led to red 
    LED[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    //delay(10);
  }
}

Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Could you take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future. Other general help and troubleshooting advice can be found here.

Bob.