1. //74HC165 start
  2. //--------------------------------------------------------------------------------------------------------------
  3. /* Width of pulse to trigger the shift register to read and latch.
  4. */
  5. #define PULSE_WIDTH_USEC 1
  6. /* Optional delay between shift register reads.
  7. */
  8. #define POLL_DELAY_MSEC 1
  9. /* You will need to change the "int" to "long" If the
  10. * NUMBER_OF_SHIFT_CHIPS is higher than 2.
  11. */
  12. int ploadPin = 5; // Connects to Parallel load pin the 165
  13. int clockEnablePin = 6; // Connects to Clock Enable pin the 165
  14. int dataPin = 7; // Connects to the Q7 pin the 165
  15. int clockPin = 8; // Connects to the Clock pin the 165
  16. /* How many shift register chips are daisy-chained.
  17. */
  18. const byte NUMBER_OF_SHIFT_CHIPS = 6;
  19. /* Width of data (how many ext lines).
  20. */
  21. const byte DATA_WIDTH = NUMBER_OF_SHIFT_CHIPS * 8;
  22. /* This function is essentially a "shift-in" routine reading the
  23. * serial Data from the shift register chips and representing
  24. * the state of those pins in an unsigned integer (or long).
  25. */
  26. //--------------------------------------------------------------------------------------------------------------
  27. const int buttons = 16;
  28. const int encoders = 16;
  29. const int encodernums = encoders * 2;
  30. int pinA[encoders]; // encoder read A
  31. int pinB[encoders]; // encoder read B
  32. int pinALast[encoders];
  33. int aVal[encoders];
  34. int encoderPosCount[encoders]; // encoder start value
  35. int taste[buttons]; // button state
  36. int tasteAlt[buttons]; // button state old
  37. int led[encoders];
  38. int led_state_old[encoders];
  39. int EncoderChannel[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; // encoder channel
  40. int ButtonChannel[] = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}; // button Channel
  41. int shift[DATA_WIDTH];
  42. int shift_old[DATA_WIDTH];
  43. int count = 0;
  44. int countA = 0;
  45. int countB = 0;
  46. int data = LOW;
  47. unsigned long millisold = 10000;
  48. int noteOn = 176; //note state
  49. byte commandByte; // read note state
  50. byte noteByte; // read Midi Channel
  51. byte velocityByte; // read Midi value
  52. int adjust = 0;
  53. int adjustold = 0;
  54. int adjuststate = 0;
  55. //74HC595 start
  56. //--------------------------------------------------------------------------------------------------------------
  57. #include <ShiftRegister74HC595.h>
  58. // create shift register object (number of shift registers, data pin, clock pin, latch pin)
  59. ShiftRegister74HC595 shifter0 (14, 2, 4, 3);
  60. ShiftRegister74HC595 shifter1 (2, 11, 9, 10);
  61. //time
  62. //--------------------------------------------------------------------------------------------------------------
  63. #include <TimeLib.h>
  64. #include <Wire.h>
  65. #include <DS1307RTC.h>
  66. //start LCD
  67. //--------------------------------------------------------------------------------------------------------------
  68. #include <LiquidCrystal_I2C.h>
  69. // set the LCD address to 0x3F for a 20 chars 4 line display
  70. // Set the pins on the I2C chip used for LCD connections:
  71. // addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
  72. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  73. //--------------------------------------------------------------------------------------------------------------
  74. void setup() {
  75. //power LED RG
  76. pinMode(24, OUTPUT);
  77. pinMode(25, OUTPUT);
  78. digitalWrite(25, HIGH);
  79. setSyncProvider(RTC.get);
  80. // start scene
  81. //--------------------------------------------------------------------------------------------------------------
  82. lcd.begin(20, 4);
  83. lcd.clear();
  84. lcd.setCursor(0, 0);
  85. lcd.print("Arduino MIDI");
  86. lcd.setCursor(0, 1);
  87. lcd.print("Controller Load ");
  88. // encoder LED graph start
  89. //--------------------------------------------------------------------------------------------------------------
  90. // for (int i = 0; i < 9; i++) {
  91. // for (int i1 = 0; i1 < encoders; i1++) {
  92. // shifterset(i, i1);
  93. // }
  94. // delay(1);
  95. // }
  96. // for (int i = 8; i > -1; i--) {
  97. // for (int i1 = 0; i1 < encoders; i1++) {
  98. // shifterset(i, i1);
  99. // }
  100. // delay(1);
  101. // }
  102. // delay(100);
  103. for (int i = 0; i < 3; i++) {
  104. shifter0.setAllHigh();
  105. shifter1.setAllHigh();
  106. delay(250);
  107. shifter0.setAllLow();
  108. shifter1.setAllLow();
  109. delay(250);
  110. }
  111. shifter0.setAllHigh();
  112. shifter1.setAllHigh();
  113. delay(1000);
  114. shifter0.setAllLow();
  115. shifter1.setAllLow();
  116. //encoder start
  117. //--------------------------------------------------------------------------------------------------------------
  118. for (int i = 0; i < encoders; i++) {
  119. led[i] = map(encoderPosCount[i], 0, 127, 0, 8);
  120. shifterset(led[i], i);
  121. }
  122. //fine adjust
  123. //--------------------------------------------------------------------------------------------------------------
  124. pinMode(23, OUTPUT);
  125. pinMode(22, INPUT);
  126. //74HC165 setup
  127. //--------------------------------------------------------------------------------------------------------------
  128. /* Initialize our digital pins...
  129. */
  130. pinMode(ploadPin, OUTPUT);
  131. pinMode(clockEnablePin, OUTPUT);
  132. pinMode(clockPin, OUTPUT);
  133. pinMode(dataPin, INPUT);
  134. digitalWrite(clockPin, LOW);
  135. digitalWrite(ploadPin, HIGH);
  136. /* Read in and display the pin states at startup.
  137. */
  138. read_shift_regs();
  139. display_pin_values();
  140. //set display
  141. //--------------------------------------------------------------------------------------------------------------
  142. Serial1.begin(31250);
  143. //Serial.begin(115200);
  144. delay(500);
  145. lcd.clear();
  146. timedate();
  147. lcd.setCursor(2, 2);
  148. lcd.print("OUT: 000 CH: 000");
  149. lcd.setCursor(2, 3);
  150. lcd.print("IN: 000 CH: 000");
  151. digitalWrite(25, LOW);
  152. digitalWrite(24, HIGH);
  153. }
  154. //setup ende
  155. //--------------------------------------------------------------------------------------------------------------
  156. void loop() {
  157. //74HC165 read
  158. read_shift_regs();
  159. /* If there was a chage in state, display which ones changed.
  160. */
  161. for (int i = 0; i < DATA_WIDTH; i++) {
  162. if (shift[i] != shift_old[i])
  163. {
  164. display_pin_values();
  165. shift_old[i] = shift[i];
  166. }
  167. }
  168. readdata(); // Midi data read
  169. encoder(); // read encoder pos and send Midi data
  170. button(); // read button state and send Midi data
  171. if (millis() > millisold) {
  172. timedate();
  173. millisold = millis() + 10000;
  174. }
  175. //fine adjust
  176. adjust = digitalRead(22);
  177. if (adjust == HIGH && adjustold == LOW) {
  178. digitalWrite(23, HIGH);
  179. if (adjuststate == 1) {
  180. adjuststate = 0;
  181. digitalWrite(23, LOW);
  182. } else {
  183. adjuststate = 1;
  184. }
  185. adjustold = adjust;
  186. }
  187. if (adjust == LOW && adjustold == HIGH) {
  188. adjustold = adjust;
  189. }
  190. }
  191. // midi send data
  192. //--------------------------------------------------------------------------------------------------------------
  193. void MIDISEND(int controlChange, int controllerNummer, int controllerWert) {
  194. Serial1.write(controlChange);
  195. Serial1.write(controllerNummer);
  196. Serial1.write(controllerWert);
  197. setout(controllerWert, controllerNummer);
  198. }
  199. // Midi read data
  200. //--------------------------------------------------------------------------------------------------------------
  201. void readdata() {
  202. if (Serial1.available() > 2) {
  203. commandByte = Serial1.read();//read first byte
  204. noteByte = Serial1.read();//read next byte Channel 1-127
  205. velocityByte = Serial1.read();//read final byte value 0-127
  206. setin(velocityByte, noteByte);
  207. if (commandByte == noteOn) { //if note on message
  208. if (noteByte < 20) {
  209. //encoders
  210. for (int i = 0; i < encoders; i++) {
  211. int encodernum = i + 1;
  212. if (noteByte == encodernum) {
  213. encoderPosCount[i] = velocityByte;
  214. led[i] = map(velocityByte, 0, 127, 0, 8);
  215. shifterset(led[i], i);
  216. }
  217. }
  218. } else {
  219. // buttons
  220. for (int i = 0; i < buttons; i++) {
  221. if (noteByte == ButtonChannel[i]) {
  222. if (velocityByte == 127) {
  223. shifter1.set(i, HIGH); //turn on led
  224. } else {
  225. shifter1.set(i, LOW); //turn on led
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. // button config
  234. //--------------------------------------------------------------------------------------------------------------
  235. void button() {
  236. for (int i = 0; i < buttons; i++) {
  237. if (taste[i] == HIGH && tasteAlt[i] == LOW) {
  238. shifter1.set(i, HIGH);
  239. MIDISEND(noteOn, ButtonChannel[i], 127);
  240. tasteAlt[i] = taste[i];
  241. }
  242. if (taste[i] == LOW && tasteAlt[i] == HIGH) {
  243. shifter1.set(i, LOW);
  244. MIDISEND(noteOn, ButtonChannel[i], 0);
  245. tasteAlt[i] = taste[i];
  246. }
  247. }
  248. }
  249. // encoder config
  250. //--------------------------------------------------------------------------------------------------------------
  251. void encoder() {
  252. for (int e_num = 0; e_num < encoders; e_num++) {
  253. aVal[e_num] = pinA[e_num];
  254. if ((pinALast[e_num] == 0) && (aVal[e_num] == 1)) {
  255. if (pinB[e_num] == 0) {
  256. if (encoderPosCount[e_num] != 127) {
  257. if (adjuststate == 0) {
  258. encoderPosCount[e_num] = encoderPosCount[e_num] + 3;
  259. } else {
  260. encoderPosCount[e_num]++;
  261. }
  262. if (encoderPosCount[e_num] > 127) {
  263. encoderPosCount[e_num] = 127;
  264. }
  265. MIDISEND(noteOn, EncoderChannel[e_num], encoderPosCount[e_num]);
  266. led[e_num] = map(encoderPosCount[e_num], 0, 127, 0, 8);
  267. shifterset(led[e_num], e_num);
  268. }
  269. } else {
  270. if (encoderPosCount[e_num] != 0) {
  271. if (adjuststate == 0) {
  272. encoderPosCount[e_num] = encoderPosCount[e_num] - 3;
  273. } else {
  274. encoderPosCount[e_num]--;
  275. }
  276. if (encoderPosCount[e_num] < 0) {
  277. encoderPosCount[e_num] = 0;
  278. }
  279. MIDISEND(noteOn, EncoderChannel[e_num], encoderPosCount[e_num]);
  280. led[e_num] = map(encoderPosCount[e_num], 0, 127, 0, 8);
  281. shifterset(led[e_num], e_num);
  282. }
  283. }
  284. }
  285. pinALast[e_num] = aVal[e_num];
  286. }
  287. }
  288. //time date
  289. //--------------------------------------------------------------------------------------------------------------
  290. void timedate() {
  291. lcd.setCursor(0, 0);
  292. if (day() < 10) {
  293. lcd.print("0");
  294. lcd.print(day());
  295. } else {
  296. lcd.print(day());
  297. }
  298. lcd.print(".");
  299. lcd.setCursor(3, 0);
  300. if (month() < 10) {
  301. lcd.print("0");
  302. lcd.print(month());
  303. } else {
  304. lcd.print(month());
  305. }
  306. lcd.print(".");
  307. if (year() < 10) {
  308. lcd.print("0");
  309. lcd.print(year());
  310. } else {
  311. lcd.print(year());
  312. }
  313. lcd.setCursor(15, 0);
  314. if (hour() < 10) {
  315. lcd.print("0");
  316. lcd.print(hour());
  317. } else {
  318. lcd.print(hour());
  319. }
  320. lcd.print(":");
  321. if (minute() < 10) {
  322. lcd.print("0");
  323. lcd.print(minute());
  324. } else {
  325. lcd.print(minute());
  326. }
  327. }
  328. //display out
  329. //--------------------------------------------------------------------------------------------------------------
  330. void setout (int wert, int chan) {
  331. lcd.setCursor(7, 2);
  332. if (wert < 10) {
  333. lcd.print("00");
  334. lcd.print(wert);
  335. } else if (wert < 100) {
  336. lcd.print("0");
  337. lcd.print(wert);
  338. } else {
  339. lcd.print(wert);
  340. }
  341. lcd.setCursor(15, 2);
  342. if (chan < 10) {
  343. lcd.print("00");
  344. lcd.print(chan);
  345. } else if (chan < 100) {
  346. lcd.print("0");
  347. lcd.print(chan);
  348. } else {
  349. lcd.print(chan);
  350. }
  351. }
  352. //set input display
  353. //--------------------------------------------------------------------------------------------------------------
  354. void setin (int wert, int chan) {
  355. lcd.setCursor(7, 3);
  356. if (wert < 10) {
  357. lcd.print("00");
  358. lcd.print(wert);
  359. } else if (wert < 100) {
  360. lcd.print("0");
  361. lcd.print(wert);
  362. } else {
  363. lcd.print(wert);
  364. }
  365. lcd.setCursor(15, 3);
  366. if (chan < 10) {
  367. lcd.print("00");
  368. lcd.print(chan);
  369. } else if (chan < 100) {
  370. lcd.print("0");
  371. lcd.print(chan);
  372. } else {
  373. lcd.print(chan);
  374. }
  375. }
  376. //set encoder shift
  377. //--------------------------------------------------------------------------------------------------------------
  378. void shifterset(int ledval, int shifternum) {
  379. int ledout = ledval;
  380. int start = shifternum * 8;
  381. for (int i = 0; i < shifternum; i++) {
  382. ledout = ledout + 8;
  383. }
  384. for (int i = start; i < ledout; i++) {
  385. shifter0.set(i, HIGH);
  386. }
  387. if (led_state_old[shifternum] > ledout) {
  388. for (int i = ledout; i < led_state_old[shifternum]; i++) {
  389. shifter0.set(i, LOW);
  390. }
  391. }
  392. led_state_old[shifternum] = ledout;
  393. }
  394. //74HC165 funktion
  395. //--------------------------------------------------------------------------------------------------------------
  396. void read_shift_regs()
  397. {
  398. int bitVal;
  399. /* Trigger a parallel Load to latch the state of the data lines,
  400. */
  401. digitalWrite(clockEnablePin, HIGH);
  402. digitalWrite(ploadPin, LOW);
  403. delayMicroseconds(PULSE_WIDTH_USEC);
  404. digitalWrite(ploadPin, HIGH);
  405. digitalWrite(clockEnablePin, LOW);
  406. /* Loop to read each bit value from the serial out line
  407. * of the SN74HC165N.
  408. */
  409. for (int i = 0; i < DATA_WIDTH; i++)
  410. {
  411. bitVal = digitalRead(dataPin);
  412. /* Set the corresponding bit in bytesVal.
  413. */
  414. shift[DATA_WIDTH - 1 - i] = bitVal;
  415. /* Pulse the Clock (rising edge shifts the next bit).
  416. */
  417. digitalWrite(clockPin, HIGH);
  418. delayMicroseconds(PULSE_WIDTH_USEC);
  419. digitalWrite(clockPin, LOW);
  420. }
  421. }
  422. //--------------------------------------------------------------------------------------------------------------
  423. /* Dump the list of zones along with their current status.
  424. */
  425. void display_pin_values()
  426. {
  427. countA = 0;
  428. for (int i = 0; i < DATA_WIDTH; i++)
  429. {
  430. if (shift[i] == 1) {
  431. data = HIGH;
  432. } else {
  433. data = LOW;
  434. }
  435. if (i < encodernums) {
  436. if (count == 0) {
  437. pinA[countA / 2] = data;
  438. count = 1;
  439. } else {
  440. pinB[countA / 2] = data;
  441. count = 0;
  442. }
  443. countA++;
  444. } else {
  445. if (i == encodernums) {
  446. countA = 0;
  447. }
  448. if (countA < buttons) {
  449. taste[countA] = data;
  450. countA++;
  451. }
  452. }
  453. }
  454. }