Problem Reading/Parsing ASCII characters into arduino over UDP

Hi,

I have a Leonardo board with an ethernet shield which i am trying to read 76 different ASCII characters (representing 76 different keys on a keyboard) coming into an arduino over UDP. This is then parsed and used to send out Keyboard.write functions over usb. With my current code i can read all alpha numeric keys (A-Z) sent as ASCII (from Max/MSP). Any other characters (space, number keys etc.) aren’t being read, the serial monitor just outputs ‘list’. I can see they are being read into the packet buffer so i think i have a problem with the code i am using for reading the char’s.

Any thoughts are really appreciated.

Here is the code im running.

/*
Leonardo IN - IP 192.168.1.177 Port 8999  Mac - 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF
 */


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8999;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

//  Serial.begin(9600);
//  while (!Serial) {
 //   }
  Keyboard.begin();

}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);

    
    if (packetBuffer[0] == 'a') {
    //  Serial.println("A was pressed");
      Keyboard.write(97);
    }
    if (packetBuffer[0] == 'b') {
 //     Serial.println("B was pressed");
            Keyboard.write(98);

    }
    if (packetBuffer[0] == 'c') {
  //    Serial.println("C was pressed");
                  Keyboard.write(99);

    }
        if (packetBuffer[0] == 'd') {
  //    Serial.println("D was pressed");
                  Keyboard.write(100);

    }
        if (packetBuffer[0] == 'e') {
  //    Serial.println("E was pressed");
                  Keyboard.write(101);

    }
        if (packetBuffer[0] == 'f') {
 //     Serial.println("F was pressed");
                  Keyboard.write(102);

    }
        if (packetBuffer[0] == 'g') {
  //    Serial.println("G was pressed");
                  Keyboard.write(103);
    }
    //from here the commands do not execute
        if (packetBuffer == "F11") { //f11
//      Serial.println("F11");
                  Keyboard.write(0xCC);
            }
        if (packetBuffer == "F12") { //f12
//      Serial.println("F12");
                  Keyboard.write(0xCD);
            }
        if (packetBuffer[0] == '0') { //1
//      Serial.println("RShift was pressed");
                  Keyboard.write(48);
            }
        if (packetBuffer[0] == '1') { //1
//      Serial.println("1 was pressed");
                  Keyboard.write(49);
            }
        if (packetBuffer[0] == '2') { //1
//      Serial.println("2 was pressed");
                  Keyboard.write(50);
            }
            
        if (packetBuffer == "Spc") { // space bar
//      Serial.println("spacebar was pressed");
                  Keyboard.write(32);
            } 
        if (packetBuffer == "Win") { // windows key
//      Serial.println("gui key was pressed");
                  Keyboard.write(0x83);
            } 

  delay(10);
}
}


Jamie Hamilton
jamiehamilton.net
        if (packetBuffer == "Spc") { // space bar

The address of an array will NEVER equal a string literal.

Using 7 if statements to determine that you need to write the character in position 0 of the array is silly, That could be done with one compound if statement.

the serial monitor just outputs 'list'.

There is nothing in the code that makes this happen. Therefore, it isn't happening.

You might print out the received characters similar to below as hex or decimal representation of the characters so the “non printable” ones can be viewed. Also, the attached ASCII chart might be of use.

    INFO = Serial.read();
    Serial.println(INFO, DEC);
    //Serial.println(INFO, HEX);
    Serial.println();