Go Down

Topic: manually control p9813 controled 5050 rgb led via serial monitor  (Read 1 time) previous topic - next topic

spikey1973

in the mean time i've been trying to find a easy way of reading int via serial read but well it just ain't there.

the code AWOL lets on a post looks nice but as i need to read out three numbers it seems rather useless to me. pitty as i is a really nice code imho.

Code: [Select]
const int ledPin = 11;
char val [4];
int index;

void setup()
{
// begin the serial communication
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
 index = 0;
}

void loop()
{
// check if data has been sent from the computer
if (Serial.available()) {
 // read the most recent byte (which will be from 0 to 255)
  byte ch = Serial.read ();
  val[index++] = ch;
  val [index] = '\0';

  Serial.println(val);
  int a = atoi(val);
  Serial.println(a);

 if (ch <= '0' || ch >= '9') {
   index = 0;
   Serial.println(a);

 }

// analogWrite(ledPin, var);
}
}

PaulS

Quote
what an idiotic naming.. but soit
It is NOT an idiotic name for a function. It describes EXACTLY what the function does and nothing more.

You missed a question:
Quote
What, exactly, are you entering in the send field?
If you are not entering something like "<100, 26, 78>" (without the quotes), then that explains your problem.
The art of getting good answers lies in asking good questions.

spikey1973

well should've said imho. it is probably obvious if you already understand the function of the function.
but if not i find it not so clear. everything is written out except the first keyword.
but anyway i should not comment i'm far far far away from writing something even close to such a code.

anyway. in the mean time i've found out something. indeed i never tried since the baud issue.
so the reading function works perfectly at the moment.
also the parsing stuff. so the Red / Green / BlueFromPC does take the correct value.

but the leds do not change color so something is wrong with the led controle. which is strange as i use the same method in my program to change the color of the leds. in which it seamed to work (but only) via CRGB for sure so maybe there is something wrong with it anyway. but as usual i copied it from a functioning program.

line of which i'm talking is.. #118

Code: [Select]
//led calibration manually

// from Example 5 - Receive with start- and end-markers combined with parsing

#include <FastLED.h>

#define    NUM_LEDS             1      // P9813 module: string is one l
#define    CLOCK_PIN            4      // Clock and data pins for LED data
#define    DATA_PIN             5
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
int RedFromPC = 0;
int GreenFromPC = 0;
int BlueFromPC = 0;

boolean newData = false;

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

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(receivedChars,","); // get the first part - the string
    RedFromPC = atoi(strtokIndx);           // convert this part to an integer
   
    strtokIndx = strtok(NULL, ",");         // this continues where the previous call left off
    GreenFromPC = atoi(strtokIndx);         // convert this part to an integer
     
    strtokIndx = strtok(NULL, ",");         // this continues where the previous call left off
    BlueFromPC = atoi(strtokIndx);          // convert this part to an integer
}

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

void showParsedData() {
    Serial.print("Red ");
    Serial.println(RedFromPC);
    Serial.print("Green ");
    Serial.println(GreenFromPC);
    Serial.print("Blue ");
    Serial.println(BlueFromPC);
}

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

void recvWithStartEndMarkers() {
    Serial.println("start rec with start end markers");
    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) {
          Serial.println("recvInProgress = true");
            if (rc != endMarker) {
              Serial.println("rc != endMarker");
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
              Serial.println("else");
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
          Serial.println("rc== endMarker");
            recvInProgress = true;
        }
    }
}

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

void setup() {
    LEDS.addLeds<P9813, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS);  // initialize LED control object
    Serial.begin(9600);
    Serial.println("This expects 3 pieces of data - red, green and blue");
    Serial.println("value between 0 and 25 ");
    Serial.println();
   
    LEDS.addLeds<P9813, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS);  // initialize LED control object
    Serial.println("p9813 ge-init");
}

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

void loop() {
  Serial.println("loop started");
    recvWithStartEndMarkers();
    if (newData == true) {
      Serial.println("new data == 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();
        Serial.println("parsed data showed");
        newData = false;
        Serial.println("new data is false");
    }
    leds[0].setRGB(RedFromPC,GreenFromPC,BlueFromPC);
    Serial.println("init leds ");
    delay(1000);
}

spikey1973

i tried to fix.. the led issue but it all seems to be right.. well according to FastLED.
But it is still not changing the leds color though..

also double checked the connection.. but that is also oke.

so a bit baffled here..


PaulS

Quote
But it is still not changing the leds color though..
Do the examples that come with the FastLED library work?

Those examples call the show() and delay() methods of the FastLED instances to actually make the change appear.
The art of getting good answers lies in asking good questions.

spikey1973

OMG, sorry this i definately should've seen this myself. !!

working like a charm. thank you very very very much.

Kind regards..


ps: just to help people who want to do a similar thing.. this is the working code.

Code: [Select]
//led calibration manually

// from Example 5 - Receive with start- and end-markers combined with parsing

#include <FastLED.h>

#define    NUM_LEDS             1      // P9813 module: string is one l
#define    CLOCK_PIN            4      // Clock and data pins for LED data
#define    DATA_PIN             5
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
int RedFromPC = 0;
int GreenFromPC = 0;
int BlueFromPC = 0;

boolean newData = false;

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

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(receivedChars,","); // get the first part - the string
    RedFromPC = atoi(strtokIndx);           // convert this part to an integer
   
    strtokIndx = strtok(NULL, ",");         // this continues where the previous call left off
    GreenFromPC = atoi(strtokIndx);         // convert this part to an integer
     
    strtokIndx = strtok(NULL, ",");         // this continues where the previous call left off
    BlueFromPC = atoi(strtokIndx);          // convert this part to an integer
}

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

void showParsedData() {
    Serial.print("Red ");
    Serial.println(RedFromPC);
    Serial.print("Green ");
    Serial.println(GreenFromPC);
    Serial.print("Blue ");
    Serial.println(BlueFromPC);
}

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

void recvWithStartEndMarkers() {
    Serial.println("start rec with start end markers");
    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) {
          Serial.println("recvInProgress = true");
            if (rc != endMarker) {
              Serial.println("rc != endMarker");
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
              Serial.println("else");
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
          Serial.println("rc== endMarker");
            recvInProgress = true;
        }
    }
}

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

void setup() {
    LEDS.addLeds<P9813, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS);  // initialize LED control object
    Serial.begin(9600);
    Serial.println("This expects 3 pieces of data - red, green and blue");
    Serial.println("value between 0 and 25 ");
    Serial.println();
   
    FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS);  // initialize LED control object
    Serial.println("p9813 ge-init");
}

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

void loop() {
  Serial.println("loop started");
    recvWithStartEndMarkers();
    if (newData == true) {
      Serial.println("new data == 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();
        Serial.println("parsed data showed");
        newData = false;
        Serial.println("new data is false");
    }
    leds[0].setRGB( RedFromPC, GreenFromPC, BlueFromPC);
    FastLED.show();
    delay(30);
    Serial.println("init leds ");
    delay(1000);
}

Go Up