LED Serial Input Question

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?

Here's my code

#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();}

}
  }
}

if u need 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'); }
    
}
[code]

Always format your code with
Do these look the same?

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

. . .

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

Suggest you get into the habit of placing { and } on lines by themselves!

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

etc
etc

Why not simply

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

Can also:

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

    FastLED.clear();   //  <-----<<<<    USE clear
    
    //1
            . . .

UKHeliBob:

   //1

if (incomingByte == '1')
    {
      leds[0] = CRGB(255, 0, 0);
      FastLED.show();
    }





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





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



etc
etc

Why not simply


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

Thanks for the reply! im a newbie though. my code looks like this now, I think I messed the brackets up.

#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();
   

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

doesn't do anything haha, some help? and where do I put FastLED.Clear in the loop?

#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);

  FastLED.clear();
  FastLED.show();
}

void loop()
{

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

    if (incomingByte > '0' && incomingByte < '7')
    {
      FastLED.clear();   //  <-----<<<<    USE clear
    }


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

    FastLED.show();
  }
}

Using switch/case

#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);

  FastLED.clear();
  FastLED.show();
  
} //END of setup()

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

    if (incomingByte > '0' && incomingByte < '7')
    {
      FastLED.clear();   //  <-----<<<<    USE clear
    }

    switch (incomingByte)
    {
      case '1':
          leds[0] = CRGB(255, 0, 0);
          break;

      case '2':
          leds[1] = CRGB(255, 0, 0);
          break;

      case '3':
          leds[2] = CRGB(255, 0, 0);
          break;

      case '4':
          leds[3] = CRGB(255, 0, 0);
          break;

      case '5':
          leds[4] = CRGB(255, 0, 0);
          break;

      case '6':
        leds[5] = CRGB(255, 0, 0);
        break;
        
    } //END of switch/case
    
    FastLED.show();
    
  }
  
} //END of loop()

larryd:

#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);

FastLED.clear();
  FastLED.show();
}

void loop()
{

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

if (incomingByte > '0' && incomingByte < '7')
    {
      FastLED.clear();  //  <-----<<<<    USE clear
    }

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

FastLED.show();
  }
}

Thank you! I have one last question. I'm working with.a 6x6 GRID. Your code works perfectly on the first 6 leds but when I use it for more it bugs out. Do you think I reached the max capability of the Arduino's voltage? it's 36 LEDS. or is it maybe my code?

#include <FastLED.h>

#define LED_PIN     13
#define NUM_LEDS    36

CRGB leds[NUM_LEDS];

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

 FastLED.clear();
 FastLED.show();
}

void loop()
{

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

   if (incomingByte > '0' && incomingByte < '7')
   {
     FastLED.clear();   //  <-----<<<<    USE clear
   }


   if (incomingByte == '1')
   {
     leds[0] = CRGB(255, 0, 0);
     leds[6] = CRGB(255, 0, 0);
     leds[12] = CRGB(255, 0, 0);
     leds[18] = CRGB(255, 0, 0);
     leds[24] = CRGB(255, 0, 0);
     leds[36] = CRGB(255, 0, 0);
   }
   else if (incomingByte == '2')
   {
     leds[1] = CRGB(255, 0, 0);
     leds[7] = CRGB(255, 0, 0);
     leds[13] = CRGB(255, 0, 0);
     leds[19] = CRGB(255, 0, 0);
     leds[25] = CRGB(255, 0, 0);
     leds[37] = CRGB(255, 0, 0);
   }
   else if (incomingByte == '3')
   {
     leds[2] = CRGB(255, 0, 0);
     leds[8] = CRGB(255, 0, 0);
     leds[14] = CRGB(255, 0, 0);
     leds[20] = CRGB(255, 0, 0);
     leds[26] = CRGB(255, 0, 0);
     leds[38] = CRGB(255, 0, 0);
    
   }
   else if (incomingByte == '4')
   {
     leds[3] = CRGB(255, 0, 0);
     leds[9] = CRGB(255, 0, 0);
     leds[15] = CRGB(255, 0, 0);
     leds[21] = CRGB(255, 0, 0);
     leds[27] = CRGB(255, 0, 0);
     leds[39] = CRGB(255, 0, 0);
   }
   else if (incomingByte == '5')
   {
     leds[4] = CRGB(255, 0, 0);
     leds[10] = CRGB(255, 0, 0);
     leds[16] = CRGB(255, 0, 0);
     leds[22] = CRGB(255, 0, 0);
     leds[28] = CRGB(255, 0, 0);
     leds[40] = CRGB(255, 0, 0);
   }
   else if (incomingByte == '6')
   {
     leds[5] = CRGB(255, 0, 0);
     leds[11] = CRGB(255, 0, 0);
     leds[17] = CRGB(255, 0, 0);
     leds[23] = CRGB(255, 0, 0);
     leds[29] = CRGB(255, 0, 0);
     leds[41] = CRGB(255, 0, 0);
   }

   FastLED.show();
 }
}
[code]

Here's the code so far. the most right bar doesn't work?

EDIT: MY BAD I DIDN'T COUNT PROPERLY!!!! it works. thanks everyone

With six LEDs on at one time plus some current for the LEDs that are off, you might be reaching the maxim for the Arduino.

Try an external power +5v supply (use 470R in series with 13, a 500uF cap on the o/p of the power supply and common GND to the Arduino).

The code in post #6 uses 'switch/case' which is better than using 'else if'.

larryd:
The code in post #6 uses 'switch/case' which is better than using 'else if'.

Is it? I wonder why?

Switch case creates a jump table which can result in faster code.

However, imo it looks cleaner too :wink:

The whole thing is a mess either way.

The way to do it is to convert the incoming character into a number

byte ledNo = incomingByte - ‘1’;
     leds[ledNo] = CRGB(255, 0, 0);
     leds[6 + ledNo] = CRGB(255, 0, 0);
     leds[12 + ledNo] = CRGB(255, 0, 0);
     leds[18 + ledNo] = CRGB(255, 0, 0);
     leds[24 + ledNo] = CRGB(255, 0, 0);
     leds[36 + ledNo] = CRGB(255, 0, 0);

No ifs and no case statement.

Mike's whiskers are showing.