Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MIDI.h>
- MIDI_CREATE_DEFAULT_INSTANCE();
- // Setting up the counter
- int reading = 0;
- int lowest = 0;
- int highest = 127;
- int changeamnt = 1;
- int ledPin = 13;
- // Timing for polling the encoder
- unsigned long currentTime;
- unsigned long lastTime;
- // Pin definitions
- const int pinA = 8;
- const int pinB = 9;
- // Storing the readings
- boolean encA;
- boolean encB;
- boolean lastA = false;
- byte midiCh = 1; // MIDI channel to be used
- byte note = 36; // Lowest MIDI note
- byte cc = 1; // Lowest MIDI CC
- void setup() {
- pinMode(ledPin, OUTPUT);
- // set the two pins as inputs with internal pullups
- pinMode(pinA, INPUT_PULLUP);
- pinMode(pinB, INPUT_PULLUP);
- // Set up the timing of the polling
- currentTime = millis();
- lastTime = currentTime;
- // Start the serial monitor for debugging
- MIDI.begin();
- MIDI.turnThruOff();
- Serial.begin(9600);
- MIDI.setHandleControlChange(handleControlChange); // Listens to control change
- //MIDI.setHandleNoteOn(handleNoteOn); // Listens to note ons
- //MIDI.setHandleNoteOff(handleNoteOff); // Listens to note offs
- }
- void loop()
- {
- if(lastTime <= (currentTime ))
- {
- MIDI.setHandleControlChange(handleControlChange); // re enable the call back function
- MIDI.read();
- }
- // Read elapsed time
- currentTime = millis();
- // Check if it's time to read
- if(currentTime >= (lastTime + 5))
- {
- MIDI.disconnectCallbackFromType(midi::ControlChange);
- // read the two pins
- encA = digitalRead(pinA);
- encB = digitalRead(pinB);
- // check if A has gone from high to low
- if ((!encA) && (lastA))
- {
- // check if B is high
- if (encB)
- {
- // clockwise
- if (reading + changeamnt <= highest)
- {
- reading = reading + changeamnt;
- }
- }
- else
- {
- // anti-clockwise
- if (reading - changeamnt >= lowest)
- {
- reading = reading - changeamnt;
- }
- }
- // Output reading for debugging
- MIDI.sendControlChange(cc, reading, midiCh);
- //Serial.println(reading);
- }
- // store reading of A and millis for next loop
- lastA = encA;
- lastTime = currentTime;
- }
- }
- ///////////////////////////////////////////
- // MIDI IN
- void handleControlChange(byte channel, byte number, byte value) { // channel, CC, value
- reading = value;
- // Serial.println(value);
- /* if (value != 0){
- digitalWrite(ledPin, HIGH);
- }else
- digitalWrite(ledPin, LOW);
- */
- }
- 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