Untitled

 avatar
unknown
plain_text
4 years ago
1.3 kB
6
Indexable
#include "MIDIUSB.h"

//Define pots
const byte NUM_POTS = 3;
byte pot[NUM_POTS] = {0, 0, 0};
byte lastpot[NUM_POTS] = {0, 0, 0};
byte midi_cc[NUM_POTS] = {11, 60, 23};  //Define cc number of each pot

// Define button
const byte BTN_PIN = 0;
int btnLastState = HIGH;  // Pin is pullup so start in a quiescent state
int btnState;

void setup() {
  Serial.begin(115200); 
  pinMode(BTN_PIN, INPUT_PULLUP);
}

// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
}

void loop() {
  // Button
  btnState = digitalRead(BTN_PIN);

  if(btnState == 0) {
    Serial.println(btnState);
    controlChange(0, 50, btnState);  
  }

  // Pots
  for (byte i = 0; i < NUM_POTS; i++) 
  {
    pot[i] = analogRead(i) >> 3;
    if (lastpot[i] != pot[i]) {
      controlChange(0, midi_cc[i], pot[i]); // Set the value of controller midi_cc[i] on channel 0 (1 for the user) to pot[i] which is read by the fader
      lastpot[i] = pot[i];
    }
  }
}
Editor is loading...