Go Down

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

spikey1973

hey guys,

i thinki'm getting really blond or really tired or reallly stupid.

but i'm trying to make a basic program to manually control the red, green, and blue values of a 5050 ledstrip controlled by a p9813 chip. this is just to calibrated the seperate color intensities so it just has to work.

so i don't need something to check if i will type in values different than between 0 and 255 and stuff.

but i can't seem to get any reaction after uploading neither from the led and or serial monitor (just stays blank)

where am i going wrong here?


Code: [Select]
//led calibration manually

#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

int       r ;                                       // intensiteit rode led
int       g ;                                       // intensiteit groen led
int       b ;                                       // intensiteit blauw led

CRGB       leds[NUM_LEDS];

//ask for input

void setup (){
LEDS.addLeds<P9813, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS);  // initialize LED control object
Serial.begin(9600);
while (! Serial); // Wait until Serial is ready
Serial.println("Enter LED Number 0 to 255 for Red, blue and green\n");
}

void loop (){

if (Serial.available()){
    Serial.println("Enter Red\n");
    r = Serial.read();
    Serial.println("Enter Green\n");
    g = Serial.read();
    Serial.println("Enter Blue\n");
    b = Serial.read();

    leds[0].setRGB(r, g, b);
    }
}


PaulS

Quote
i thinki'm getting really blond or really tired or reallly stupid.
Do we get to choose?

Code: [Select]
while (! Serial); // Wait until Serial is ready
Which Arduino do you have?

Code: [Select]
    Serial.println("Enter Red\n");
    r = Serial.read();

When you type "127" in the Serial Monitor application, this will read the '1'. Setting the red value to '1' will NOT produce the results that you expect.

A very useful site:
http://forum.arduino.cc/index.php?topic=396450.0
The art of getting good answers lies in asking good questions.

spikey1973

yes you can choose! definately,... but be aware it can also me a multiple of the optionsso you have 6 choices there.

and thanks.. i will definately look into that for sure.

but for now that doens't seem to be the major problem as i can't even type in the 1.

it just doesn't do anything

i'm fiddling with a nano ATmega 328


PaulS

Quote
i'm fiddling with a nano ATmega 328
Then, you do not need that while statement. Get rid of it.
The art of getting good answers lies in asking good questions.

spikey1973

oke this is going from bad to wurse..

with this code i get a compiling error.. but no message. just an (orange) bar with Error Complining.

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(NULL, ","); // this continues where the previous call left off
    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() {
    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 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
}

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

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;
    }
    leds[0].setRGB(RedFromPC, GreenFromPC, BlueFromPC);
}

PaulS

When I try to compile your code, I get:
Quote
sketch_sep12a:91: error: missing terminating " character
and the line:
Code: [Select]
   Serial.println("This expects 3 pieces of data - red, green and blue");");
is highlighted.

The art of getting good answers lies in asking good questions.

spikey1973

hmmm no highlights here..

but none the less i should've seen that though..

so solved that part.. uploaded.. but still no funcionality..

i'm going shopping...


PaulS

Quote
so solved that part.. uploaded.. but still no funcionality..
What is connected to the Arduino? At this point, the ONLY thing that should be connected is the USB cable. What we are trying to do is determine why nothing appears in the Serial Monitor application, so we need to eliminate any possible hardware conflicts.
The art of getting good answers lies in asking good questions.

spikey1973

just had the p9813 controller connected. as it should be expecting that.

now i took everything of.. still no change.
:( anyway.. your help is very much appreciated

kind regards

Matthieu

spikey1973

well it seems to be more than just a code problem..


i copy pasted this serial print tutorial in a new blank field and well lets's say thing did not go right.


a slider bar appeared at the bottom with the slider completely to the right and rather quickly getting smaller and smaller.. (so the field increasing" but nothing was written.

completely blank..

also on the board a led is blinking.. so it is doing something..

i wanted to try a different nano board i have around. but can't find it at the moment

maybe a different usb cable will help


i'll try another anano i have around so if that will be different.

PaulS

Does the baud rate in the sketch match that of the Serial Monitor app? I'm guessing that they do not match.
The art of getting good answers lies in asking good questions.

spikey1973

i actually beat you to it this time. actually figured it out myself.
the little break did wonders apparently. So indeed it did not match.. after matching them the sketch works fine..

now i can go back with my sketch..

thanks!!

spikey1973

 i filled the code with serial prints and it seems that the while loop.

i doesn't read in data and goes over the loop

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(NULL, ","); // this continues where the previous call left off
    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);
}


PaulS

Code: [Select]
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
There is no previous call. This call will NOT work.

Quote
i filled the code with serial prints and it seems that the while loop.

i doesn't read in data and goes over the loop
What, exactly, are you entering in the send field? What serial output are you seeing?
The art of getting good answers lies in asking good questions.

spikey1973

i understand.. just copy pasted it from the existing code, where three types (string, int, float) were read. as i just need three times a int i did this. clearly wrongly.

i can't enter anything. it goes through the setup and loop and a small by step into recvWithStartEndMarkers (what an idiotic naming.. but soit) where it doesn't do anything.

i tried to copy pate the serial print but to no avail.

so all i have is a printscreen which i can't upload as i have no url of it so added it here below. .




Go Up