- //74HC165 start
- //--------------------------------------------------------------------------------------------------------------
- /* How many shift register chips are daisy-chained.
- */
- #define NUMBER_OF_SHIFT_CHIPS 2
- /* Width of data (how many ext lines).
- */
- #define DATA_WIDTH NUMBER_OF_SHIFT_CHIPS * 8
- /* Width of pulse to trigger the shift register to read and latch.
- */
- #define PULSE_WIDTH_USEC 2
- /* Optional delay between shift register reads.
- */
- #define POLL_DELAY_MSEC 0
- /* You will need to change the "int" to "long" If the
- * NUMBER_OF_SHIFT_CHIPS is higher than 2.
- */
- #define BYTES_VAL_T unsigned int
- int ploadPin = 5; // Connects to Parallel load pin the 165
- int clockEnablePin = 6; // Connects to Clock Enable pin the 165
- int dataPin = 7; // Connects to the Q7 pin the 165
- int clockPin = 8; // Connects to the Clock pin the 165
- BYTES_VAL_T pinValues;
- BYTES_VAL_T oldPinValues;
- /* This function is essentially a "shift-in" routine reading the
- * serial Data from the shift register chips and representing
- * the state of those pins in an unsigned integer (or long).
- */
- //--------------------------------------------------------------------------------------------------------------
- int pinA[] = {LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW}; // encoder read A
- int pinB[] = {LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW}; // encoder read B
- int pinALast[] = {LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW};
- int aVal[] = {LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW};
- 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[] = {65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65}; // 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 ButtonChannel[] = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}; // button Channel
- int taste[] = {LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW}; // button state
- int tasteAlt[] = {LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW,LOW, LOW, LOW, LOW, LOW}; // button state old
- int buttons = 10;
- int encoders = 3;
- int encodernums = 6;
- int count = 0;
- int countA = 0;
- int countB = 0;
- int data = LOW;
- 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 shifter0 (3, 2, 4, 3);
- ShiftRegister74HC595 shifter1 (2, 11, 9, 10);
- //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);
- }
- shifter0.setAllHigh();
- shifter1.setAllHigh();
- delay(500);
- shifter0.setAllLow();
- shifter1.setAllLow();
- //set display
- //--------------------------------------------------------------------------------------------------------------
- Serial1.begin(31250);
- delay(500);
- 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);
- }
- //74HC165 setup
- //--------------------------------------------------------------------------------------------------------------
- /* Initialize our digital pins...
- */
- pinMode(ploadPin, OUTPUT);
- pinMode(clockEnablePin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, INPUT);
- digitalWrite(clockPin, LOW);
- digitalWrite(ploadPin, HIGH);
- /* Read in and display the pin states at startup.
- */
- pinValues = read_shift_regs();
- display_pin_values();
- oldPinValues = pinValues;
- }
- //setup ende
- //--------------------------------------------------------------------------------------------------------------
- void loop() {
- //74HC165 read
- pinValues = read_shift_regs();
- /* If there was a chage in state, display which ones changed.
- */
- if(pinValues != oldPinValues)
- {
- display_pin_values();
- oldPinValues = pinValues;
- }
- 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) {
- shifter1.set(i, HIGH); //turn on led
- } else {
- shifter1.set(i, LOW); //turn on led
- }
- }
- }
- }
- }
- }
- }
- // button config
- //--------------------------------------------------------------------------------------------------------------
- void button() {
- for (int i = 0; i < buttons; i++) {
- if (taste[i] == HIGH && tasteAlt[i] == LOW) {
- shifter1.set(i, HIGH);
- MIDISEND(noteOn, ButtonChannel[i], 127);
- tasteAlt[i] = taste[i];
- }
- if (taste[i] == LOW && tasteAlt[i] == HIGH) {
- shifter1.set(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] = 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 (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++){
- shifter0.set(i, HIGH);
- }
- if (led_state_old[shifternum] > ledout) {
- for (int i = ledout; i < led_state_old[shifternum]; i++) {
- shifter0.set(i, LOW);
- }
- }
- led_state_old[shifternum] = ledout;
- }
- //74HC165 funktion
- //--------------------------------------------------------------------------------------------------------------
- BYTES_VAL_T read_shift_regs()
- {
- long bitVal;
- BYTES_VAL_T bytesVal = 0;
- /* Trigger a parallel Load to latch the state of the data lines,
- */
- digitalWrite(clockEnablePin, HIGH);
- digitalWrite(ploadPin, LOW);
- delayMicroseconds(PULSE_WIDTH_USEC);
- digitalWrite(ploadPin, HIGH);
- digitalWrite(clockEnablePin, LOW);
- /* Loop to read each bit value from the serial out line
- * of the SN74HC165N.
- */
- for(int i = 0; i < DATA_WIDTH; i++)
- {
- bitVal = digitalRead(dataPin);
- /* Set the corresponding bit in bytesVal.
- */
- bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));
- /* Pulse the Clock (rising edge shifts the next bit).
- */
- digitalWrite(clockPin, HIGH);
- delayMicroseconds(PULSE_WIDTH_USEC);
- digitalWrite(clockPin, LOW);
- }
- return(bytesVal);
- }
- //--------------------------------------------------------------------------------------------------------------
- /* Dump the list of zones along with their current status.
- */
- void display_pin_values()
- {
- countA = 0;
- for(int i = 0; i < DATA_WIDTH; i++)
- {
- if((pinValues >> i) & 1){
- data = HIGH;
- } else {
- data = LOW;
- }
- if (i < encodernums){
- if (count == 0){
- pinA[countA / 2] = data;
- count = 1;
- } else {
- pinB[countA / 2] = data;
- count = 0;
- }
- countA++;
- } else {
- if (i == encodernums) {
- countA = 0;
- }
- if (countA < buttons){
- taste[countA] = data;
- countA++;
- }
- }
- }
- }