- int pinA[] = { 8, 5,52}; // encoder read A
- int pinB[] = { 9, 6,53}; // encoder read B
- int pinALast[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- int aVal[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- int EncoderChannel[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; // encoder channel
- int encoderPosCount[] = {64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64}; // encoder start value
- int led[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- int led_state_old[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- int encodernums = 0;
- int input[] = {10, 30, 31, 34, 35}; // button input
- int output[] = {12, 32, 33, 36, 37}; // button activ LED out
- int ButtonChannel[] = {21, 22, 23, 24, 25}; // button Channel
- int taste[] = {LOW, LOW, LOW, LOW, LOW}; // button state
- int tasteAlt[] = {LOW, LOW, LOW, LOW, LOW}; // button state old
- int buttons = 5;
- int encoders = 3;
- unsigned long millisold = 10000;
- int noteOn = 176; //note state
- byte commandByte; // read note state
- byte noteByte; // read Midi Channel
- byte velocityByte; // read Midi value
- //74HC595 start
- //--------------------------------------------------------------------------------------------------------------
- #define SER_Pin 2 //Serial data input
- #define RCLK_Pin 3 //Register clock
- #define SRCLK_Pin 4 //Shift register clock
- #define NumOf 4 //Shift register clock
- #include <ShiftRegister74HC595.h>
- // create shift register object (number of shift registers, data pin, clock pin, latch pin)
- ShiftRegister74HC595 shifter (3, 2, 4, 3);
- //time
- //--------------------------------------------------------------------------------------------------------------
- #include <TimeLib.h>
- #include <Wire.h>
- #include <DS1307RTC.h>
- //start LCD
- //--------------------------------------------------------------------------------------------------------------
- #include <LiquidCrystal_I2C.h>
- // set the LCD address to 0x3F for a 20 chars 4 line display
- // Set the pins on the I2C chip used for LCD connections:
- // addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- //--------------------------------------------------------------------------------------------------------------
- void setup() {
- setSyncProvider(RTC.get);
- // start scene
- //--------------------------------------------------------------------------------------------------------------
- lcd.begin(20, 4);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Arduino MIDI");
- lcd.setCursor(0, 1);
- lcd.print("Controller Load ");
- // encoder LED graph start
- //--------------------------------------------------------------------------------------------------------------
- for (int i = 0; i < 9; i++) {
- for (int i1 = 0; i1 < encoders; i1++){
- shifterset(i, i1);
- }
- delay(40);
- }
- for (int i = 9; i > -1; i--) {
- for (int i1 = 0; i1 < encoders; i1++){
- shifterset(i, i1);
- }
- delay(40);
- }
- // setup encoder
- //--------------------------------------------------------------------------------------------------------------
- for (int i = 0; i < 1; i++) {
- pinMode (pinA[i], INPUT);
- pinMode (pinB[i], INPUT);
- pinALast[i] = digitalRead(pinA[i]);
- }
- // setup inputs
- //--------------------------------------------------------------------------------------------------------------
- for (int i = 0; i < 5; i++) {
- pinMode(input[i], INPUT);
- }
- // setup output
- //--------------------------------------------------------------------------------------------------------------
- for (int i = 0; i < 5; i++) {
- pinMode(output[i], OUTPUT);
- }
- //set display
- //--------------------------------------------------------------------------------------------------------------
- Serial1.begin(31250);
- delay(1000);
- lcd.clear();
- lcd.setCursor(2, 2);
- lcd.print("OUT: 000 CH: 000");
- lcd.setCursor(2, 3);
- lcd.print("IN: 000 CH: 000");
- //--------------------------------------------------------------------------------------------------------------
- timedate(); // read time
- //encoder start
- //--------------------------------------------------------------------------------------------------------------
- for (int i = 0; i < encoders; i++){
- led[i] = map(encoderPosCount[i], 0, 127, 0, 8);
- shifterset(led[i],i);
- }
- }
- void loop() {
- readdata(); // Midi data read
- encoder(); // read encoder pos and send Midi data
- button(); // read button state and send Midi data
- if (millis() > millisold) {
- timedate();
- millisold = millis() + 10000;
- }
- }
- // midi send data
- //--------------------------------------------------------------------------------------------------------------
- void MIDISEND(int controlChange, int controllerNummer, int controllerWert) {
- Serial1.write(controlChange);
- Serial1.write(controllerNummer);
- Serial1.write(controllerWert);
- setout(controllerWert, controllerNummer);
- }
- // Midi read data
- //--------------------------------------------------------------------------------------------------------------
- void readdata() {
- if (Serial1.available() > 2) {
- commandByte = Serial1.read();//read first byte
- noteByte = Serial1.read();//read next byte Channel 1-127
- velocityByte = Serial1.read();//read final byte value 0-127
- setin(velocityByte, noteByte);
- if (commandByte == noteOn) { //if note on message
- if (noteByte < 20) {
- //encoders
- for (int i = 0; i < encoders; i++) {
- int encodernum = i + 1;
- if (noteByte == encodernum) {
- encoderPosCount[i] = velocityByte;
- led[i] = map(velocityByte, 0, 127, 0, 8);
- shifterset(led[i],i);
- }
- }
- } else {
- // buttons
- for (int i = 0; i < buttons; i++) {
- if (noteByte == ButtonChannel[i]) {
- if (velocityByte == 127) {
- digitalWrite(output[i], HIGH); //turn on led
- } else {
- digitalWrite(output[i], LOW); //turn on led
- }
- }
- }
- }
- }
- }
- }
- // button config
- //--------------------------------------------------------------------------------------------------------------
- void button() {
- for (int i = 0; i < buttons; i++) {
- taste[i] = digitalRead(input[i]);
- if (taste[i] == HIGH && tasteAlt[i] == LOW) {
- digitalWrite(output[i], HIGH);
- MIDISEND(noteOn, ButtonChannel[i], 127);
- tasteAlt[i] = taste[i];
- }
- if (taste[i] == LOW && tasteAlt[i] == HIGH) {
- digitalWrite(output[i], LOW);
- MIDISEND(noteOn, ButtonChannel[i], 0);
- tasteAlt[i] = taste[i];
- }
- }
- }
- // encoder config
- //--------------------------------------------------------------------------------------------------------------
- void encoder() {
- for (int e_num = 0; e_num < encoders; e_num++) {
- aVal[e_num] = digitalRead(pinA[e_num]);
- if (aVal[e_num] != pinALast[e_num]) { // Means the knob is rotating
- // if the knob is rotating, we need to determine direction
- // We do that by reading pin B.
- if (digitalRead(pinB[e_num]) != aVal[e_num]) { // Means pin A Changed first - We're Rotating Clockwise
- if (encoderPosCount[e_num] < 127) {
- encoderPosCount[e_num]++;
- MIDISEND(noteOn, EncoderChannel[e_num], encoderPosCount[e_num]);
- }
- } else {// Otherwise B changed first and we're moving CCW
- if (encoderPosCount[e_num] > 0) {
- encoderPosCount[e_num]--;
- MIDISEND(noteOn, EncoderChannel[e_num], encoderPosCount[e_num]);
- }
- }
- led[e_num] = map(encoderPosCount[e_num], 0, 127, 0, 8);
- shifterset(led[e_num],e_num);
- }
- pinALast[e_num] = aVal[e_num];
- }
- }
- //time date
- //--------------------------------------------------------------------------------------------------------------
- void timedate() {
- lcd.setCursor(0, 0);
- if (day() < 10) {
- lcd.print("0");
- lcd.print(day());
- } else {
- lcd.print(day());
- }
- lcd.print(".");
- lcd.setCursor(3, 0);
- if (month() < 10) {
- lcd.print("0");
- lcd.print(month());
- } else {
- lcd.print(month());
- }
- lcd.print(".");
- if (year() < 10) {
- lcd.print("0");
- lcd.print(year());
- } else {
- lcd.print(year());
- }
- lcd.setCursor(15, 0);
- if (hour() < 10) {
- lcd.print("0");
- lcd.print(hour());
- } else {
- lcd.print(hour());
- }
- lcd.print(":");
- if (minute() < 10) {
- lcd.print("0");
- lcd.print(minute());
- } else {
- lcd.print(minute());
- }
- }
- //display out
- //--------------------------------------------------------------------------------------------------------------
- void setout (int wert, int chan) {
- lcd.setCursor(7, 2);
- if (wert < 10) {
- lcd.print("00");
- lcd.print(wert);
- } else if (wert < 100) {
- lcd.print("0");
- lcd.print(wert);
- } else {
- lcd.print(wert);
- }
- lcd.setCursor(15, 2);
- if (chan < 10) {
- lcd.print("00");
- lcd.print(chan);
- } else if (chan < 100) {
- lcd.print("0");
- lcd.print(chan);
- } else {
- lcd.print(chan);
- }
- }
- //set input display
- //--------------------------------------------------------------------------------------------------------------
- void setin (int wert, int chan) {
- lcd.setCursor(7, 3);
- if (wert < 10) {
- lcd.print("00");
- lcd.print(wert);
- } else if (wert < 100) {
- lcd.print("0");
- lcd.print(wert);
- } else {
- lcd.print(wert);
- }
- lcd.setCursor(15, 3);
- if (chan < 10) {
- lcd.print("00");
- lcd.print(chan);
- } else if (chan < 100) {
- lcd.print("0");
- lcd.print(chan);
- } else {
- lcd.print(chan);
- }
- }
- //set encoder shift
- //--------------------------------------------------------------------------------------------------------------
- void shifterset(int ledval, int shifternum) {
- int ledout = ledval;
- int start = shifternum * 8;
- for (int i = 0; i < shifternum; i++){
- ledout = ledout + 8;
- }
- for (int i = start; i < ledout; i++){
- shifter.set(i, HIGH);
- }
- if (led_state_old[shifternum] > ledout) {
- for (int i = ledout; i < led_state_old[shifternum]; i++) {
- shifter.set(i, LOW);
- }
- }
- led_state_old[shifternum] = ledout;
- }