   NodeSerial.write(state); // volumeon
   NodeSerial.write(0); // empty byte, because we always expect 4 bytes ;-)
 }
+
+
+
+void sendSoundLevel(int pin, int level){
+  if(pin != 0) return; //debug
+  
+  
+  
+  uint16_t number = level;
+  uint16_t mask   = B11111111;
+  uint8_t first_half   = number >> 8;
+  uint8_t sencond_half = number & mask;
+  
+  // always start with 3 bytes of 255 so that Node.js knows were this 'packet' starts:
+  NodeSerial.write(255);
+  NodeSerial.write(255);
+  NodeSerial.write(255);
+  NodeSerial.write(03); // 03 = soundlevel
+  NodeSerial.write(pin);  // channel
+  NodeSerial.write(first_half);   // value, first byte (Big Endian)
+  NodeSerial.write(sencond_half); // value, last byte  (Big Endian)
+}
+
+
+
+
+
 
 void readIncommingNodeData() {
   // READ RESPONSE

/* Function prototypes -------------------------------------------------------*/
void updateState(int pin, int state);
void sendSoundState(int pin, int state);
void readIncommingNodeData();
void sendSoundLevels();
void checkSoundStates();

/* Variables -----------------------------------------------------------------*/
#define NodeSerial Serial1 // makes it a little easier to read the code :-) NodeSerial is the serial port that communicates with Node.js, accesible as /dev/ttyATH0 in Linux

      //reset:
      signalMax[pin] = 0;
      signalMin[pin] = 1024;
      startMillis = millis();
      
      // check sound state (SOUND or SILCENCE):
      checkSoundState(pin, soundValues[pin]);
      
      // send sound level (if required):
      sendSoundLevel(pin, soundValues[pin]);
    }
  }
  
  readIncommingNodeData();
}

void checkSoundState(int pin, int level){
  if(pin != 0) return; //only check pin 0 for now
    
  if( currentState == 0 ) {
    // IF SILENCE
    
    if(level > soundThreshold){ // if louder than soundThreshold
      soundSampleCounter++;
    }else{
      soundSampleCounter = 0; // reset
    }
    
    if(soundSampleCounter >= nrOfConsecutiveSoundSamplesBeforeFlippingToSilence){
      currentState = 1; // flip to "SOUND"
      soundSampleCounter = 0; // reset soundSampleCounter for the next time there is SILENCE
      
      sendSoundState(pin, currentState); // send sound state to Node.js
    }
    
  }else{
    // IF SOUND
    
    if(level < silenceThreshold){ // if quieter than silenceThreshold
      silenceSampleCounter++;
    }else{
      silenceSampleCounter = 0; // reset
    }
    
    if(silenceSampleCounter >= nrOfConsecutiveSilenceSamplesBeforeFlippingToSound){
      currentState = 0; // flip to "SILENCE"
      silenceSampleCounter = 0; // reset silenceSampleCounter for the next time there is SOUND

      sendSoundState(pin, currentState);
    }
  
  }
}

void sendSoundState(int pin, int state) {
  digitalWrite(ledPin, state); // debug to LED
  
  if(debug) Serial.print("> pin: ");
  if(debug) Serial.print(pin);
  if(debug) Serial.print(" | state: ");
