Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* MAKING MUSIC WITH ARDUINO
- "Encoder 4 - MIDI CC for Traktor"
- by Gustavo Silveira
- ofered by: www.musiconerd.com & www.bitcontrollers.com
- gustavosilveira@musiconerd.com
- */
- #include <EnableInterrupt.h>
- #include <MIDI.h>
- #include <RotaryEncoder.h>;
- MIDI_CREATE_DEFAULT_INSTANCE();
- #define outputA 8
- #define outputB 9
- #define outputA2 6
- #define outputB2 7
- #define ledPin 5
- int val = 0;
- int val2 = 0;
- RotaryEncoder encoder(8,9,10,3,10);
- RotaryEncoder encoder2(6,7,10,3,10);
- int counter = 0;
- int aState;
- int aLastState;
- int counter2 = 0;
- int aState2;
- int aLastState2;
- /////////////////////////////////////////////
- // buttons
- const int NButtons = 1;
- const int buttonPin[NButtons] = {4}; // the number of the pushbutton pin
- int buttonCState[NButtons] = {0}; // stores the button current value
- int buttonPState[NButtons] = {0}; // stores the button previous value
- /////////////////////////////////////////////
- // debounce
- unsigned long lastDebounceTime[NButtons] = {0}; // the last time the output pin was toggled
- unsigned long debounceDelay = 5; // the debounce time; increase if the output flickers
- /////////////////////////////////////////////
- // midi
- byte midiCh = 1; // MIDI channel to be used
- byte note = 36; // Lowest MIDI note
- byte cc = 1; // Lowest MIDI CC
- void setup() {
- enableInterrupt(outputA, encoderRot, CHANGE);
- enableInterrupt(outputB, encoderRot, CHANGE);
- enableInterrupt(val, encoderRot, CHANGE);
- pinMode (outputA, INPUT_PULLUP);
- pinMode (outputB, INPUT_PULLUP);
- pinMode (val, INPUT_PULLUP);
- pinMode (buttonPin[0], INPUT_PULLUP);
- pinMode (ledPin, OUTPUT);
- enableInterrupt(outputA2, encoderRot2, CHANGE);
- enableInterrupt(outputB2, encoderRot2, CHANGE);
- enableInterrupt(val2, encoderRot2, CHANGE);
- pinMode (outputA2, INPUT_PULLUP);
- pinMode (outputB2, INPUT_PULLUP);
- pinMode (val2, INPUT_PULLUP);
- // Reads the initial state of the outputA
- aLastState = digitalRead(outputA);
- aLastState2 = digitalRead(outputA2);
- MIDI.begin();
- Serial.begin(115200);
- MIDI.setHandleControlChange(handleControlChange); // Listens to control change
- MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
- MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
- }
- void loop() {
- int enc = encoder.readEncoder();
- if(enc != 0) {
- val = val + (enc);
- val = min(val,127);
- val = max(val,0);
- //Serial.println(val);
- }
- int enc2 = encoder2.readEncoder();
- if(enc2 != 0) {
- val2 = val2 + (enc2);
- val2 = min(val2,127);
- val2 = max(val2,0);
- //Serial.println(val);
- }
- delayMicroseconds(1);
- buttons();
- }
- void encoderRot () {
- aState = digitalRead(outputA); // Reads the current "state" of the Encoder
- // If the previous state and the current state of outputA are different, it means that a pulse occurred
- if (aState != aLastState) {
- // If the outputB state is different from outputA it means that the Encoder is rotating clockwise
- if (digitalRead(outputB) != aState) {
- MIDI.sendControlChange(cc, val, 1);
- } else {
- MIDI.sendControlChange(cc, val, 1);
- }
- //Serial.print("Midi CC: "); Serial.println(val);
- }
- aLastState = aState; // Stores the actual value in the previous value
- }
- void encoderRot2 () {
- aState2 = digitalRead(outputA2); // Reads the current "state" of the Encoder
- // If the previous state and the current state of outputA are different, it means that a pulse occurred
- if (aState2 != aLastState2) {
- // If the outputB state is different from outputA it means that the Encoder is rotating clockwise
- if (digitalRead(outputB2) != aState2) {
- MIDI.sendControlChange(cc, val2, 2);
- } else {
- MIDI.sendControlChange(cc, val2, 2);
- }
- //Serial.print("Midi CC: "); Serial.println(val);
- }
- aLastState2 = aState2; // Stores the actual value in the previous value
- }
- /////////////////////////////////////////////
- // BUTTONS
- void buttons() {
- for (int i = 0; i < NButtons; i++) {
- buttonCState[i] = digitalRead(buttonPin[i]);
- if ((millis() - lastDebounceTime[i]) > debounceDelay) {
- if (buttonPState[i] != buttonCState[i]) {
- lastDebounceTime[i] = millis();
- if (buttonCState[i] == LOW) {
- MIDI.sendNoteOn(note + i, 127, midiCh);
- // Serial.print("button on >> ");
- // Serial.println(i);
- }
- else {
- MIDI.sendNoteOn(note + i, 0, midiCh);
- // Serial.print("button off >> ");
- // Serial.println(i);
- }
- buttonPState[i] = buttonCState[i];
- }
- }
- }
- }
- ////////////////////////////////////////////
- // MIDI IN
- void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
- }
- void handleNoteOn(byte channel, byte number, byte value) { // channel, note, velocity
- }
- void handleNoteOff(byte channel, byte number, byte value) { // channel, note, velocity
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement