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