0
#define LED 13
#define BAUDRATE 9600
#define DEBUGOUTPUT 0
#define LED 13
#define powercontrol 10

// checksum variables
byte generatedChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
byte payloadData[64] = {0};
byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;

// system variables
long lastReceivedPacket = 0;
boolean bigPacket = false;

//////////////////////////
// Microprocessor Setup //
//////////////////////////
void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(BAUDRATE); // USB
}

////////////////////////////////
// Read data from Serial UART //
////////////////////////////////
byte ReadOneByte() {
  int ByteRead;
  while(!Serial.available());
  ByteRead = Serial.read();
  #if DEBUGOUTPUT 
  Serial.print((char)ByteRead);
  // echo the same byte out the USB serial (for debug purposes)
  #endif
  return ByteRead;
}

/////////////
//MAIN LOOP//
/////////////
void loop() {
  // Look for sync bytes
  if(ReadOneByte() == 170) {
    if(ReadOneByte() == 170) {
      payloadLength = ReadOneByte();
      if(payloadLength > 169)
      //Payload length can not be greater than 169
        return;
      generatedChecksum = 0;
      for(int i = 0; i < payloadLength; i++) {
        payloadData[i] = ReadOneByte();
        //Read payload into memory
        generatedChecksum += payloadData[i];
      }
      checksum = ReadOneByte();
      //Read checksum byte from stream
      generatedChecksum = 255 - generatedChecksum;
      //Take one's complement of generated checksum
      if(checksum == generatedChecksum) {
        poorQuality = 200;
        attention = 0;
        meditation = 0;
        for(int i = 0; i < payloadLength; i++) {
          // Parse the payload
          switch (payloadData[i]) {
            case 2:
              i++;
              poorQuality = payloadData[i];
              bigPacket = true;
              break;
            case 4:
              i++;
              attention = payloadData[i];
              break;
            case 5:
              i++;
              meditation = payloadData[i];
              break;
            case 0x80:
              i = i + 3;
              break;
            case 0x83:
              i = i + 25;
              break;
            default:
              break;
          } // switch
        } // for loop

        #if !DEBUGOUTPUT
        // *** Add your code here ***
        if(bigPacket) {
          if(poorQuality == 0) digitalWrite(LED, HIGH);
          else digitalWrite(LED, LOW);
          Serial.print("PoorQuality: ");
          Serial.print(poorQuality, DEC);
          Serial.print(" Attention: ");
          Serial.print(attention, DEC);
          Serial.print(" Time since last packet: ");
          Serial.print(millis() - lastReceivedPacket, DEC);
          lastReceivedPacket = millis();
          Serial.print("\n");
          switch(attention / 10) {
            digitalWrite(LED, HIGH);
            digitalWrite(LED, LOW);
          }
        }
        #endif
        bigPacket = false;
      } else {
        // Checksum Error
      } // end if else for checksum
    } // end if read 0xAA byte
  } // end if read 0xAA byte
}
  • 1
    A snapshot of the output would be quite helpful... – KC Tucker Feb 27 '17 at 6:45
1

It time to start stroking our crystal balls, what symbols?

Actually it doesn't really matter, ReadOneByte() is the cause, you are printing ByteRead without checking that it is printable. Character greater than 127 will look strange when you print them out, only 32-126 are printable (127 is backspace) see http://www.asciitable.com/ for more details. In this case I would print the value as a hex number, Serial.println(ByteRead, HEX);

If you are still getting symbols after changing that there may be other prints that have the same problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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