How to turn SK6812 RGBW LED strip with button Click using C# windows form?

Hello. I am new to arduino and RGBW LED strip. I am using SK6812 RGBW LED Strip.
This post helped me in filling the LED strip with single color.
However, I want to make the LED strip glow once the buttion from C# form is clicked. I have used serial Port and when the char '1' is sent, the led strip is to glow. But, the code is not working.

The Arduino code is as follows:

#include "FastLED.h"
#include "FastLED_RGBW.h"
#define NUM_LEDS 300
#define DATA_PIN 6
CRGBW leds[NUM_LEDS];
CRGB *ledsRGB = (CRGB *) &leds[0];
const uint8_t brightness = 128;
char incomingOption;
void setup() { 
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(ledsRGB, getRGBWsize(NUM_LEDS));
  FastLED.setBrightness(brightness);
  FastLED.show();
}
void loop(){

  incomingOption = Serial.read();

  switch (incomingOption){
    case'1':
    colorFill(CRGB::Red);
    break;

    case'2':
    colorFill(CRGB::Green);
    break;

    case'3':
    colorFill(CRGB::Blue);
    break;

    case'0':
    fillWhite();
  }
}
void colorFill(CRGB c){
  for(int i = 0; i < NUM_LEDS; i++){
    leds[i] = c;
    FastLED.show();
    delay(50);
  }
  delay(500);
}
void fillWhite(){
  for(int i = 0; i < NUM_LEDS; i++){
    leds[i] = CRGBW(0, 0, 0, 255);
    FastLED.show();
    delay(50);
  }
  delay(500);
}

The FastLED.h code is same as mentioned in [this post].(FastLED with RGBW NeoPixels (SK6812) - Parts Not Included)

The C# code snippet is as follows:

private void btnRedOn_Click(object sender, EventArgs e)
        {
            
            
            try
            {
                serialPort1.Write("1"); //send 1 to audrino
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            
        }      

How do I turn on the LED strip using C# WinForm Button
P.S.: In the simple RGB LED strip, the Code in C# works. It is not working in this RGBW Strip.
Th epicture of the RGBG strip I am using is below.

What do you see if you print the value of incomingOption ?

Where, In arduino or C#?

On the Arduino. After all, that is where you test its value and if it is not what you think then the test will never return true

I am not quite sure, where should I print it?
I added ** Serial.print(incomingOption);** as in the following code snippet, but it did not print anything.

switch (incomingOption){
    case '1':
    colorFill(CRGB::Red);
    Serial.print(incomingOption);
    break;

Print it immediately before the switch() that uses it, not in a case where it has already been tested

It is printing nothing.
image

Do you thinks that it might work better if you had Serial.begin() in setup() ?

What does "turn on" mean? Set the colour as in your C# snippet?

Based on the information that you have provided, I suggest that you modify your Arduino code to echo the received command (Serial.print). Then, in the C# code

  1. Add a multiline textbox where you will display the received data.
  2. After a write to the port, do a read to check what the Arduino echoed back.
  3. Dump that in the textbox

This will help you to debug.

When the button is clicked, the LED strip glows. I want to control the LED strip by the C# winform button.

Can you post the full code of the form. If you created your own C# classes in different files, please post those files as well.

RGBW_LED Woprking.zip (72.4 KB)
This is the full source code.

Thanks for providing everything.

  1. As indicated in post #9 by @UKHeliBob , you're still missing a Serial.begin() in setup(); did you ever test this with serial monitor.
  2. You should check if serial data is available before reading it.
  3. Your code is unresponsive; it takes ages once you have send a dgit before it reacts to another digit. This is due to your 300x50 ms delay. I suggest that you rewrite it in a non-blocking way.

Please fix at least (1) and (2) and preferably (3) and post a new version of the sketch [edit]if you still have problems[/edit].

I don't immediately see something wrong with the C# code but haven't tested it.

Since, I am new in arduino, I have not tested these all. I will try to test them all. Thank You.

I solved this problem. Thank you everyone for your help.

#include "FastLED.h"
#include "FastLED_RGBW.h"
#define NUM_LEDS 300
#define DATA_PIN 6
#define BaudRate 9600

char incomingOption;

CRGBW leds[NUM_LEDS];
CRGB *ledsRGB = (CRGB *) &leds[0];
const uint8_t brightness = 128;


void setup() { 
  Serial.begin(BaudRate);
  
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(ledsRGB, getRGBWsize(NUM_LEDS));
  FastLED.setBrightness(brightness);
  FastLED.show();
}
void loop(){
  incomingOption=Serial.read();
  switch(incomingOption){
    case '1':
      colorFill(CRGB::Red);
      break;
    case '0':
      fillWhite();
      break;
  }
}

void colorFill(CRGB c){
  for(int i = 0; i < NUM_LEDS; i++){
    leds[i] = c;
    FastLED.show();
    delay(1);
  }
  delay(500);
}
void fillWhite(){
  for(int i = 0; i < NUM_LEDS; i++){
    leds[i] = CRGBW(0, 0, 0, 255);
    FastLED.show();
    delay(1);
  }
  delay(500);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.