Monday, 1 June 2015

Dub Siren For The Keyboard PLayer ( Code )

For the last post DUB SIREN FOR THE LIVE KEYBOARD PLAYER, all you need is an Arduino Midi Shield (  ( you should find a local distributor for all these) , an Arduino Uno, R3 preferably, and MIDI library ( I am using the Arduino_MIDI_Library_v4.2) along with this code. NO SOLDERING DONE !

#include <MIDI.h>
//
MIDI_CREATE_DEFAULT_INSTANCE();
#define PBMAX 8191
// defines for MIDI Shield components only
#define POT  0
#define POT2  1

#define BUTTON1  2
#define BUTTON2  3
#define BUTTON3  4

#define LED  7
#define LED2  6
//
#define SINSAMPLES 256
int sineData [SINSAMPLES];
bool ledOn = false;
bool ledOn2 = false;
unsigned int inc = 0;
uint16_t noteIn = 0;
uint16_t lastnote = 0;
char off = 0;
char lastplay = 0;

//char drv = 0; // digital read variable

void setup()
{
  sine();
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(POT, INPUT);
  pinMode(POT2, INPUT);
  pinMode(BUTTON1, INPUT);
  // defines for MIDI Shield components only
  digitalWrite(LED2, HIGH);
  digitalWrite(LED, HIGH);
  digitalWrite(BUTTON1, HIGH);
  //
  MIDI.begin();          // Launch MIDI
}
void loop()
{ uint16_t average = noteIn;

  uint16_t anIn = analogRead(POT);
  char play = !(digitalRead(BUTTON1));
  if (lastplay != play) {
    if (play == 0)
      off = 1;
  }
  lastplay = play;
  noteIn = analogRead(POT2);

  average = (average + noteIn) >> 1;
  uint8_t note = average >> 3;

  if (play == 1) {
    if (lastnote != note) {
      MIDI.sendPitchBend(0, 1);//reset pitch bend
      delay(10);
      MIDI.sendNoteOff(lastnote, 0, 1); // Stop the note
      delay(10);
      MIDI.sendNoteOn(note, 127, 1); // Send a Note (pitch , velo 127 on channel 1)
      lastnote = note;
    }
    else {
      lastnote = note;
    }
    int smpl = sineData[inc];
    int var = (smpl - PBMAX); // this calculation can be avoided
    MIDI.sendPitchBend(var, 1);
    delay(10);
    ledOn = !ledOn;
    digitalWrite(LED, ledOn);

    inc++;
    inc += (anIn >> 4);
    if (inc >= SINSAMPLES) {
      inc -= SINSAMPLES;
      ledOn2 = !ledOn2;
      digitalWrite(LED2, ledOn2);
    }
  }
  if (play == 0) {
    if (off == 1) {
      MIDI.sendPitchBend(0, 1);//reset pitch bend
      delay(10);
      MIDI.sendNoteOff(lastnote, 0, 1); // Stop the note
      inc = 0;
      lastnote = 0;
      digitalWrite(LED2, HIGH);
      digitalWrite(LED, HIGH);
      off = 0;
    }
  }
}
void sine () {
  int i;
  int b;
  for (i = 0; i < SINSAMPLES; i++) {
    b = PBMAX * sin((2 * PI / SINSAMPLES) * i);
    b += PBMAX;
    sineData [i] = b;
  }
}

No comments:

Post a Comment

Feel free to contact me with any suggestions, doubts or requests.

Bless