/* Code Arduino pour le projet "TapisSon" un tapis interactif et musical bidirectional potentiometer this sketch sense the two resistor VccGndPinA | | - R1 | | |-----| textile |---------> sensorPin | | | | - R2 - R3 | | | | VccGndPinB GND R1 is variable R2 is variable R3 is constant 10K */ /////////////////////// variables const unsigned int VccGndPinA = 5; // use as Vcc or Gnd const unsigned int VccGndPinB = 10; // use as Vcc or gnd const unsigned int sensorPin = A0; // select the input pin for both side of the potentiometer sensing int rowSensorVal = 0; unsigned int DELAY = 50; boolean DEBUG = true; /////////////////////// initialisation void setup(){ pinMode(VccGndPinA, OUTPUT); pinMode(VccGndPinB, OUTPUT); if(DEBUG) Serial.begin(9600), Serial.println("boot"); } /////////////////////// boucle principale void loop(){ ////////////////////////////////////////////////////// time 0 digitalWrite(VccGndPinA, HIGH); // GND digitalWrite(VccGndPinB, LOW); // GND delay(DELAY); rowSensorVal = analogRead(sensorPin); if(DEBUG) Serial.print(rowSensorVal), Serial.print(" "); if(!DEBUG) transmit2byteA(rowSensorVal); ////////////////////////////////////////////////////// time 1 digitalWrite(VccGndPinA, LOW); // GND digitalWrite(VccGndPinB, HIGH); // GND delay(DELAY); rowSensorVal = analogRead(sensorPin); if(DEBUG) Serial.println(rowSensorVal); if(!DEBUG) transmit2byteB(rowSensorVal); delay(DELAY); } ////////////////// fonctions de communication série (Arduino -> PC) void transmit2byteA(int data){ byte msbA, lsbA; // octet 1 for X ; MSB for X (More Signifiant Bits) // 00000xxx Value (after being shifted 5 times) // || // ||--------> MSB // |---------> X msbA = data >> 5 ; // Division by 32, save the 5 Msb bits Serial.write(msbA); // Send X MSB // octet 0 for X ; LSB for X (Less Signifiant Bits) // xxxxxxxx value // 00011111 & mask (31) // 01000000 + flag // 010xxxxx = result for // 01000000 + 64 for LSB flag // first bit is 0 for X flag lsbA = data & 31 ; // save the 5 lsb bits lsbA = lsbA + 64 ; // set second bit to 1 for lsb marker Serial.write(lsbA); // Send X LSB } void transmit2byteB(int data){ byte msbB, lsbB; // octet 1 for Y ; MSB for Y (More Significant Bits) // 10000xxx Value (after being shifted 5 times) // || // ||--------> MSB // |---------> 1=>Y msbB = data >> 5 ; // Division by 32, save the 5 Msb bits msbB = msbB + 128 ; // add a bit for Y Serial.write(msbB); // Send Y MSB // octet 0 for Y ; LSB for Y (Less Significant Bits) // xxxxxxxx value // 00011111 & mask // 000xxxxx = result // 01000000 + 64 for LSB flag // 10000000 + 128 for Y flag lsbB = data & 31 ; // save the 5 lsb bits lsbB = lsbB + 64 ; // set second bit to 1 for lsb marker lsbB = lsbB + 128 ; // add a bit for Y Serial.write(lsbB); // Send Y LSB }