Skip to main content
deleted 58 characters in body
Source Link
Michel Keijzers
  • 13k
  • 7
  • 42
  • 59
/* 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. IfAssume messages0xFF (nonis buttonthe messagesstart can
of a message. */
 have different lengths, than instead ofbyte this,incoming_byte you= haveSerial.read();
 to check for a
if (incoming_byte == 0xFF)
 unique part of the{
 button message and restart (set index to 0)= based0;
    on a different condition.}
 */ 
    incomingBytes[index] = Serial.read();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;
}
/* 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. If messages (non button messages can
    have different lengths, than instead of this, you have to check for a
    unique part of the button message and restart (set index to 0) based
    on a different condition. */ 
    incomingBytes[index] = Serial.read();
    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;
}
/* 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;
}
Source Link
Michel Keijzers
  • 13k
  • 7
  • 42
  • 59

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. If messages (non button messages can
    have different lengths, than instead of this, you have to check for a
    unique part of the button message and restart (set index to 0) based
    on a different condition. */ 
    incomingBytes[index] = Serial.read();
    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;
}