1

I want to know how I can use case statement for each array.

Below is my code:

int incomingByte = 0;

void setup()
{
  Serial.begin(115200);
  for (int thisPin = 2; thisPin < 6; thisPin++)
  {
    pinMode(thisPin, OUTPUT);
  }
}

void loop()
{
  int button1[] = {0xFF, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05};
  int button2[] = {0xFF, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06};
  int button3[] = {0xFF, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08};
  int button4[] = {0xFF, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C};

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

  switch (incomingByte)
  {
    case button1:
      digitalWrite(2, HIGH);
      break;
    case button2:
      digitalWrite(3, HIGH);
      break;
    case button3:
      digitalWrite(4, HIGH);
      break;
    case button4:
      digitalWrite(5, HIGH);
      break;
    default:
      for (int thisPin = 2; thisPin < 6; thisPin++)
      {
        digitalWrite(thisPin, LOW);
      }
  }
} 

2 Answers 2

0

Not checked/compiled, just to give an idea.

/* One button message is 12 bytes, it is assumed that all received messages are 12 bytes. If note, you have to check later differently. */
#define MSG_SIZE 11   
byte incomingBytes[MSG_SIZE]; /* Storage for 11 bytes */
byte index = 0; /* Current byte to set within incomingBytes */

void setup()
{
  Serial.begin(115200);
  for (int thisPin = 2; thisPin < 6; thisPin++)
  {
    pinMode(thisPin, OUTPUT);
  }
}

void loop()
{
  if (Serial.available() > 0)
  {
    /* Store the next incoming byte. Assume 0xFF is the start of a message. */
    byte incoming_byte = Serial.read();
    if (incoming_byte == 0xFF)
    {
      index = 0;
    }

    incomingBytes[index] = incoming_byte;
    index++;
    if (index >= MSG_SIZE)
    {
       check_message();
       index = 0;
    }
  }
}

/* Checks which button is pressed and performs an action based on the 
pressed button. */
void check_message()
{
  case check_button_pressed())
  {
    case 1:
      digitalWrite(2, HIGH);
      break;
    case 2:
      digitalWrite(3, HIGH);
      break;
    case 3:
      digitalWrite(4, HIGH);
      break;
    case 4:
      digitalWrite(5, HIGH);
      break;
    default:
      for (int thisPin = 2; thisPin < 6; thisPin++)
      {
        digitalWrite(thisPin, LOW);
      }
  }
} 

/* Checks which button is pressed by comparing 11 incoming bytes to the
four buttons to be checked. Returns -1 for an illegal message/unsupported
button and 1, 2, 3 or 4 for the pressed button. */
int check_button_pressed()
{
  int button_pressed = -1;
  if ((incomingBytes[0] == 0xFF) &&
      (incomingBytes[1] == 0x04) &&
      (incomingBytes[3] == 0x00) &&
      (incomingBytes[4] == 0x00) &&
      (incomingBytes[5] == 0x00) &&
      (incomingBytes[6] == 0x00) &&
      (incomingBytes[7] == 0x00) &&
      (incomingBytes[8] == 0x00) &&
      (incomingBytes[9] == 0x00))
  {
    if ((incomingBytes[2] == 0x01) && (incomingBytes[10] == 0x05))
    {
      button_pressed = 1;
    }
    else if ((incomingBytes[2] == 0x02) && (incomingBytes[10] == 0x06))
    {
      button_pressed = 2;
    }
    else if ((incomingBytes[2] == 0x04) && (incomingBytes[10] == 0x08))
    {
      button_pressed = 3;
    }
    else if ((incomingBytes[2] == 0x08) && (incomingBytes[10] == 0x0C))
    {
      button_pressed = 4;
    }
  }
  return button_pressed;
}
4
  • I'd add something to explicitly re-sync the stream when 0xFF is received, to handle variable length messages and cases where the serial connection is established in mid-message, but otherwise this is a good approach. Jan 14, 2018 at 14:48
  • True, this is in case all messages are same length. But I don't know what the start of a message is, probably 0xFF. If so, if character is encounter, set Index to 0 Jan 14, 2018 at 15:34
  • @RussellBorogove I adapted the code for it. Jan 14, 2018 at 15:37
  • yw ... if it is helpful, you can upvote the helpful answers, and select one which you consider best to answer your question (and press accept). Jan 15, 2018 at 19:28
0

case doesn't work like that; it can only switch on a single integer. Are you trying to pattern-match those sequences of bytes? You'll have to use some sort of state machine instead, keeping track of what bytes you've received and where in the expected pattern you are.

2
  • Can you give me some hints? I am a newbie and i don't know much about Arduino. Thanks
    – Ali khan
    Jan 13, 2018 at 21:49
  • As mentioned it won't work. But also note you read a single byte, but your arrays have eleven items in them. You should probably read 11 bytes and use a comparison routine. (memcmp, etc.)
    – user29840
    Jan 14, 2018 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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