 /**
  * Update an LED's state
  */
-boolean updateLed(int ledNumber, int red, int green, int blue, int brightness) {
+boolean updateLed(int ledNumber, int red, int green, int blue, int brightness) {  
   if(ledNumber >= currentLedCount) {
     // LED out of bounds
     return false;
   }
+  // Normalize brightness value
+  brightness = min(brightness, 31);
   LED *led = &leds[ledNumber];
   boolean needsUpdate = led->red != red
                           || led->green != green

  int endLength = round(((float)(currentLedCount - 1) / 16.0) + 0.5F);
  endLength = max(endLength, 4);
  int i;
  byte headerByte = B11100000;  
  LED *led;
  SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE1));
  // Start by sending 32 zero bits
  for(i = 0 ; i < 4 ; i++) {
    SPI.transfer(0x00);    
  }
  // Send each LED state, header is 1110 0000 & 5-bit brightness, then b/g/r color channels
  for(i = 0 ; i < currentLedCount ; i++) {
    led = &leds[i];
    headerByte = headerByte & led->brightness;
    SPI.transfer(headerByte);
    SPI.transfer(led->blue);
    SPI.transfer(led->green);
    SPI.transfer(led->red);
  }
  for(i = 0 ; i < endLength ; i++) {
    SPI.transfer(0x00);
  }
  SPI.endTransaction();  
  Serial.print("Updated LEDs, endlength: ");
  Serial.println(endLength);
}


  } else if(command[0] == 'S') {
    // Output status - currently for debugging but could be adapted for bidirectional communication
    outputLedState(currentLedCount);
  } else if(command[0] == 'B') {
    printByte(0xCD, true);
  } else if(command[0] == 'T') {
    simulateTransfer();
  }
  return false;
}
