Go Down

Topic: FastLed Won't show colors after receiving correct input from C# application (Read 638 times) previous topic - next topic

Dandekiller

Hello everyone!

Today I got an error, whilst programming a ledstrip that uses FastLed.H.

First I used a NeoPixel Ledstrip from Adafruit (144 Leds), and then the code worked correctly, but when I added Fastleds (I got a few more led strips of those than the NeoPixel from Adafruit), and the leds won't change the correct ones into the appropriate color.


Do I write wrong from the ledWrite Method towards the SetSingleFastLed method? Or is the error in the variable type?

Code:

 
Code: [Select]




#include <Adafruit_NeoPixel.h>
#include <FastLED.h>


#define NumberOfLedsOnFastLedStrip 60
#define DataPinForFastLedLedstrip 5

CRGB FastLeds[NumberOfLedsOnFastLedStrip];

#define TotalNumberOfLeds (144)
#define InputPin (9)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(TotalNumberOfLeds, InputPin, NEO_RGB + NEO_KHZ800);



byte bytesReceived[5];
int coupeNr;
int currentCapacity;
int lengthCoupe;
int startingPoint;



void UpdateStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
  // If NeoPixel Is Defined
  strip.show();
#endif

}

void SetSingleNeoPixel(int NeoPixel, byte G, byte R, byte B) {
#ifdef ADAFRUIT_NEOPIXEL_H
  // If NeoPixel Is Defined
  strip.setPixelColor(NeoPixel, strip.Color(G, R, B));

  UpdateStrip();
#endif
}


void SetSingleFastled(int FastLed, uint8_t R, uint8_t G, uint8_t B)
{

  //FastLeds[FastLed].setRGB(R,G,B);
  FastLeds[FastLed].r = R;
  FastLeds[FastLed].g = G;
  FastLeds[FastLed].b = B;
FastLED.show();

}


void SetAllNeoPixels(byte G, byte R, byte B) {
  for (int i = 0; i < TotalNumberOfLeds; i++ ) {
    SetSingleNeoPixel(i, G, R, B);
  }
  UpdateStrip();
}

void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<NEOPIXEL, DataPinForFastLedLedstrip>(FastLeds, NumberOfLedsOnFastLedStrip);

  strip.begin();
  strip.setBrightness(10);
  SetAllNeoPixels(255, 255, 255);
  strip.show();
 
  Serial.begin(9600);
  fill_solid(FastLeds, 60, CRGB(255, 0, 0));
  FastLED.show();
  delay(2000);
  fill_solid(FastLeds, 60, CRGB(0, 0, 0));
  FastLED.show();

}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    while (Serial.available() > 0) {
      Serial.readBytes(bytesReceived, 5);//lees de bytes van serial en sla ze op in een array
    }
    delay(2);
    coupeNr = bytesReceived[0];
    currentCapacity = bytesReceived[1];
    lengthCoupe = bytesReceived[2];
    byte startingPointHigh = bytesReceived[3];
    byte startingPointLow = bytesReceived[4];
    startingPoint = (startingPointHigh << 8) | startingPointLow; // Because starting point can be bigger than 255, we will need to apply Bit Manipulation
    ledWrite(coupeNr, currentCapacity, lengthCoupe, startingPoint);
  }
}


void ledWrite(int coupeInput, int peopleInput, int coupeLength, int startingPoint) {
  int R;
  int G;
  int B;
  if (peopleInput < 100) {
    R = 2.5 * peopleInput;
    G = 255 - (2.5 * peopleInput);
    B = 0;
  }
  if (peopleInput >= 100) {
    R = 255;
    G = 0;
    B = 0;
  }
uint8_t fastR = R;
uint8_t fastG = G;
uint8_t fastB = B;

  for (int i = (coupeLength * coupeInput) + startingPoint; i < coupeLength + (coupeInput * coupeLength) + startingPoint; i++) { //berekening om de LEDs op de juiste plek aan te zetten
   // SetSingleNeoPixel(i, G, R, B);
 
    SetSingleFastled(i, R, G, B);
  }
   
}



I already looked around for the reason, and tried a few CRGB functions out, but they did not fix the problem. I even made Unsigned Ints before the for loop, but it still did not work.

The ledstrip works though when I upload it to the Arduino (with the Fill_Solid Function).

So my question is: Can someone give me a few pointers on where the error lies, or show me what I am doing wrong?


Thank you all in advance, and I wish you all a nice day.



Grumpy_Mike

Quote
Can someone give me a few pointers on where the error lies, or show me what I am doing wrong?
You can not use both the fastLED and the Adafruit library in the same sketch.

