Outils pour utilisateurs

Outils du site


arduino:ruban

un ruban tactile avec arduino Midi/USB

/*
   Arduino MIDI Ribbon controller
   de  www.coagula.org
   http://www.coagula.org/content/pages/how-build-midi-ribbon-controller-arduino
   librairie https://github.com/FortySevenEffects/arduino_midi_library
*/
#include <MIDI.h>
#define LED 13       // LED pin on Arduino board
#define minNote 40              // The lower note to play
#define maxNote 100             // The higher note to play

const int sensorPin = A0;       // pin that's connected to pressure sensor
const int sensorPin2 = A1;      // pin that's connected to position sensor

// variables:
int sensorValue = 0;         // the pressure sensor value
int sensorValue2 = 0;        // the position sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
int sensorMin2 = 1023;       // minimum sensor value
int sensorMax2 = 0;          // maximum sensor value
int lastNote = 0;
int lastSensor2Value = 0;
MIDI_CREATE_DEFAULT_INSTANCE();

bool debug = 1;

void setup() {
  if (debug) {
    Serial.begin(57600);
    Serial.print("debug test");
  }
  // turn on LED to signal the calibration sequence:
  pinMode(LED, OUTPUT);
  digitalWrite(13, HIGH);

  // calibrate during the first five seconds
  while (millis() < 5000) {
    sensorValue = analogRead(sensorPin);
    // record the maximum pressure sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }
    // record the minimum pressure sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
    sensorValue2 = analogRead(sensorPin2);
    // record the maximum position sensor value
    if (sensorValue2 > sensorMax2) {
      sensorMax2 = sensorValue2;
    }
    // record the minimum position sensor value
    if (sensorValue2 < sensorMin2) {
      sensorMin2 = sensorValue2;
    }
    if (debug) {
      Serial.print("pression max:");
      Serial.println(sensorMax);
      Serial.print("pression min:");
      Serial.println(sensorMin);
      Serial.print("pos max:");
      Serial.println(sensorMax2);
      Serial.print("pos min:");
      Serial.println(sensorMin2);
      delay(10000);
    } else {
      MIDI.begin(4);
      //MIDI.begin(MIDI_CHANNEL_OMNI);  // Listen to all incoming messages
    }
    // end of calibration sequence
    digitalWrite(13, LOW);

  }

  void loop() {
    // sensors reading:
    sensorValue = analogRead(sensorPin);
    sensorValue2 = analogRead(sensorPin2);
    if (debug) {
      Serial.print("pos ");
      Serial.print(sensorValue2, HEX);
      Serial.print(" pres ");
      Serial.println(sensorValue, HEX);
      delay(1000);
    } else {
      // apply the calibration to the pressure sensor reading
      sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 127);
      // in case the sensor value is outside the range seen during calibration
      sensorValue = constrain(sensorValue, 0, 127);
      // if starts the touch. I added 20 to hte minum just to avoid sensor little variations
      if (sensorValue2 > (sensorMin2 + 20)) {
        delay(30); //this delay is really important to let the position sensor settle its resistance
        sensorValue2 = analogRead(sensorPin2); //read the settled sensor position
        // apply the calibration to the position sensor reading
        sensorValue2 = map(sensorValue2, sensorMin2, sensorMax2, minNote, maxNote);
        // in case the position sensor value is outside the range seen during calibration
        sensorValue2 = constrain(sensorValue2, minNote, maxNote);
        MIDI.sendNoteOn(sensorValue2, 100, 4); // Send a Note (pitch 42, velo 127 on channel 1)
        // register the played note in order to stop it later
        lastNote = sensorValue2;
        digitalWrite(LED, HIGH);    // Blink the LED
        do {
          sensorValue = analogRead(sensorPin);
          sensorValue2 = analogRead(sensorPin2);
          //registers the position sensor value before processing in order to use it later for the loop condition
          lastSensor2Value = sensorValue2;
          // apply the calibration to pressure sensor reading
          sensorValue = map(sensorValue, sensorMin + 200, sensorMax, 0, 127);
          // in case the position sensor value is outside the range seen during calibration
          sensorValue = constrain(sensorValue, 0, 127);
          if (sensorValue2 > (sensorMin2)) {
            // apply the calibration to the position sensor reading
            sensorValue2 = map(sensorValue2, sensorMin2, sensorMax2, minNote, maxNote);
            // in case the position sensor value is outside the range seen during calibration
            sensorValue2 = constrain(sensorValue2, minNote, maxNote);
            // send midi control change defined by position sensor
            MIDI.sendControlChange(20, sensorValue2, 4);
            // send midi control change defined by pressure sensor
            MIDI.sendControlChange(21, sensorValue, 4);

          }
        } while (lastSensor2Value > (sensorMin2 + 10)); //loops till the sensor is pressed
        MIDI.sendNoteOff(lastNote, 0, 4); // Stop the note
        digitalWrite(LED, LOW); // switch off led
      }
    }
  }
arduino/ruban.txt · Dernière modification: 2017/04/11 15:13 (modification externe)