/* /* nononono is a eurorack capacitive touch sensor based on OTOTO and Makey Makey ideas and /* Paul Badger CapitiveSense Library, made by Gijs Gieskes 2014/2015 */ #include #include //calibration parameters const long BASELOW = 0; const long BASEHIGH = 1400; long ses1high = BASEHIGH; long ses1low = BASELOW; long ses2high = BASEHIGH; long ses2low = BASELOW; long ses3high = BASEHIGH; long ses3low = BASELOW; long ses4high = BASEHIGH; long ses4low = BASELOW; boolean prev17 = true; //only save on bootup CapacitiveSensor cs_2_4 = CapacitiveSensor(2,4); CapacitiveSensor cs_2_5 = CapacitiveSensor(2,5); CapacitiveSensor cs_2_12 = CapacitiveSensor(2,12); CapacitiveSensor cs_2_14 = CapacitiveSensor(2,14); void setup() { TCCR1B = TCCR1B & 0b11111000 | 0x01; TCCR2B = TCCR2B & 0b11111000 | 0x01; pinMode(3, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(17, INPUT); digitalWrite(17, HIGH); //turn off autocalibrate cs_2_4.set_CS_AutocaL_Millis(0xFFFFFFFF); cs_2_5.set_CS_AutocaL_Millis(0xFFFFFFFF); cs_2_12.set_CS_AutocaL_Millis(0xFFFFFFFF); cs_2_14.set_CS_AutocaL_Millis(0xFFFFFFFF); //Serial.begin(9600); if(EEPROM.read(0) == 1){ //load touch values ses1high = EEPROMReadlong(1); ses1low = EEPROMReadlong(5); ses2high = EEPROMReadlong(10); ses2low = EEPROMReadlong(15); ses3high = EEPROMReadlong(20); ses3low = EEPROMReadlong(25); ses4high = EEPROMReadlong(30); ses4low = EEPROMReadlong(35); } } void loop() { long total1 = cs_2_4.capacitiveSensor(30); long total2 = cs_2_5.capacitiveSensor(30); long total3 = cs_2_12.capacitiveSensor(30); long total4 = cs_2_14.capacitiveSensor(30); //calibration, slightly tab connected object while holding the button to calibrate. if(!digitalRead(17)){ if(prev17){ prev17 = false; //reset all parameters ses1high = BASELOW; ses1low = BASEHIGH; ses2high = BASELOW; ses2low = BASEHIGH; ses3high = BASELOW; ses3low = BASEHIGH; ses4high = BASELOW; ses4low = BASEHIGH; } //calibarte all 4 sensors if(total1 > ses1high){ ses1high = total1; } if(total1 < ses1low){ ses1low = total1 + 96; } if(total2 > ses2high){ ses2high = total2; } if(total2 < ses2low){ ses2low = total2 + 96; } if(total3 > ses3high){ ses3high = total3; } if(total3 < ses3low){ ses3low = total3 + 96; } if(total4 > ses4high){ ses4high = total4; } if(total4 < ses4low){ ses4low = total4 + 96; } }else{ if(!prev17){ prev17 = true; EEPROMWritelong(0, 1); EEPROMWritelong(1, ses1high); EEPROMWritelong(5, ses1low); EEPROMWritelong(10, ses2high); EEPROMWritelong(15, ses2low); EEPROMWritelong(20, ses3high); EEPROMWritelong(25, ses3low); EEPROMWritelong(30, ses4high); EEPROMWritelong(35, ses4low); } } byte total1post = map(constrain(total1, ses1low, ses1high), ses1low, ses1high, 0, 255); byte total2post = map(constrain(total2, ses2low, ses2high), ses2low, ses2high, 0, 255); byte total3post = map(constrain(total3, ses3low, ses3high), ses3low, ses3high, 0, 255); byte total4post = map(constrain(total4, ses4low, ses4high), ses4low, ses4high, 0, 255); delay(10); //dont know why but delay improves stability allot analogWrite(3, 255 - total1post); analogWrite(10, 255 - total2post); analogWrite(9, 255 - total3post); analogWrite(11, 255 - total4post); } //http://playground.arduino.cc/Code/EEPROMReadWriteLong long EEPROMReadlong(long address){ //Read the 4 bytes from the eeprom memory. long four = EEPROM.read(address); long three = EEPROM.read(address + 1); long two = EEPROM.read(address + 2); long one = EEPROM.read(address + 3); //Return the recomposed long by using bitshift. return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF); } //This function will write a 4 byte (32bit) long to the eeprom at //the specified address to address + 3. void EEPROMWritelong(int address, long value){ //Decomposition from a long to 4 bytes by using bitshift. //One = Most significant -> Four = Least significant byte byte four = (value & 0xFF); byte three = ((value >> 8) & 0xFF); byte two = ((value >> 16) & 0xFF); byte one = ((value >> 24) & 0xFF); //Write the 4 bytes into the eeprom memory. EEPROM.write(address, four); EEPROM.write(address + 1, three); EEPROM.write(address + 2, two); EEPROM.write(address + 3, one); }