Dandekiller

You can not use both the fastLED and the Adafruit library in the same sketch.
May I ask why?

Is it because Fastled accesses and assigns the same resources Adafruit does?

also thanks for the answer, the code works now! :D

Grumpy_Mike

Quote
May I ask why?
I think it is because of overloading, that is using functions with the same name with the last to be defined overriding the functions of the earlier ones.

Dandekiller

I think it is because of overloading, that is using functions with the same name with the last to be defined overriding the functions of the earlier ones.
I see.

Well the code works except for the last method (called LedWrite)

Do you know what I am doing wrong there? I am using long int since normal int's do not equal the same amount of bytes.

Is it the formula that is used to calculate the I? or am I calling upon the CRGB values wrong? The ledstrip does work when called in the Setup(), but not in my own method.


Code: [Select]

#include <FastLED.h>


#define NumberOfLedsOnFastLedStrip 60
#define DataPinForFastLedLedstrip 5

CRGB FastLeds[NumberOfLedsOnFastLedStrip];


byte bytesReceived[5];
int coupeNr;
int currentCapacity;
int lengthCoupe;
int startingPoint;

void setup() {
  // put your setup code here, to run once:
  delay(2000);
      FastLED.addLeds<NEOPIXEL, DataPinForFastLedLedstrip>(FastLeds, NumberOfLedsOnFastLedStrip);
      FastLED.setBrightness(255);
  Serial.begin(9600);
 /*
  fill_solid(FastLeds, 60, CRGB(0, 0, 255));
  FastLED.show();
  delay(2000);
  fill_solid(FastLeds, 60, CRGB(0, 0, 0));
  FastLED.show();
  */

}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    while (Serial.available() > 0) {
      Serial.readBytes(bytesReceived, 5);//lees de bytes van serial en sla ze op in een array
    }
    delay(2);
    coupeNr = bytesReceived[0];
    currentCapacity = bytesReceived[1];
    lengthCoupe = bytesReceived[2];
    byte startingPointHigh = bytesReceived[3];
    byte startingPointLow = bytesReceived[4];
    startingPoint = (startingPointHigh << 8) | startingPointLow; //omdat startingpoint groter dan 255 kan zijn moet bit manipulation worden toegepast
    ledWrite(coupeNr, currentCapacity, lengthCoupe, startingPoint);
  }
}

void ledWrite(int coupeInput, int peopleInput, int coupeLength, int startingPoint) {
long  int R;
long  int G;
long  int B;
  if (peopleInput < 100) {
    R = 2.5 * peopleInput;
    G = 255 - (2.5 * peopleInput);
    B = 0;
  }
  if (peopleInput >= 100) {
    R = 255;
    G = 0;
    B = 0;
  }
  for (int i = (coupeLength * coupeInput) + startingPoint; i < coupeLength + (coupeInput * coupeLength) + startingPoint; i++) { //berekening om de LEDs op de juiste plek aan te zetten

 FastLeds[i].setRGB(R,G,B);
   FastLED.show();
  
  }

}


Grumpy_Mike

I would cast the variables to float when doing the calculations and cast them back to long in before assigning them to R G and B.

Dandekiller

I would cast the variables to float when doing the calculations and cast them back to long in before assigning them to R G and B.
Something like this:
  long int fastR = (long int)R;
  long int fastG = (long int)G;
  long int fastB = (long int)B;
?
Edit:

Did the long int cast, and still the ledstrip won't light up.

Or do you mean int R then cast toward a float before doing the if statements and after those and before the for loop I do the long int cast.

Thanks again for your help

Grumpy_Mike

I said cast to float
Code: [Select]
R = int()( 2.5 * float() peopleInput);

Dandekiller

I said cast to float
Code: [Select]
R = int()( 2.5 * float() peopleInput);

so like this then:
Code: [Select]

  long int R;
  long int G;
  long int B;
  if (peopleInput < 100) {
    R = (long int)( 2.5 *(float) peopleInput);
    G = (long int) ( 255 - (2.5 * (float) peopleInput));
    B = 0;
  }
  if (peopleInput >= 100) {
    R = 255;
    G = 0;
    B = 0;
  }

 

Seeing that you have to put the cast variable into the parenthesis, instead of putting them in front of them.

Would I also have to make the cast in the if >= 100? Would not make a lot of sense because the variable there is put to max of byte value and no calculation is needed.

Grumpy_Mike

Quote
Would I also have to make the cast in the if >= 100?
No you are not using any mixed data type arithmetic.

Go Up