1

Hey guys I have a LED strip with 6 LEDS (I control these through Kinect & processing. It sends 1,2,3,4,5,6 to Arduino. This seems to work fine.)

In Arduino LED0,LED1,LED2,LED3,LED4 work perfectly but the last one don't work. it doesn't light up at all.

Anyone know what's going on?


#include <FastLED.h>

#define LED_PIN     13
#define NUM_LEDS    6

CRGB leds[NUM_LEDS];

void setup() {

  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  Serial.begin(9600);



}

void loop() {

  if (Serial.available() > 0) {
int incomingByte = Serial.read();

  //1
if(incomingByte=='1'){
  leds[0] = CRGB(255, 0, 0);
  FastLED.show();}
if(incomingByte=='2'){
  leds[0] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='3'){
  leds[0] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='4'){
  leds[0] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='5'){
  leds[0] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='6'){
  leds[0] = CRGB(0, 0, 0);
  FastLED.show();}



 //2
 if(incomingByte=='1'){
  leds[1] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='2'){
  leds[1] = CRGB(255, 0, 0);
  FastLED.show();}
if(incomingByte=='3'){
  leds[1] = CRGB(0, 0, 0);
  FastLED.show();}
  if(incomingByte=='4'){
  leds[1] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='5'){
  leds[1] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='6'){
  leds[1] = CRGB(0, 0, 0);
  FastLED.show();}



  //3
   if(incomingByte=='1'){
  leds[2] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='2'){
  leds[2] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='3'){
  leds[2] = CRGB(255, 0, 0);
  FastLED.show();}
   if(incomingByte=='4'){
  leds[2] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='5'){
  leds[2] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='6'){
  leds[2] = CRGB(0, 0, 0);
  FastLED.show();}


  //4
   if(incomingByte=='1'){
  leds[3] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='2'){
  leds[3] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='3'){
  leds[3] = CRGB(0, 0, 0);
  FastLED.show();}
    if(incomingByte=='4'){
  leds[3] = CRGB(255, 0, 0);
  FastLED.show();}
if(incomingByte=='5'){
  leds[3] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='6'){
  leds[3] = CRGB(0, 0, 0);
  FastLED.show();}



  //5
   if(incomingByte=='1'){
  leds[4] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='2'){
  leds[4] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='3'){
  leds[4] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='4'){
  leds[4] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='5'){
  leds[4] = CRGB(255, 0, 0);
  FastLED.show();}
if(incomingByte=='6'){
  leds[4] = CRGB(0, 0, 0);
  FastLED.show();}



  //6
   if(incomingByte=='1'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();
if(incomingByte=='2'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='3'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='4'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='5'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='6'){
  leds[5] = CRGB(255, 0, 0);
  FastLED.show();}

}
  }
}

here's the processing code:

float x = 100;
float y = 50;
float w = 150;
float h = 80;

import org.openkinect.freenect.*;
import org.openkinect.processing.*;
import processing.serial.*;


// The kinect stuff is happening in another class
KinectTracker tracker;
Kinect kinect;
Serial myPort;

void setup() {
  size(512, 424);
  kinect = new Kinect(this);
  tracker = new KinectTracker();
  String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw() {

  tracker.track();
  tracker.display();

  PVector v1 = tracker.getPos();
  fill(50, 100, 250, 200);
  noStroke();
  ellipse(v1.x, v1.y, 50, 50);




   //1
    if (v1.x > 0 && v1.x < 50 ){
    println('1');
    //rect(0,200,200,200);
    myPort.write('1'); }
    //println('0'); }

    //2
  if (v1.x > 50 && v1.x < 100){
    println('2');
    myPort.write('2'); }   
 //3
   if (v1.x > 100 && v1.x < 150 ){
    println('3');
    myPort.write('3'); }

    //4
   if (v1.x > 150 && v1.x < 200 ){
    println('4');
   myPort.write('4'); }

    //5
   if (v1.x > 200 && v1.x < 250 ){
    println('5');
    myPort.write('5'); }

    //6
   if (v1.x > 250 && v1.x < 300 ){
    println('6');
    myPort.write('6'); }

}

1
  • Can you make the LED light up manually? That is, write simpler code that just turns that LED on and nothing else.
    – Majenko
    May 4 '19 at 21:30
0

Your problem lies in the position of the closing bracket of the first if statement for LED 6:

//6
if(incomingByte=='1'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();         // here the closing bracket is missing
if(incomingByte=='2'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='3'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='4'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='5'){
  leds[5] = CRGB(0, 0, 0);
  FastLED.show();}
if(incomingByte=='6'){
  leds[5] = CRGB(255, 0, 0);
  FastLED.show();}

} // this is the missing closing bracket
  }
}

This means, that this whole block will only be executed, when incomingByte has the value '1'. This also means, that the last if statement will never be executed. Correct the position of the marked closing bracket and it should work.


Besides that, your code is a very complicated way of doing this. Why not simply switching the incomingByte? Also, to write less code and making it more understandable, use a function to turn all LEDs to off ("resetting" them):

void reset_LEDs(){
    for(int i=0;i<NUM_LEDS;i++){
        leds[i] = 0;
    }
}

void loop(){
if (Serial.available() > 0) {
    int incomingByte = Serial.read();

    switch(incomingByte){
        case '1':
            reset_LEDs();
            leds[0] = CRGB(255, 0, 0);
            break;
        case '2':
            reset_LEDs();
            leds[1] = CRGB(255, 0, 0);
            break;
        ...
    }
    FastLED.show();
}
}

When you stay with such easy conditions, you can even optimize more:

int incomingByte = Serial.read() - '0'; //Convert ASCII character value to decimal int value, by subtracting the ASCII value of zero

if(incomingByte > 0 && incomingByte <= NUM_LEDS){ //Test, if the incomingByte lies between 1 and NUM_LEDS including
    reset_LEDs();
    leds[incomingByte - 1] = CRGB(255, 0, 0); // -1 because array counting starts with 0, we start counting with 1
    FastLED.show();
}

For both versions, you save time, because you we only call `FastLED.show()´ once, when a change happens, not multiple times.

2
  • Thank you for this!
    – Juriaan
    May 14 '19 at 15:00
  • If this answers your question, you can accept it as correct. Thanks
    – chrisl
    May 14 '19 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.