1. int pinA[] = { 8, 5,52}; // encoder read A
  2. int pinB[] = { 9, 6,53}; // encoder read B
  3. int pinALast[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  4. int aVal[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  5. int EncoderChannel[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; // encoder channel
  6. 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
  7. int led[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  8. int led_state_old[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  9. int encodernums = 0;
  10. int input[] = {10, 30, 31, 34, 35}; // button input
  11. int output[] = {12, 32, 33, 36, 37}; // button activ LED out
  12. int ButtonChannel[] = {21, 22, 23, 24, 25}; // button Channel
  13. int taste[] = {LOW, LOW, LOW, LOW, LOW}; // button state
  14. int tasteAlt[] = {LOW, LOW, LOW, LOW, LOW}; // button state old
  15. int buttons = 5;
  16. int encoders = 3;
  17. unsigned long millisold = 10000;
  18. int noteOn = 176; //note state
  19. byte commandByte; // read note state
  20. byte noteByte; // read Midi Channel
  21. byte velocityByte; // read Midi value
  22. //74HC595 start
  23. //--------------------------------------------------------------------------------------------------------------
  24. #define SER_Pin 2 //Serial data input
  25. #define RCLK_Pin 3 //Register clock
  26. #define SRCLK_Pin 4 //Shift register clock
  27. #define NumOf 4 //Shift register clock
  28. #include <ShiftRegister74HC595.h>
  29. // create shift register object (number of shift registers, data pin, clock pin, latch pin)
  30. ShiftRegister74HC595 shifter (3, 2, 4, 3);
  31. //time
  32. //--------------------------------------------------------------------------------------------------------------
  33. #include <TimeLib.h>
  34. #include <Wire.h>
  35. #include <DS1307RTC.h>
  36. //start LCD
  37. //--------------------------------------------------------------------------------------------------------------
  38. #include <LiquidCrystal_I2C.h>
  39. // set the LCD address to 0x3F for a 20 chars 4 line display
  40. // Set the pins on the I2C chip used for LCD connections:
  41. // addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
  42. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  43. //--------------------------------------------------------------------------------------------------------------
  44. void setup() {
  45. setSyncProvider(RTC.get);
  46. // start scene
  47. //--------------------------------------------------------------------------------------------------------------
  48. lcd.begin(20, 4);
  49. lcd.clear();
  50. lcd.setCursor(0, 0);
  51. lcd.print("Arduino MIDI");
  52. lcd.setCursor(0, 1);
  53. lcd.print("Controller Load ");
  54. // encoder LED graph start
  55. //--------------------------------------------------------------------------------------------------------------
  56. for (int i = 0; i < 9; i++) {
  57. for (int i1 = 0; i1 < encoders; i1++){
  58. shifterset(i, i1);
  59. }
  60. delay(40);
  61. }
  62. for (int i = 9; i > -1; i--) {
  63. for (int i1 = 0; i1 < encoders; i1++){
  64. shifterset(i, i1);
  65. }
  66. delay(40);
  67. }
  68. // setup encoder
  69. //--------------------------------------------------------------------------------------------------------------
  70. for (int i = 0; i < 1; i++) {
  71. pinMode (pinA[i], INPUT);
  72. pinMode (pinB[i], INPUT);
  73. pinALast[i] = digitalRead(pinA[i]);
  74. }
  75. // setup inputs
  76. //--------------------------------------------------------------------------------------------------------------
  77. for (int i = 0; i < 5; i++) {
  78. pinMode(input[i], INPUT);
  79. }
  80. // setup output
  81. //--------------------------------------------------------------------------------------------------------------
  82. for (int i = 0; i < 5; i++) {
  83. pinMode(output[i], OUTPUT);
  84. }
  85. //set display
  86. //--------------------------------------------------------------------------------------------------------------
  87. Serial1.begin(31250);
  88. delay(1000);
  89. lcd.clear();
  90. lcd.setCursor(2, 2);
  91. lcd.print("OUT: 000 CH: 000");
  92. lcd.setCursor(2, 3);
  93. lcd.print("IN: 000 CH: 000");
  94. //--------------------------------------------------------------------------------------------------------------
  95. timedate(); // read time
  96. //encoder start
  97. //--------------------------------------------------------------------------------------------------------------
  98. for (int i = 0; i < encoders; i++){
  99. led[i] = map(encoderPosCount[i], 0, 127, 0, 8);
  100. shifterset(led[i],i);
  101. }
  102. }
  103. void loop() {
  104. readdata(); // Midi data read
  105. encoder(); // read encoder pos and send Midi data
  106. button(); // read button state and send Midi data
  107. if (millis() > millisold) {
  108. timedate();
  109. millisold = millis() + 10000;
  110. }
  111. }
  112. // midi send data
  113. //--------------------------------------------------------------------------------------------------------------
  114. void MIDISEND(int controlChange, int controllerNummer, int controllerWert) {
  115. Serial1.write(controlChange);
  116. Serial1.write(controllerNummer);
  117. Serial1.write(controllerWert);
  118. setout(controllerWert, controllerNummer);
  119. }
  120. // Midi read data
  121. //--------------------------------------------------------------------------------------------------------------
  122. void readdata() {
  123. if (Serial1.available() > 2) {
  124. commandByte = Serial1.read();//read first byte
  125. noteByte = Serial1.read();//read next byte Channel 1-127
  126. velocityByte = Serial1.read();//read final byte value 0-127
  127. setin(velocityByte, noteByte);
  128. if (commandByte == noteOn) { //if note on message
  129. if (noteByte < 20) {
  130. //encoders
  131. for (int i = 0; i < encoders; i++) {
  132. int encodernum = i + 1;
  133. if (noteByte == encodernum) {
  134. encoderPosCount[i] = velocityByte;
  135. led[i] = map(velocityByte, 0, 127, 0, 8);
  136. shifterset(led[i],i);
  137. }
  138. }
  139. } else {
  140. // buttons
  141. for (int i = 0; i < buttons; i++) {
  142. if (noteByte == ButtonChannel[i]) {
  143. if (velocityByte == 127) {
  144. digitalWrite(output[i], HIGH); //turn on led
  145. } else {
  146. digitalWrite(output[i], LOW); //turn on led
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. // button config
  155. //--------------------------------------------------------------------------------------------------------------
  156. void button() {
  157. for (int i = 0; i < buttons; i++) {
  158. taste[i] = digitalRead(input[i]);
  159. if (taste[i] == HIGH && tasteAlt[i] == LOW) {
  160. digitalWrite(output[i], HIGH);
  161. MIDISEND(noteOn, ButtonChannel[i], 127);
  162. tasteAlt[i] = taste[i];
  163. }
  164. if (taste[i] == LOW && tasteAlt[i] == HIGH) {
  165. digitalWrite(output[i], LOW);
  166. MIDISEND(noteOn, ButtonChannel[i], 0);
  167. tasteAlt[i] = taste[i];
  168. }
  169. }
  170. }
  171. // encoder config
  172. //--------------------------------------------------------------------------------------------------------------
  173. void encoder() {
  174. for (int e_num = 0; e_num < encoders; e_num++) {
  175. aVal[e_num] = digitalRead(pinA[e_num]);
  176. if (aVal[e_num] != pinALast[e_num]) { // Means the knob is rotating
  177. // if the knob is rotating, we need to determine direction
  178. // We do that by reading pin B.
  179. if (digitalRead(pinB[e_num]) != aVal[e_num]) { // Means pin A Changed first - We're Rotating Clockwise
  180. if (encoderPosCount[e_num] < 127) {
  181. encoderPosCount[e_num]++;
  182. MIDISEND(noteOn, EncoderChannel[e_num], encoderPosCount[e_num]);
  183. }
  184. } else {// Otherwise B changed first and we're moving CCW
  185. if (encoderPosCount[e_num] > 0) {
  186. encoderPosCount[e_num]--;
  187. MIDISEND(noteOn, EncoderChannel[e_num], encoderPosCount[e_num]);
  188. }
  189. }
  190. led[e_num] = map(encoderPosCount[e_num], 0, 127, 0, 8);
  191. shifterset(led[e_num],e_num);
  192. }
  193. pinALast[e_num] = aVal[e_num];
  194. }
  195. }
  196. //time date
  197. //--------------------------------------------------------------------------------------------------------------
  198. void timedate() {
  199. lcd.setCursor(0, 0);
  200. if (day() < 10) {
  201. lcd.print("0");
  202. lcd.print(day());
  203. } else {
  204. lcd.print(day());
  205. }
  206. lcd.print(".");
  207. lcd.setCursor(3, 0);
  208. if (month() < 10) {
  209. lcd.print("0");
  210. lcd.print(month());
  211. } else {
  212. lcd.print(month());
  213. }
  214. lcd.print(".");
  215. if (year() < 10) {
  216. lcd.print("0");
  217. lcd.print(year());
  218. } else {
  219. lcd.print(year());
  220. }
  221. lcd.setCursor(15, 0);
  222. if (hour() < 10) {
  223. lcd.print("0");
  224. lcd.print(hour());
  225. } else {
  226. lcd.print(hour());
  227. }
  228. lcd.print(":");
  229. if (minute() < 10) {
  230. lcd.print("0");
  231. lcd.print(minute());
  232. } else {
  233. lcd.print(minute());
  234. }
  235. }
  236. //display out
  237. //--------------------------------------------------------------------------------------------------------------
  238. void setout (int wert, int chan) {
  239. lcd.setCursor(7, 2);
  240. if (wert < 10) {
  241. lcd.print("00");
  242. lcd.print(wert);
  243. } else if (wert < 100) {
  244. lcd.print("0");
  245. lcd.print(wert);
  246. } else {
  247. lcd.print(wert);
  248. }
  249. lcd.setCursor(15, 2);
  250. if (chan < 10) {
  251. lcd.print("00");
  252. lcd.print(chan);
  253. } else if (chan < 100) {
  254. lcd.print("0");
  255. lcd.print(chan);
  256. } else {
  257. lcd.print(chan);
  258. }
  259. }
  260. //set input display
  261. //--------------------------------------------------------------------------------------------------------------
  262. void setin (int wert, int chan) {
  263. lcd.setCursor(7, 3);
  264. if (wert < 10) {
  265. lcd.print("00");
  266. lcd.print(wert);
  267. } else if (wert < 100) {
  268. lcd.print("0");
  269. lcd.print(wert);
  270. } else {
  271. lcd.print(wert);
  272. }
  273. lcd.setCursor(15, 3);
  274. if (chan < 10) {
  275. lcd.print("00");
  276. lcd.print(chan);
  277. } else if (chan < 100) {
  278. lcd.print("0");
  279. lcd.print(chan);
  280. } else {
  281. lcd.print(chan);
  282. }
  283. }
  284. //set encoder shift
  285. //--------------------------------------------------------------------------------------------------------------
  286. void shifterset(int ledval, int shifternum) {
  287. int ledout = ledval;
  288. int start = shifternum * 8;
  289. for (int i = 0; i < shifternum; i++){
  290. ledout = ledout + 8;
  291. }
  292. for (int i = start; i < ledout; i++){
  293. shifter.set(i, HIGH);
  294. }
  295. if (led_state_old[shifternum] > ledout) {
  296. for (int i = ledout; i < led_state_old[shifternum]; i++) {
  297. shifter.set(i, LOW);
  298. }
  299. }
  300. led_state_old[shifternum] = ledout;
  301. }