-#define PIN_MUX_S0 6
+#define PIN_MUX_S0 A1
 #define PIN_MUX_S1 12
 #define PIN_MUX_S2 7
 #define PIN_MUX_S3 4
-#define PIN_MUX_AN 5
+#define PIN_MUX_AN A5
 #define PIN_MUX_ENABLE 16
 int values[16];
-int SENSOR_MIN;
-int SENSOR_MAX;
+int SENSOR_MIN = 100;
+int SENSOR_MAX = 500;
+int ini = 8;
+int fim = 16;
 static int S[] = {PIN_MUX_S0, PIN_MUX_S1, PIN_MUX_S2, PIN_MUX_S3};
   
 void readValues(){
-    for(uint8_t i = 8; i < 16; i++){
+    for(uint8_t i = ini; i < fim; i++){
       for(int n = 0; n < 4; n++){
-        digitalWrite(S[n], i & (0x1 << n));
+        digitalWrite(S[n], !!(i & (0x1 << n)));
+      //  Serial.print(!!(i & (0x1 << n)));
       }
+      //Serial.print("\t");
       values[i] = analogRead(PIN_MUX_AN);
     }
   }

float getPosition(){
  float weighted_sum = 0;
  float sum = 0;
  for(int i = 0; i < 9; i++){
    float value = map(getValue(i), SENSOR_MIN, SENSOR_MAX, 0, 1);
    weighted_sum = weighted_sum + value  * (i + 1);
    sum = sum + value;
  }
  float position = ((weighted_sum / sum) - 4.5) / 3.5;

  pinMode(PIN_MUX_S2, OUTPUT);
  pinMode(PIN_MUX_S3, OUTPUT);
  pinMode(PIN_MUX_AN, INPUT);
  //calibrateSensor();
  
}

void loop() {
  digitalWrite(PIN_MUX_ENABLE, HIGH);
  digitalWrite(PIN_MUX_S0, HIGH);
  readValues();
  Serial.print(getPosition());
  Serial.println();
  delay(100);
