Categorizing serials

Hey guys I have a led grid. it’s 8 sides that are addressable with the bytes 1 through 8. (a Kinect is sending these bytes through processing) I have also creates three buttons in processing that change the colour of the leds. (R,G,B).

I’m stuck in the code, my code now when receiving the bytes: R, G or B changes all leds to that colour. instead of remembering the colour and only lighting up the sides determined by the 1,2,3,4,5,6,7,8 values.

I need it to remember the chosen R,G,B colour until another colour is chosen and only use the bytes 1,2,3,4,5,6,7,8 to select what side of the grid to light up.

Here is my code so far:

#include <FastLED.h>

#define LED_PIN     6
#define NUM_LEDS    360

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 (Serial.available() > 1)
    {
      firstByte = Serial.read();
      secondByte = Serial.read();
    }

    if (incomingByte == 'R')
      for (int i = 0; i < 360; i++) {
        leds[i] = CRGB(255, 0, 0);
      }
    if (incomingByte == 'G')
      for (int i = 0; i < 360; i++) {
        leds[i] = CRGB(0, 255, 0);
      }
    if (incomingByte == 'B')
      for (int i = 0; i < 360; i++) {
        leds[i] = CRGB(0, 0, 255);
      }

    if (incomingByte == '1')
      for (int i = 0; i <= 44; i++) {
        leds[i] = CRGB(255, 0, 0);
      }
    else if (incomingByte == '2')
      for (int i = 44; i <= 89; i++) {
        leds[i] = CRGB(255, 0, 0);
      }
    else if (incomingByte == '3')
      for (int i = 90; i <= 134; i++) {
        leds[i] = CRGB(255, 0, 0);
      }

    else if (incomingByte == '4')
      for (int i = 135; i <= 179; i++) {
        leds[i] = CRGB(255, 0, 0);
      }
    else if (incomingByte == '5')
      for (int i = 180; i <= 224; i++) {
        leds[i] = CRGB(255, 0, 0);
      }
    else if (incomingByte == '6')
      for (int i = 225; i <= 269; i++) {
        leds[i] = CRGB(255, 0, 0);
      }
    else if (incomingByte == '7')
      for (int i = 270; i <= 314; i++) {
        leds[i] = CRGB(255, 0, 0);
      }
    else if (incomingByte == '8')
      for (int i = 315; i <= 359; i++) {
        leds[i] = CRGB(255, 0, 0);

      }

    FastLED.show();
  }
}

Serial can only receive one character at a time. So if you have a 2-character command like "4R" then you need to remember what the first one was at the time you are processing the second.

Then you need to keep the receiver synced up to the transmission. If something happened to the '4' and it is lost, the receiver should ignore the 'R' until it does see something that is correct.

Search for Robin2's Serial Input Basics thread.

Hey Morgan,

i want to send the R,G or B first afterwards the 1,2,3,4,5,6,7,8.

Does that mean it can remember the R,G or B and receive the other serial afterwards?
The receiver will be connected to my pc at all times

Does that mean it can remember the R,G or B and receive the other serial afterwards?

It can remember, but only if you write code to put that into a variable. It is the variable that remembers the value.

Grumpy_Mike:
It can remember, but only if you write code to put that into a variable. It is the variable that remembers the value.

Hey Mike, do you mind showing me an example?

i am reading through this right now. Arduino - VariableDeclaration

should I declare the R, G or B with true or false statements?

How would that look in code?

MorganS:
Serial Input Basics - updated - Introductory Tutorials - Arduino Forum

What example should I look at? I am a bit lost here

should I declare the R, G or B with true or false statements?

No.

if (Serial.available() > 1)
  {
    firstByte = Serial.read();
    secondByte = Serial.read();
}

Where you have previously declaired

byte firstByte, secondByte;

outside of any function.

So you have remembered the first and second byte. And you can then go on and use that in your if statements.

Grumpy_Mike:
No.

if (Serial.available() > 1)

{
    firstByte = Serial.read();
    secondByte = Serial.read();
}



Where you have previously declaired


byte firstByte, secondByte;



outside of any function.

I’m not sure I follow this. what is first byte and second byte? is R the first byte, G the second byte?

I'm not sure I follow this. what is first byte and second byte?

It is the first byte sent to the Arduino and the second byte sent to the Arduino over the serial port.

You asked:-

Does that mean it can remember the R,G or B and receive the other serial afterwards?

This is how it can remember.

Grumpy_Mike:
It is the first byte sent to the Arduino and the second byte sent to the Arduino over the serial port.

You asked:-This is how it can remember.

could you show me how to imply it into the code, I don't think I understand what you mean.

Is first byte the RGB?
second byte the 1,2,3,4,5,6,7,8?

could you show me how to imply it into the code,

I have shown you this already. You clearly are not getting something but I don’t get what you don’t get.

Is first byte the RGB?

No the first byte is a byte, that is it is an 8 bit number or bit pattern expressed as a number. It can take in any value from 0 to 255. It is what ever you send to the Arduino first. So you could send the ASCII for the letter ‘R’ which is a hex value of 0x52, or send any other letter.

second byte the 1,2,3,4,5,6,7,8?

The second byte could be any one of those numbers from 0 to 255.