 /* Function prototypes -------------------------------------------------------*/
-void updateState(int pin, int state);
+void readIncommingNodeData();
+void checkSoundState(int pin, int level);
 void sendSoundState(int pin, int state);
-void readIncommingNodeData();
-void sendSoundLevels();
-void checkSoundStates();
+void sendSoundLevel(int pin, int level);
 
 /* 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

unsigned int signalMin[pinCount] = {1024};
unsigned long startMillis = millis();

int silenceSamples = 60;
int soundSamples = 60;
int silenceThreshold = 880;
int silenceCapacitor = silenceSamples;
int soundCapacitor = 0;
int soundThreshold = 990;
int currentState = 0;


int threshold = 700; //in mV
int capacity[pinCount] = {0};
int thresholdCapacity[pinCount] = {0};
int currentSoundState[pinCount] = {0};

bool debug = false;

  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
