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