Go Down

Topic: WS2812B RGB Led Strip communication with PC  (Read 2885 times) previous topic - next topic

Kostek454

Nov 23, 2016, 03:28 pm Last Edit: Nov 23, 2016, 08:27 pm by Kostek454
Hi
I have a WS2812B RGB Led Strip. I want to cotrol my led strip from PC, what i mean is to controll it by serial port monitor. Can i do this by digitalWrite(); command or sth ? I want to controll each diod separately. What I mean control is to Set it OF or ON, set parameters of color R,G,B.
Have anyone some idea how to run my Led strip by Serial port monitor ?

Thank you in advance

Grumpy_Mike

Quote
Can i do this by digitalWrite(); command
No. Those LEDs require a fast, precise pulse train, which is beyond the capabilities of a digitalWrite function call.

Quote
Have anyone some idea how to run my Led strip by Serial port monitor ?
You can not do this direct from the  Serial port monitor, you need to put an Arduino between your PC and the LED strip. You might know this but it did not come across in your post.

So assuming you have an Arduino then look at the serial port examples in the IDE. Also look at the examples in the fastLED libiary.

Then try the examples and finally when you understand what you are doing write code that does exactly what you want to do.

Kostek454

#2
Nov 25, 2016, 11:59 am Last Edit: Nov 28, 2016, 10:53 am by Kostek454
Thanks for answer. Of course i want to have connection with Arduino PC -> Arduino-> Led Strip.
I was looknig to fastLED library and made some simple code :

Code: [Select]

#include "FastLED.h"
#define NUM_LEDS 20
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
int r;
int g;
int b;
void setup() {
Serial.begin(9600);
delay(2000);
    FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
 
}
void loop() {
r=255;
g=0;
b=0;

while (Serial.available() == 0);

int val = Serial.read() - '0';

Serial.println(val);

leds[val] = CRGB(r,g,b);
 
 FastLED.show();
 delay(1000);
}



Now i can control first 9 Leds, and have a problem with controllig rest of Led on my strip. When i write for example diod number 24 it turns On diode nummber 2 and nummber 4, so it works only for Diodes from nummber 0 -9. How to cotrol rest of them ?

Grumpy_Mike

You get the characters one at a time from the serial port. To get a number from several characters you have to do a bit more work, see https://forum.arduino.cc/index.php?topic=288234.0

Also
Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

Kostek454

Thanks it helps me a lot, but i have already one problem. My code works only whent i enter data in this style <text,value d,value r,value g,value b>,

 i want to enter data without this text part:
 <value d,value r,value g,value b>

Here is my code:
Code: [Select]
#include "FastLED.h"
#define NUM_LEDS 144
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0};

int d = 0;
int r = 0;
int g = 0;
int b = 0;


boolean newData = false;

//============

void setup() {
    FastLED.clear ();
    Serial.begin(9600);
    Serial.println("Enter data in this style <text,int d,int r,int g,int b>  ");
    Serial.println();
    FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
     
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
       
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts
 
    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars, ",");      // get the first part - the string
    strcpy(messageFromPC,strtokIndx); // copy it to messageFromPC

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    d = atoi(strtokIndx);    // convert this part to an integer
    d=d/5;
     
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    r = atoi(strtokIndx);     // convert this part to an integer
     
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    g = atoi(strtokIndx);     // convert this part to an integer
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    b = atoi(strtokIndx);     // convert this part to an integer

}

//============

void showParsedData() {
    FastLED.clear ();
    delay(250);
    leds[d-1] = CRGB(r,g,b);
    leds[d] = CRGB(r,g,b);


How can i change/delete this text part from char to int ??

Grumpy_Mike

Try removing these two lines
Code: [Select]
    strtokIndx = strtok(tempChars, ",");      // get the first part - the string
    strcpy(messageFromPC,strtokIndx); // copy it to messageFromPC

Kostek454

When I remove those two lines it does not work properly. I enter data like this : <25,255,255,255> and also like <text,25,255,255,255>. Any diod turns on ...
I print at the end all the values d , r, g and b and they all have value 0

Grumpy_Mike

Make the next line not
Code: [Select]
strtokIndx = strtok(NULL, ",");

But
Code: [Select]
strtokIndx = strtok(tempChars, ",");

Go Up