Advertisement
Guest User

Auduino with working LCD readout

a guest
Nov 23rd, 2012
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.97 KB | None | 0 0
  1. // Auduino, the Lo-Fi granular synthesiser
  2. //
  3. // by Peter Knight, Tinker.it http://tinker.it
  4. // PROGMEM additions by Duane B http://rcarduino.blogspot.com
  5. // Additional scales, LCD, customization and tweaks by Patrick Enright
  6. //
  7. // Help: http://code.google.com/p/tinkerit/wiki/Auduino
  8. // More help: http://groups.google.com/group/auduino
  9. //
  10. // Analog in 0: Grain 1 pitch
  11. // Analog in 1: Grain 2 decay
  12. // Analog in 2: Grain 1 decay
  13. // Analog in 3: Grain 2 pitch
  14. // Analog in 4: Grain repetition frequency
  15. //
  16. // Digital 3: Audio out (Digital 11 on ATmega8)
  17. //
  18. // Changelog:
  19. // 19 Nov 2008: Added support for ATmega8 boards
  20. // 21 Mar 2009: Added support for ATmega328 boards
  21. // 7 Apr 2009: Fixed interrupt vector for ATmega328 boards
  22. // 8 Apr 2009: Added support for ATmega1280 boards (Arduino Mega)
  23.  
  24. #include <avr/io.h>
  25. #include <avr/interrupt.h>
  26. #include <LiquidCrystal.h>
  27. #include <Wire.h>
  28.  
  29. #define REDLITE 5
  30. #define GREENLITE 6
  31. #define BLUELITE 11
  32.  
  33. // initialize the library with the numbers of the interface pins
  34. LiquidCrystal lcd(0, 1, 4, 7, 8, 12);
  35.  
  36. // you can change the overall brightness by range 0 -> 255
  37. int brightness = 255;
  38.  
  39. int scalePin =5;
  40. int keyPin = 2; // This is shared with grain 1's decay pot. A board with more serial ports will be necessary for the final product
  41. //const char scaleName = "Scale Name";
  42.  
  43. uint16_t syncPhaseAcc;
  44. volatile uint16_t syncPhaseInc;
  45. uint16_t grainPhaseAcc;
  46. volatile uint16_t grainPhaseInc;
  47. uint16_t grainAmp;
  48. volatile uint8_t grainDecay;
  49. uint16_t grain2PhaseAcc;
  50. volatile uint16_t grain2PhaseInc;
  51. uint16_t grain2Amp;
  52. volatile uint8_t grain2Decay;
  53.  
  54. // Map Analogue channels
  55. #define SYNC_CONTROL (4)
  56. #define GRAIN_FREQ_CONTROL (0)
  57. #define GRAIN_DECAY_CONTROL (2)
  58. #define GRAIN2_FREQ_CONTROL (3)
  59. #define GRAIN2_DECAY_CONTROL (1)
  60.  
  61.  
  62. // Changing these will also requires rewriting audioOn()
  63.  
  64. #if defined(__AVR_ATmega8__)
  65. //
  66. // On old ATmega8 boards.
  67. // Output is on pin 11
  68. //
  69. #define LED_PIN 13
  70. #define LED_PORT PORTB
  71. #define LED_BIT 5
  72. #define PWM_PIN 11
  73. #define PWM_VALUE OCR2
  74. #define PWM_INTERRUPT TIMER2_OVF_vect
  75. #elif defined(__AVR_ATmega1280__)
  76. //
  77. // On the Arduino Mega
  78. // Output is on pin 3
  79. //
  80. #define LED_PIN 13
  81. #define LED_PORT PORTB
  82. #define LED_BIT 7
  83. #define PWM_PIN 3
  84. #define PWM_VALUE OCR3C
  85. #define PWM_INTERRUPT TIMER3_OVF_vect
  86. #else
  87. //
  88. // For modern ATmega168 and ATmega328 boards
  89. // Output is on pin 3
  90. //
  91. #define PWM_PIN 3
  92. #define PWM_VALUE OCR2B
  93. #define LED_PIN 13
  94. #define LED_PORT PORTB
  95. #define LED_BIT 5
  96. #define PWM_INTERRUPT TIMER2_OVF_vect
  97. #endif
  98.  
  99. #ifndef _NOTETABLES_
  100. #define _NOTETABLES_
  101.  
  102. #include "avr/pgmspace.h"
  103.  
  104. #define MIDI_NOTES 128
  105. // used to convert midi note numbers into the increments required to generate the note in the ISR
  106. PROGMEM unsigned int midiNoteToWavePhaseIncrement[MIDI_NOTES] =
  107. {
  108. 66 // C 0, 8.18,66.98,66 C
  109. ,70 // C# 1, 8.66,70.96,70
  110. ,75 // D 2, 9.18,75.18,75
  111. ,79 // D# 3, 9.72,79.65,79
  112. ,84 // E 4, 10.30,84.38,84
  113. ,89 // F 5, 10.91,89.40,89
  114. ,94 // F# 6, 11.56,94.72,94
  115. ,100 // G 7, 12.25,100.35,100
  116. ,106 // G# 8, 12.98,106.32,106
  117. ,112 // A 9, 13.75,112.64,112
  118. ,119 // A# 10, 14.57,119.34,119
  119. ,126 // B 11, 15.43,126.43,126
  120. ,133 // C0 12, 16.35,133.95,133 C0
  121. ,141 // C#0 13, 17.32,141.92,141
  122. ,150 // D0 14, 18.35,150.35,150
  123. ,159 // D#0 15, 19.45,159.29,159
  124. ,168 // E0 16, 20.60,168.77,168
  125. ,178 // F0 17, 21.83,178.80,178
  126. ,189 // F#0 18, 23.12,189.43,189
  127. ,200 // G0 19, 24.50,200.70,200
  128. ,212 // G#0 20, 25.96,212.63,212
  129. ,225 // A0 21, 27.50,225.28,225
  130. ,238 // A#0 22, 29.14,238.67,238
  131. ,252 // B0 23, 30.87,252.86,252
  132. ,267 // C1 24, 32.70,267.90,267 C1
  133. ,283 // C#1 25, 34.65,283.83,283
  134. ,300 // D1 26, 36.71,300.71,300
  135. ,318 // D#1 27, 38.89,318.59,318
  136. ,337 // E1 28, 41.20,337.53,337
  137. ,357 // F1 29, 43.65,357.60,357
  138. ,378 // F#1 30, 46.25,378.87,378
  139. ,401 // G1 31, 49.00,401.40,401
  140. ,425 // G#1 32, 51.91,425.27,425
  141. ,450 // A1 33, 55.00,450.55,450
  142. ,477 // A#1 34, 58.27,477.34,477
  143. ,505 // B1 35, 61.74,505.73,505
  144. ,535 // C2 36, 65.41,535.80,535 C2
  145. ,567 // C#2 37, 69.30,567.66,567
  146. ,601 // D2 38, 73.42,601.42,601
  147. ,637 // D#2 39, 77.78,637.18,637
  148. ,675 // E2 40, 82.41,675.07,675
  149. ,715 // F2 41, 87.31,715.21,715
  150. ,757 // F#2 42, 92.50,757.74,757
  151. ,802 // G2 43, 98.00,802.79,802
  152. ,850 // G#2 44, 103.83,850.53,850
  153. ,901 // A2 45, 110.00,901.11,901
  154. ,954 // A#2 46, 116.54,954.69,954
  155. ,1011 // B2 47, 123.47,1011.46,1011
  156. ,1071 // C3 48, 130.81,1071.60,1071 C3
  157. ,1135 // C#3 49, 138.59,1135.32,1135
  158. ,1202 // D3 50, 146.83,1202.83,1202
  159. ,1274 // D#3 51, 155.56,1274.36,1274
  160. ,1350 // E3 52, 164.81,1350.13,1350
  161. ,1430 // F3 53, 174.61,1430.42,1430
  162. ,1515 // F#3 54, 185.00,1515.47,1515
  163. ,1605 // G3 55, 196.00,1605.59,1605
  164. ,1701 // G#3 56, 207.65,1701.06,1701
  165. ,1802 // A3 57, 220.00,1802.21,1802
  166. ,1909 // A#3 58, 233.08,1909.38,1909
  167. ,2022 // B3 59, 246.94,2022.92,2022
  168. ,2143 // C4 60, 261.63,2143.20,2143 C4
  169. ,2270 // C#4 61, 277.18,2270.64,2270
  170. ,2405 // D4 62, 293.66,2405.66,2405
  171. ,2548 // D#4 63, 311.13,2548.71,2548
  172. ,2700 // E4 64, 329.63,2700.27,2700
  173. ,2860 // F4 65, 349.23,2860.83,2860
  174. ,3030 // F#4 66, 369.99,3030.95,3030
  175. ,3211 // G4 67, 392.00,3211.18,3211
  176. ,3402 // G#4 68, 415.30,3402.12,3402
  177. ,3604 // A4 69, 440.00,3604.42,3604
  178. ,3818 // A#4 70, 466.16,3818.75,3818
  179. ,4045 // B4 71, 493.88,4045.83,4045
  180. ,4286 // C5 72, 523.25,4286.41,4286 C5
  181. ,4541 // C#5 73, 554.37,4541.29,4541
  182. ,4811 // D5 74, 587.33,4811.33,4811
  183. ,5097 // D#5 75, 622.25,5097.42,5097
  184. ,5400 // E5 76, 659.26,5400.53,5400
  185. ,5721 // F5 77, 698.46,5721.67,5721
  186. ,6061 // F#5 78, 739.99,6061.89,6061
  187. ,6422 // G5 79, 783.99,6422.36,6422
  188. ,6804 // G#5 80, 830.61,6804.25,6804
  189. ,7208 // A5 81, 880.00,7208.85,7208
  190. ,7637 // A#5 82, 932.33,7637.51,7637
  191. ,8091 // B5 83, 987.77,8091.66,8091
  192. ,8572 // C6 84, 1046.50,8572.82,8572 C6
  193. ,9082 // C#6 85, 1108.73,9082.58,9082
  194. ,9622 // D6 86, 1174.66,9622.66,9622
  195. ,10194 // D#6 87, 1244.51,10194.85,10194
  196. ,10801 // E6 88, 1318.51,10801.07,10801
  197. ,11443 // F6 89, 1396.91,11443.33,11443
  198. ,12123 // F#6 90, 1479.98,12123.79,12123
  199. ,12844 // G6 91, 1567.98,12844.71,12844
  200. ,13608 // G#6 92, 1661.22,13608.50,13608
  201. ,14417 // A6 93, 1760.00,14417.70,14417
  202. ,15275 // A#6 94, 1864.65,15275.02,15275
  203. ,16183 // B6 95, 1975.53,16183.31,16183
  204. ,17145 // C7 96, 2093.00,17145.63,17145 C7
  205. ,18165 // C#7 97, 2217.46,18165.16,18165
  206. ,19245 // D7 98, 2349.32,19245.31,19245
  207. ,20389 // D#7 99, 2489.01,20389.70,20389
  208. ,21602 // E7 100, 2637.02,21602.14,21602
  209. ,22886 // F7 101, 2793.83,22886.67,22886
  210. ,24247 // F#7 102, 2959.95,24247.58,24247
  211. ,25689 // G7 103, 3135.96,25689.42,25689
  212. ,27216 // G#7 104, 3322.44,27216.99,27216
  213. ,28835 // A7 105, 3520.00,28835.39,28835
  214. ,30550 // A#7 106, 3729.31,30550.04,30550
  215. ,32366 // B7 107, 3951.06,32366.63,32366
  216. ,34291 // C8 108, 4186.01,34291.26,34291 C8
  217. ,36330 // C#8 109, 4434.92,36330.32,36330
  218. ,38490 // D8 110, 4698.64,38490.65,38490
  219. ,40779 // D#8 111, 4978.03,40779.41,40779
  220. ,43204 // E8 112, 5274.04,43204.25,43204
  221. ,45773 // F8 113, 5587.65,45773.32,45773
  222. ,48495 // F#8 114, 5919.91,48495.14,48495
  223. ,51378 // G8 115, 6271.92,51378.79,51378
  224. ,54433 // G#8 116, 6644.87,54433.96,54433
  225. ,57670 // A8 117, 7040.00,57670.76,57670
  226. ,61100 // A#8 118, 7458.62,61100.07,61100
  227. ,64733 // B8 119, 7902.13,64733.26,64733
  228. ,3046 // C9 120, 8372.02,68582.53,3046 C9
  229. ,7124 // C#9 121, 8869.84,72660.64,7124
  230. ,11445 // D9 122, 9397.27,76981.30,11445
  231. ,16022 // D#9 123, 9956.06,81558.77,16022
  232. ,20872 // E9 124, 10548.07,86408.50,20872
  233. ,26010 // F9 125, 11175.30,91546.65,26010
  234. ,31454 // F#9 126, 11839.81,96990.28,31454
  235. ,31454 // G9 127, 11839.81,96990.28,31454 // possibly incorrect, however no scale will access notes this high
  236. };
  237.  
  238. unsigned int getMidiNotePhaseIncrement(unsigned char sNote)
  239. {
  240. return pgm_read_word(midiNoteToWavePhaseIncrement + (sNote));
  241. }
  242.  
  243. // Major keys
  244. // Key of C ........C D E F G A B
  245. PROGMEM unsigned char majorC[29] = {
  246. 48, 50, 52, 53, 55, 57, 59,
  247. 60, 62, 64, 65, 67, 69, 71,
  248. 72, 74, 76, 77, 79, 81, 83,
  249. 84, 86, 88, 89, 91, 93, 95, 96
  250. };
  251.  
  252. unsigned int getMajorC(unsigned int nAnalogInput)
  253. {
  254. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  255. uint8_t sMidiIndex = pgm_read_byte(majorC + sPentatonicNote);
  256. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  257. }
  258.  
  259. // Key of G ........G A B C D E F#
  260. PROGMEM unsigned char majorG[29] = {
  261. 55, 57, 59, 48, 50, 52, 54,
  262. 67, 69, 71, 72, 74, 76, 78,
  263. 79, 81, 83, 84, 86, 88, 90,
  264. 91, 93, 95, 96, 98, 100, 102, 103,
  265. };
  266.  
  267. unsigned int getMajorG(unsigned int nAnalogInput)
  268. {
  269. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  270. uint8_t sMidiIndex = pgm_read_byte(majorG + sPentatonicNote);
  271. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  272. }
  273.  
  274. // Key of D ........D E F# G A B C#
  275. PROGMEM unsigned char majorD[29] = {
  276. 50, 52, 54, 55, 57, 59, 61,
  277. 62, 64, 66, 67, 69, 71, 73,
  278. 74, 76, 78, 79, 81, 83, 85,
  279. 86, 88, 90, 91, 93, 95, 97, 98,
  280. };
  281.  
  282. unsigned int getMajorD(unsigned int nAnalogInput)
  283. {
  284. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  285. uint8_t sMidiIndex = pgm_read_byte(majorD + sPentatonicNote);
  286. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  287. }
  288.  
  289. // Key of A ........A B C# D E F# G#
  290. PROGMEM unsigned char majorA[29] = {
  291. 57, 59, 61, 62, 64, 66, 68,
  292. 69, 71, 73, 74, 76, 78, 80,
  293. 81, 83, 85, 86, 88, 90, 92,
  294. 93, 95, 97, 98, 100, 102, 104, 105,
  295. };
  296.  
  297. unsigned int getMajorA(unsigned int nAnalogInput)
  298. {
  299. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  300. uint8_t sMidiIndex = pgm_read_byte(majorA + sPentatonicNote);
  301. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  302. }
  303.  
  304. // Key of E ........E F# G# A B C# D#
  305. PROGMEM unsigned char majorE[29] = {
  306. 52, 54, 56, 57, 59, 61, 63,
  307. 64, 66, 68, 69, 71, 73, 75,
  308. 76, 78, 80, 81, 83, 85, 87,
  309. 88, 90, 92, 93, 95, 97, 99, 100
  310. };
  311.  
  312. unsigned int getMajorE(unsigned int nAnalogInput)
  313. {
  314. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  315. uint8_t sMidiIndex = pgm_read_byte(majorE + sPentatonicNote);
  316. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  317. }
  318.  
  319. // Key of B ........B C# D# E F# G# A#
  320. PROGMEM unsigned char majorB[29] = {
  321. 59, 61, 63, 64, 66, 68, 70,
  322. 71, 73, 75, 76, 78, 80, 82,
  323. 83, 85, 87, 88, 90, 92, 94,
  324. 95, 97, 99, 100, 102, 104, 106, 107
  325. };
  326.  
  327. unsigned int getMajorB(unsigned int nAnalogInput)
  328. {
  329. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  330. uint8_t sMidiIndex = pgm_read_byte(majorB + sPentatonicNote);
  331. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  332. }
  333.  
  334. // Key of F# ........F# G# A# B C# D# E#
  335. PROGMEM unsigned char majorFs[29] = {
  336. 54, 56, 58, 59, 61, 63, 65,
  337. 66, 68, 70, 71, 73, 75, 77,
  338. 78, 80, 82, 83, 85, 87, 89,
  339. 90, 92, 94, 95, 97, 99, 101, 102,
  340. };
  341.  
  342. unsigned int getMajorFs(unsigned int nAnalogInput)
  343. {
  344. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  345. uint8_t sMidiIndex = pgm_read_byte(majorFs + sPentatonicNote);
  346. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  347. }
  348.  
  349. // Key of Db ........C# D# F F# G# A# C
  350. PROGMEM unsigned char majorDb[29] = {
  351. 49, 51, 53, 54, 56, 58, 48,
  352. 61, 63, 65, 66, 68, 70, 60,
  353. 73, 75, 77, 78, 80, 82, 72,
  354. 85, 87, 89, 90, 92, 94, 84, 97
  355. };
  356.  
  357. unsigned int getMajorDb(unsigned int nAnalogInput)
  358. {
  359. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  360. uint8_t sMidiIndex = pgm_read_byte(majorDb + sPentatonicNote);
  361. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  362. }
  363.  
  364. // Key of Ab ........G# A# C C# D# F G
  365. PROGMEM unsigned char majorAb[29] = {
  366. 56, 58, 48, 61, 63, 65, 67,
  367. 68, 70, 60, 73, 75, 77, 79,
  368. 80, 82, 72, 85, 87, 89, 91,
  369. 92, 94, 84, 97, 99, 101, 103, 104
  370. };
  371.  
  372. unsigned int getMajorAb(unsigned int nAnalogInput)
  373. {
  374. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  375. uint8_t sMidiIndex = pgm_read_byte(majorAb + sPentatonicNote);
  376. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  377. }
  378.  
  379. // Key of Eb ........D# F G G# A# C D
  380. PROGMEM unsigned char majorEb[29] = {
  381. 51, 53, 55, 56, 58, 48, 62,
  382. 63, 65, 67, 68, 70, 60, 74,
  383. 75, 77, 79, 80, 82, 72, 86,
  384. 87, 89, 91, 92, 94, 84, 98, 99
  385. };
  386.  
  387. unsigned int getMajorEb(unsigned int nAnalogInput)
  388. {
  389. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  390. uint8_t sMidiIndex = pgm_read_byte(majorEb + sPentatonicNote);
  391. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  392. }
  393.  
  394. // Key of Bb ........A# C D D# F G A
  395. PROGMEM unsigned char majorBb[29] = {
  396. 58, 48, 62, 63, 65, 67, 69,
  397. 70, 60, 74, 75, 77, 79, 81,
  398. 82, 72, 86, 87, 89, 91, 93,
  399. 94, 84, 98, 99, 101, 103, 105, 106
  400. };
  401.  
  402. unsigned int getMajorBb(unsigned int nAnalogInput)
  403. {
  404. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  405. uint8_t sMidiIndex = pgm_read_byte(majorBb + sPentatonicNote);
  406. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  407. }
  408.  
  409. // Key of F ........F G A A# C D E
  410. PROGMEM unsigned char majorF[29] = {
  411. 53, 55, 57, 58, 48, 62, 64,
  412. 65, 67, 69, 70, 60, 74, 76,
  413. 77, 79, 81, 82, 72, 86, 88,
  414. 89, 91, 93, 94, 84, 98, 100, 101
  415. };
  416.  
  417. unsigned int getMajorF(unsigned int nAnalogInput)
  418. {
  419. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
  420. uint8_t sMidiIndex = pgm_read_byte(majorF + sPentatonicNote);
  421. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  422. }
  423.  
  424. // Half Whole Diminished keys
  425. // Key of C ........C,C#,Eb,E,F#,G,A,A#
  426. PROGMEM unsigned char dimC[25] = {
  427. 48, 49, 51, 52, 54, 55, 57, 58,
  428. 60, 61, 63, 64, 66, 67, 69, 70,
  429. 72, 73, 75, 76, 78, 79, 81, 82, 84,
  430. };
  431.  
  432. unsigned int getDimC(unsigned int nAnalogInput)
  433. {
  434. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  435. uint8_t sMidiIndex = pgm_read_byte(dimC + sPentatonicNote);
  436. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  437. }
  438.  
  439. // Key of G ........G,G#,A#,B,C#,D,E,F
  440. PROGMEM unsigned char dimG[25] = {
  441. 55, 56, 58, 59, 61, 62, 64, 65,
  442. 67, 68, 70, 71, 73, 74, 76, 77,
  443. 79, 80, 82, 83, 85, 86, 88, 89, 91,
  444. };
  445.  
  446. unsigned int getDimG(unsigned int nAnalogInput)
  447. {
  448. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  449. uint8_t sMidiIndex = pgm_read_byte(dimG + sPentatonicNote);
  450. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  451. }
  452.  
  453. // Key of D ........D,Eb,F,F#,G#,A,B,C
  454. PROGMEM unsigned char dimD[25] = {
  455. 50, 51, 53, 54, 56, 57, 59, 60,
  456. 62, 63, 65, 66, 68, 69, 71, 72,
  457. 74, 75, 77, 78, 80, 81, 83, 84, 86,
  458. };
  459.  
  460. unsigned int getDimD(unsigned int nAnalogInput)
  461. {
  462. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  463. uint8_t sMidiIndex = pgm_read_byte(dimD + sPentatonicNote);
  464. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  465. }
  466.  
  467. // Key of A ........A,A#,C,C#,Eb,E,F#,G
  468. PROGMEM unsigned char dimA[25] = {
  469. 57, 58, 60, 61, 63, 64, 66, 67,
  470. 69, 70, 72, 73, 75, 76, 78, 79,
  471. 81, 82, 84, 85, 87, 88, 90, 91, 93,
  472. };
  473.  
  474. unsigned int getDimA(unsigned int nAnalogInput)
  475. {
  476. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  477. uint8_t sMidiIndex = pgm_read_byte(dimA + sPentatonicNote);
  478. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  479. }
  480.  
  481. // Key of E ........E,F,G,G#,A#,B,C#,D
  482. PROGMEM unsigned char dimE[25] = {
  483. 52, 53, 55, 56, 58, 59, 61, 62,
  484. 64, 65, 67, 68, 70, 71, 73, 74,
  485. 76, 77, 79, 80, 82, 83, 85, 86, 88,
  486. };
  487.  
  488. unsigned int getDimE(unsigned int nAnalogInput)
  489. {
  490. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  491. uint8_t sMidiIndex = pgm_read_byte(dimE + sPentatonicNote);
  492. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  493. }
  494.  
  495. // Key of B ........B,C,D,Eb,F,F#,G#,A
  496. PROGMEM unsigned char dimB[25] = {
  497. 59, 60, 62, 63, 65, 66, 68, 69,
  498. 71, 72, 74, 75, 77, 78, 80, 81,
  499. 83, 84, 86, 87, 89, 90, 92, 93, 93,
  500. };
  501.  
  502. unsigned int getDimB(unsigned int nAnalogInput)
  503. {
  504. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  505. uint8_t sMidiIndex = pgm_read_byte(dimB + sPentatonicNote);
  506. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  507. }
  508.  
  509. // Key of F# ........F#,G,A,A#,C,C#,Eb,E
  510. PROGMEM unsigned char dimFs[25] = {
  511. 54, 55, 57, 58, 60, 61, 63, 64,
  512. 66, 67, 69, 70, 72, 73, 75, 76,
  513. 78, 79, 81, 82, 84, 85, 87, 88, 90,
  514. };
  515.  
  516. unsigned int getDimFs(unsigned int nAnalogInput)
  517. {
  518. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  519. uint8_t sMidiIndex = pgm_read_byte(dimFs + sPentatonicNote);
  520. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  521. }
  522.  
  523. // Key of Db ........C#,D,E,F,G,G#,A#,B
  524. PROGMEM unsigned char dimDb[25] = {
  525. 49, 50, 52, 53, 55, 56, 58, 59,
  526. 61, 62, 64, 65, 67, 68, 70, 71,
  527. 73, 74, 76, 77, 79, 80, 82, 83, 85,
  528. };
  529.  
  530. unsigned int getDimDb(unsigned int nAnalogInput)
  531. {
  532. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  533. uint8_t sMidiIndex = pgm_read_byte(dimDb + sPentatonicNote);
  534. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  535. }
  536.  
  537. // Key of Ab ........G#,A,B,C,D,Eb,F,F#
  538. PROGMEM unsigned char dimAb[25] = {
  539. 56, 57, 59, 60, 62, 63, 65, 66,
  540. 68, 69, 71, 72, 74, 75, 77, 78,
  541. 80, 81, 83, 84, 86, 87, 89, 90, 92,
  542. };
  543.  
  544. unsigned int getDimAb(unsigned int nAnalogInput)
  545. {
  546. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  547. uint8_t sMidiIndex = pgm_read_byte(dimAb + sPentatonicNote);
  548. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  549. }
  550.  
  551. // Key of Eb ........Eb,E,F#,G,A,A#,C,C#
  552. PROGMEM unsigned char dimEb[25] = {
  553. 51, 52, 54, 55, 57, 58, 60, 61,
  554. 63, 64, 66, 67, 69, 70, 72, 73,
  555. 75, 76, 78, 79, 81, 82, 84, 85, 87,
  556. };
  557.  
  558. unsigned int getDimEb(unsigned int nAnalogInput)
  559. {
  560. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  561. uint8_t sMidiIndex = pgm_read_byte(dimEb + sPentatonicNote);
  562. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  563. }
  564.  
  565. // Key of Bb ........A#,B,C#,D,E,F,G,G#
  566. PROGMEM unsigned char dimBb[25] = {
  567. 58, 59, 61, 62, 64, 65, 67, 68,
  568. 70, 71, 73, 74, 76, 77, 79, 80,
  569. 82, 83, 85, 86, 88, 89, 91, 92, 94
  570. };
  571.  
  572. unsigned int getDimBb(unsigned int nAnalogInput)
  573. {
  574. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  575. uint8_t sMidiIndex = pgm_read_byte(dimBb + sPentatonicNote);
  576. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  577. }
  578.  
  579. // Key of F ........F,F#,G#,A,B,C,D,Eb
  580. PROGMEM unsigned char dimF[25] = {
  581. 53, 54, 56, 57, 59, 60, 62, 63,
  582. 65, 66, 68, 69, 71, 72, 74, 75,
  583. 77, 78, 80, 81, 83, 84, 86, 87, 89,
  584. };
  585.  
  586. unsigned int getDimF(unsigned int nAnalogInput)
  587. {
  588. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
  589. uint8_t sMidiIndex = pgm_read_byte(dimF + sPentatonicNote);
  590. return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
  591. }
  592. // Minor Pentatonic Keys
  593. // Key of C ........C,Eb,F,G,A#,C
  594. PROGMEM unsigned char pentC[26] = {
  595. 36, 39, 41, 43, 46,
  596. 48, 51, 53, 55, 58,
  597. 60, 63, 65, 67, 70,
  598. 72, 75, 77, 79, 82,
  599. 84, 87, 89, 91, 94, 96,
  600. };
  601.  
  602. unsigned int getPentC(unsigned int nAnalogInput)
  603. {
  604. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  605. uint8_t sMidiIndex = pgm_read_byte(pentC + sPentatonicNote);
  606. return pgm_read_word(midiNoteToWavePhaseIncrement +
  607.  
  608. sMidiIndex);
  609. }
  610.  
  611. // Key of G ........G,A#,C,D,F,G
  612. PROGMEM unsigned char pentG[26] = {
  613. 43, 46, 48, 50, 53,
  614. 55, 58, 60, 62, 65,
  615. 67, 70, 72, 74, 77,
  616. 79, 82, 84, 86, 89,
  617. 91, 94, 96, 98, 101, 103,
  618. };
  619.  
  620. unsigned int getPentG(unsigned int nAnalogInput)
  621. {
  622. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  623. uint8_t sMidiIndex = pgm_read_byte(pentG + sPentatonicNote);
  624. return pgm_read_word(midiNoteToWavePhaseIncrement +
  625.  
  626. sMidiIndex);
  627. }
  628.  
  629. // Key of D ........D,F,G,A,C,D
  630. PROGMEM unsigned char pentD[26] = {
  631. 38, 41, 43, 45, 48,
  632. 50, 53, 55, 57, 60,
  633. 62, 65, 67, 69, 72,
  634. 74, 77, 79, 81, 84,
  635. 86, 89, 91, 93, 96, 98,
  636. };
  637.  
  638. unsigned int getPentD(unsigned int nAnalogInput)
  639. {
  640. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  641. uint8_t sMidiIndex = pgm_read_byte(pentD + sPentatonicNote);
  642. return pgm_read_word(midiNoteToWavePhaseIncrement +
  643.  
  644. sMidiIndex);
  645. }
  646.  
  647. // Key of A ........A,C,D,E,G,A
  648. PROGMEM unsigned char pentA[26] = {
  649. 45, 48, 50, 52, 55,
  650. 57, 60, 62, 64, 67,
  651. 69, 72, 74, 76, 79,
  652. 81, 84, 86, 88, 91,
  653. 93, 96, 98, 100, 103, 105,
  654. };
  655.  
  656. unsigned int getPentA(unsigned int nAnalogInput)
  657. {
  658. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  659. uint8_t sMidiIndex = pgm_read_byte(pentA + sPentatonicNote);
  660. return pgm_read_word(midiNoteToWavePhaseIncrement +
  661.  
  662. sMidiIndex);
  663. }
  664.  
  665. // Key of E ........E,G,A,B,D,E
  666. PROGMEM unsigned char pentE[26] = {
  667. 40, 43, 45, 47, 50,
  668. 52, 55, 57, 59, 62,
  669. 64, 67, 69, 71, 74,
  670. 76, 79, 81, 83, 86,
  671. 88, 91, 93, 95, 98, 100,
  672. };
  673.  
  674. unsigned int getPentE(unsigned int nAnalogInput)
  675. {
  676. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  677. uint8_t sMidiIndex = pgm_read_byte(pentE + sPentatonicNote);
  678. return pgm_read_word(midiNoteToWavePhaseIncrement +
  679.  
  680. sMidiIndex);
  681. }
  682.  
  683. // Key of B ........B,D,E,F#,A,
  684. PROGMEM unsigned char pentB[26] = {
  685. 47, 50, 52, 54, 57,
  686. 59, 62, 64, 66, 69,
  687. 71, 74, 76, 78, 81,
  688. 83, 86, 88, 90, 93,
  689. 95, 98, 100, 102, 105, 107,
  690. };
  691.  
  692. unsigned int getPentB(unsigned int nAnalogInput)
  693. {
  694. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  695. uint8_t sMidiIndex = pgm_read_byte(pentB + sPentatonicNote);
  696. return pgm_read_word(midiNoteToWavePhaseIncrement +
  697.  
  698. sMidiIndex);
  699. }
  700.  
  701. // Key of F# ........F#,A,B,C#,E,F#
  702. PROGMEM unsigned char pentFs[26] = {
  703. 42, 45, 47, 49, 52,
  704. 54, 57, 59, 61, 64,
  705. 66, 69, 71, 73, 76,
  706. 78, 81, 83, 85, 88,
  707. 90, 93, 95, 97, 100, 102,
  708. };
  709.  
  710. unsigned int getPentFs(unsigned int nAnalogInput)
  711. {
  712. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  713. uint8_t sMidiIndex = pgm_read_byte(pentFs + sPentatonicNote);
  714. return pgm_read_word(midiNoteToWavePhaseIncrement +
  715.  
  716. sMidiIndex);
  717. }
  718.  
  719. // Key of Db ........C#,E,F#,G#,B,C#
  720. PROGMEM unsigned char pentDb[26] = {
  721. 37, 40, 42, 44, 47,
  722. 49, 52, 54, 56, 59,
  723. 61, 64, 66, 68, 71,
  724. 73, 76, 78, 80, 83,
  725. 85, 88, 90, 92, 95, 97,
  726. };
  727.  
  728. unsigned int getPentDb(unsigned int nAnalogInput)
  729. {
  730. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  731. uint8_t sMidiIndex = pgm_read_byte(pentDb + sPentatonicNote);
  732. return pgm_read_word(midiNoteToWavePhaseIncrement +
  733.  
  734. sMidiIndex);
  735. }
  736.  
  737. // Key of Ab ........G#,B,C#,Eb,F#,G#
  738. PROGMEM unsigned char pentAb[26] = {
  739. 44, 47, 49, 51, 54,
  740. 56, 59, 61, 63, 66,
  741. 68, 71, 73, 75, 78,
  742. 80, 83, 85, 87, 90,
  743. 92, 95, 97, 99, 102, 104,
  744. };
  745.  
  746. unsigned int getPentAb(unsigned int nAnalogInput)
  747. {
  748. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  749. uint8_t sMidiIndex = pgm_read_byte(pentAb + sPentatonicNote);
  750. return pgm_read_word(midiNoteToWavePhaseIncrement +
  751.  
  752. sMidiIndex);
  753. }
  754.  
  755. // Key of Eb ........Eb,F#,G#,A#,C#,Eb
  756. PROGMEM unsigned char pentEb[26] = {
  757. 39, 42, 44, 46, 49,
  758. 51, 66, 68, 70, 61,
  759. 63, 78, 80, 82, 73,
  760. 75, 78, 92, 94, 85,
  761. 87, 90, 92, 94, 97, 99
  762. };
  763.  
  764. unsigned int getPentEb(unsigned int nAnalogInput)
  765. {
  766. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  767. uint8_t sMidiIndex = pgm_read_byte(pentEb + sPentatonicNote);
  768. return pgm_read_word(midiNoteToWavePhaseIncrement +
  769.  
  770. sMidiIndex);
  771. }
  772.  
  773. // Key of Bb ........A#,C#,Eb,F,G#,A#
  774. PROGMEM unsigned char pentBb[26] = {
  775. 46, 49, 51, 53, 56,
  776. 58, 61, 63, 65, 68,
  777. 70, 73, 75, 77, 80,
  778. 82, 85, 87, 89, 92,
  779. 94, 97, 99, 101, 104, 106,
  780. };
  781.  
  782. unsigned int getPentBb(unsigned int nAnalogInput)
  783. {
  784. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  785. uint8_t sMidiIndex = pgm_read_byte(pentBb + sPentatonicNote);
  786. return pgm_read_word(midiNoteToWavePhaseIncrement +
  787.  
  788. sMidiIndex);
  789. }
  790.  
  791. // Key of F ........F,G#,A#,C,Eb,F
  792. PROGMEM unsigned char pentF[26] = {
  793. 41, 44, 46, 48, 51,
  794. 53, 56, 58, 60, 63,
  795. 65, 68, 70, 72, 75,
  796. 77, 80, 82, 84, 87,
  797. 89, 92, 94, 96, 99, 101,
  798. };
  799.  
  800. unsigned int getPentF(unsigned int nAnalogInput)
  801. {
  802. uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
  803. uint8_t sMidiIndex = pgm_read_byte(pentF + sPentatonicNote);
  804. return pgm_read_word(midiNoteToWavePhaseIncrement +
  805.  
  806. sMidiIndex);
  807. }
  808. #endif
  809.  
  810. // Smooth logarithmic mapping
  811. //
  812. uint16_t antilogTable[] = {
  813. 64830,64132,63441,62757,62081,61413,60751,60097,59449,58809,58176,57549,56929,56316,55709,55109,
  814. 54515,53928,53347,52773,52204,51642,51085,50535,49991,49452,48920,48393,47871,47356,46846,46341,
  815. 45842,45348,44859,44376,43898,43425,42958,42495,42037,41584,41136,40693,40255,39821,39392,38968,
  816. 38548,38133,37722,37316,36914,36516,36123,35734,35349,34968,34591,34219,33850,33486,33125,32768
  817. };
  818. uint16_t mapPhaseInc(uint16_t input) {
  819. return (antilogTable[input & 0x3f]) >> (input >> 6);
  820. }
  821.  
  822. void audioOn() {
  823. #if defined(__AVR_ATmega8__)
  824. // ATmega8 has different registers
  825. TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
  826. TIMSK = _BV(TOIE2);
  827. #elif defined(__AVR_ATmega1280__)
  828. TCCR3A = _BV(COM3C1) | _BV(WGM30);
  829. TCCR3B = _BV(CS30);
  830. TIMSK3 = _BV(TOIE3);
  831. #else
  832. // Set up PWM to 31.25kHz, phase accurate
  833. TCCR2A = _BV(COM2B1) | _BV(WGM20);
  834. TCCR2B = _BV(CS20);
  835. TIMSK2 = _BV(TOIE2);
  836. #endif
  837. }
  838.  
  839.  
  840. void setup() {
  841. pinMode(PWM_PIN,OUTPUT);
  842. audioOn();
  843. pinMode(LED_PIN,OUTPUT);
  844.  
  845. // set up the LCD's number of rows and columns:
  846. lcd.begin(16, 2);
  847. // Print a message to the LCD.
  848. //lcd.print("Auduino Granular");
  849. //lcd.setCursor(0,1);
  850. //lcd.print(" Synthesizer");
  851. pinMode(REDLITE, OUTPUT);
  852. pinMode(GREENLITE, OUTPUT);
  853. pinMode(BLUELITE, OUTPUT);
  854.  
  855. brightness = 100;
  856.  
  857. }
  858.  
  859. void loop() {
  860. // The loop is pretty simple - it just updates the parameters for the oscillators.
  861. //
  862. // Avoid using any functions that make extensive use of interrupts, or turn interrupts off.
  863. // They will cause clicks and poops in the audio.
  864. /*
  865. for (int i = 0; i < 255; i++) {
  866. setBacklight(i, 0, 255-i);
  867. delayMicroseconds(50);
  868. }
  869.  
  870. for (int i = 0; i < 255; i++) {
  871. setBacklight(255-i, i, 0);
  872. delayMicroseconds(50);
  873. }
  874. for (int i = 0; i < 255; i++) {
  875. setBacklight(0, 255-i, i);
  876. delayMicroseconds(50);
  877. }
  878. */
  879. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  880. // Reads the scale select pin, converts input to 3 choices.+
  881. // Reads the converted input to select a scale from the +
  882. // corresponding case. This then reads from the key pin +
  883. // and selects from a choice of 12 keys from a nested case.+
  884. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  885.  
  886. int scale = analogRead(scalePin);
  887. scale = constrain(scale, 0, 1023);
  888. int scaleSelect = map(scale, 0, 1024, 0, 3);
  889.  
  890. int key = analogRead(keyPin);
  891. key = constrain(key, 0, 1023);
  892. int keySelect = map(key, 0, 1024, 0, 11);
  893.  
  894. switch (scaleSelect) {
  895. case 0:
  896. switch (keySelect) {
  897. case 0:
  898. syncPhaseInc = getMajorC(analogRead(SYNC_CONTROL));
  899. lcd.setCursor(0, 1);
  900. lcd.print("C Major/a Minor ");
  901. break;
  902. case 1:
  903. syncPhaseInc = getMajorG(analogRead(SYNC_CONTROL));
  904. lcd.setCursor(0, 1);
  905. lcd.print("G Major/e Minor ");
  906. break;
  907. case 2:
  908. syncPhaseInc = getMajorD(analogRead(SYNC_CONTROL));
  909. lcd.setCursor(0, 1);
  910. lcd.print("D Major/b Minor ");
  911. break;
  912. case 3:
  913. syncPhaseInc = getMajorA(analogRead(SYNC_CONTROL));
  914. lcd.setCursor(0, 1);
  915. lcd.print("A Major/f# Minor");
  916. break;
  917. case 4:
  918. syncPhaseInc = getMajorE(analogRead(SYNC_CONTROL));
  919. lcd.setCursor(0, 1);
  920. lcd.print("E Major/c# Minor");
  921. break;
  922. case 5:
  923. syncPhaseInc = getMajorB(analogRead(SYNC_CONTROL));
  924. lcd.setCursor(0, 1);
  925. lcd.print("B Major/g# Minor");
  926. break;
  927. case 6:
  928. syncPhaseInc = getMajorFs(analogRead(SYNC_CONTROL));
  929. lcd.setCursor(0, 1);
  930. lcd.print("F# Maj / d# Min ");
  931. break;
  932. case 7:
  933. syncPhaseInc = getMajorDb(analogRead(SYNC_CONTROL));
  934. lcd.setCursor(0, 1);
  935. lcd.print("Db Maj / bb Min ");
  936. break;
  937. case 8:
  938. syncPhaseInc = getMajorAb(analogRead(SYNC_CONTROL));
  939. lcd.setCursor(0, 1);
  940. lcd.print("Ab Major/f Minor");
  941. break;
  942. case 9:
  943. syncPhaseInc = getMajorEb(analogRead(SYNC_CONTROL));
  944. lcd.setCursor(0, 1);
  945. lcd.print("Eb Major/c Minor");
  946. break;
  947. case 10:
  948. syncPhaseInc = getMajorBb(analogRead(SYNC_CONTROL));
  949. lcd.setCursor(0, 1);
  950. lcd.print("Bb Major/g Minor");
  951. break;
  952. case 11:
  953. syncPhaseInc = getMajorF(analogRead(SYNC_CONTROL));
  954. lcd.setCursor(0, 1);
  955. lcd.print("F Major/d Minor ");
  956. break;
  957. }
  958. break;
  959. case 1:
  960. switch (keySelect) {
  961. case 0:
  962. syncPhaseInc = getDimC(analogRead(SYNC_CONTROL));
  963. lcd.setCursor(0, 1);
  964. lcd.print("Diminished C ");
  965. break;
  966. case 1:
  967. syncPhaseInc = getDimG(analogRead(SYNC_CONTROL));
  968. lcd.setCursor(0, 1);
  969. lcd.print("Diminished G ");
  970. break;
  971. case 2:
  972. syncPhaseInc = getDimD(analogRead(SYNC_CONTROL));
  973. lcd.setCursor(0, 1);
  974. lcd.print("Diminished D ");
  975. break;
  976. case 3:
  977. syncPhaseInc = getDimA(analogRead(SYNC_CONTROL));
  978. lcd.setCursor(0, 1);
  979. lcd.print("Diminished A ");
  980. break;
  981. case 4:
  982. syncPhaseInc = getDimE(analogRead(SYNC_CONTROL));
  983. lcd.setCursor(0, 1);
  984. lcd.print("Diminished E ");
  985. break;
  986. case 5:
  987. syncPhaseInc = getDimB(analogRead(SYNC_CONTROL));
  988. lcd.setCursor(0, 1);
  989. lcd.print("Diminished B ");
  990. break;
  991. case 6:
  992. syncPhaseInc = getDimFs(analogRead(SYNC_CONTROL));
  993. lcd.setCursor(0, 1);
  994. lcd.print("Diminished F# ");
  995. break;
  996. case 7:
  997. syncPhaseInc = getDimDb(analogRead(SYNC_CONTROL));
  998. lcd.setCursor(0, 1);
  999. lcd.print("Diminished Db ");
  1000. break;
  1001. case 8:
  1002. syncPhaseInc = getDimAb(analogRead(SYNC_CONTROL));
  1003. lcd.setCursor(0, 1);
  1004. lcd.print("Diminished Ab ");
  1005. break;
  1006. case 9:
  1007. syncPhaseInc = getDimEb(analogRead(SYNC_CONTROL));
  1008. lcd.setCursor(0, 1);
  1009. lcd.print("Diminished Eb ");
  1010. break;
  1011. case 10:
  1012. syncPhaseInc = getDimBb(analogRead(SYNC_CONTROL));
  1013. lcd.setCursor(0, 1);
  1014. lcd.print("Diminished Bb ");
  1015. break;
  1016. case 11:
  1017. syncPhaseInc = getDimF(analogRead(SYNC_CONTROL));
  1018. lcd.setCursor(0, 1);
  1019. lcd.print("Diminished F ");
  1020. break;
  1021. }
  1022. break;
  1023. case 2:
  1024. switch (keySelect) {
  1025. case 0:
  1026. syncPhaseInc = getPentC(analogRead(SYNC_CONTROL));
  1027. lcd.setCursor(0, 1);
  1028. lcd.print("C Pent Minor ");
  1029. break;
  1030. case 1:
  1031. syncPhaseInc = getPentG(analogRead(SYNC_CONTROL));
  1032. lcd.setCursor(0, 1);
  1033. lcd.print("G Pent Minor ");
  1034. break;
  1035. case 2:
  1036. syncPhaseInc = getPentD(analogRead(SYNC_CONTROL));
  1037. lcd.setCursor(0, 1);
  1038. lcd.print("D Pent Minor ");
  1039. break;
  1040. case 3:
  1041. syncPhaseInc = getPentA(analogRead(SYNC_CONTROL));
  1042. lcd.setCursor(0, 1);
  1043. lcd.print("A Pent Minor ");
  1044. break;
  1045. case 4:
  1046. syncPhaseInc = getPentE(analogRead(SYNC_CONTROL));
  1047. lcd.setCursor(0, 1);
  1048. lcd.print("E Pent Minor ");
  1049. break;
  1050. case 5:
  1051. syncPhaseInc = getPentB(analogRead(SYNC_CONTROL));
  1052. lcd.setCursor(0, 1);
  1053. lcd.print("B Pent Minor ");
  1054. break;
  1055. case 6:
  1056. syncPhaseInc = getPentFs(analogRead(SYNC_CONTROL));
  1057. lcd.setCursor(0, 1);
  1058. lcd.print("F# Pent Minor ");
  1059. break;
  1060. case 7:
  1061. syncPhaseInc = getPentDb(analogRead(SYNC_CONTROL));
  1062. lcd.setCursor(0, 1);
  1063. lcd.print("Db Pent Minor ");
  1064. break;
  1065. case 8:
  1066. syncPhaseInc = getPentAb(analogRead(SYNC_CONTROL));
  1067. lcd.setCursor(0, 1);
  1068. lcd.print("Ab Pent Minor ");
  1069. break;
  1070. case 9:
  1071. syncPhaseInc = getPentEb(analogRead(SYNC_CONTROL));
  1072. lcd.setCursor(0, 1);
  1073. lcd.print("Eb Pent Minor ");
  1074. break;
  1075. case 10:
  1076. syncPhaseInc = getPentBb(analogRead(SYNC_CONTROL));
  1077. lcd.setCursor(0, 1);
  1078. lcd.print("Bb Pent Minor ");
  1079. break;
  1080. case 11:
  1081. syncPhaseInc = getPentF(analogRead(SYNC_CONTROL));
  1082. lcd.setCursor(0, 1);
  1083. lcd.print("F Pent Minor ");
  1084. break;
  1085. }
  1086. break;
  1087. }
  1088.  
  1089. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1090. // Once each time through the loop syncPhaseInc, which +
  1091. // dictates the currently playing note, is read and its +
  1092. // value is compared to possible values for each note. +
  1093. // This is displayed on the LCD so the player knows what +
  1094. // the hell they're actually playing. +
  1095. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1096.  
  1097. if (syncPhaseInc == 1071||syncPhaseInc == 2143 || syncPhaseInc == 4286 || syncPhaseInc == 8572 || syncPhaseInc == 17145){
  1098. lcd.setCursor(0, 0);
  1099. lcd.print("Current Note: C");
  1100. }
  1101. else if (syncPhaseInc == 1135 || syncPhaseInc == 2270 || syncPhaseInc == 4541 || syncPhaseInc == 9082 || syncPhaseInc == 18165){
  1102. lcd.setCursor(0, 0);
  1103. lcd.print("Current Note: C#");
  1104. }
  1105. else if (syncPhaseInc == 1202 || syncPhaseInc == 2405 || syncPhaseInc == 4811 || syncPhaseInc == 9622 || syncPhaseInc == 19245){
  1106. lcd.setCursor(0, 0);
  1107. lcd.print("Current Note: D");
  1108. }
  1109. else if (syncPhaseInc == 1274 || syncPhaseInc == 2548 || syncPhaseInc == 5097 || syncPhaseInc == 10194 || syncPhaseInc == 20389){
  1110. lcd.setCursor(0, 0);
  1111. lcd.print("Current Note: D#");
  1112. }
  1113. else if (syncPhaseInc == 1350 || syncPhaseInc == 2700 || syncPhaseInc == 5400 || syncPhaseInc == 10801 || syncPhaseInc == 21602){
  1114. lcd.setCursor(0, 0);
  1115. lcd.print("Current Note: E");
  1116. }
  1117. else if (syncPhaseInc == 1430 || syncPhaseInc == 2860 || syncPhaseInc == 5721 || syncPhaseInc == 11443 || syncPhaseInc == 22886){
  1118. lcd.setCursor(0, 0);
  1119. lcd.print("Current Note: F");
  1120. }
  1121. else if (syncPhaseInc == 1515 || syncPhaseInc == 3030 || syncPhaseInc == 6061 || syncPhaseInc == 12123 || syncPhaseInc == 24247){
  1122. lcd.setCursor(0, 0);
  1123. lcd.print("Current Note: F#");
  1124. }
  1125. else if (syncPhaseInc == 1605 || syncPhaseInc == 3211 || syncPhaseInc == 6422 || syncPhaseInc == 12844 || syncPhaseInc == 25689){
  1126. lcd.setCursor(0, 0);
  1127. lcd.print("Current Note: G");
  1128. }
  1129. else if (syncPhaseInc == 1701 || syncPhaseInc == 3402 || syncPhaseInc == 6804 || syncPhaseInc == 13608 || syncPhaseInc == 27216){
  1130. lcd.setCursor(0, 0);
  1131. lcd.print("Current Note: G#");
  1132. }
  1133. else if (syncPhaseInc == 1802 || syncPhaseInc == 3604 || syncPhaseInc == 7208 || syncPhaseInc == 14417 || syncPhaseInc == 28835){
  1134. lcd.setCursor(0, 0);
  1135. lcd.print("Current Note: A");
  1136. }
  1137. else if (syncPhaseInc == 1909 || syncPhaseInc == 3818 || syncPhaseInc == 7637 || syncPhaseInc == 15275 || syncPhaseInc == 30550){
  1138. lcd.setCursor(0, 0);
  1139. lcd.print("Current Note: A#");
  1140. }
  1141. else if (syncPhaseInc == 2022 || syncPhaseInc == 4045 || syncPhaseInc == 8091 || syncPhaseInc == 16183 || syncPhaseInc == 32366){
  1142. lcd.setCursor(0, 0);
  1143. lcd.print("Current Note: B");
  1144. }
  1145.  
  1146. grainPhaseInc = mapPhaseInc(analogRead(GRAIN_FREQ_CONTROL)) / 2;
  1147. grainDecay = analogRead(GRAIN_DECAY_CONTROL) / 8;
  1148. grain2PhaseInc = mapPhaseInc(analogRead(GRAIN2_FREQ_CONTROL)) / 2;
  1149. grain2Decay = analogRead(GRAIN2_DECAY_CONTROL) / 4;
  1150. }
  1151.  
  1152. SIGNAL(PWM_INTERRUPT)
  1153. {
  1154. uint8_t value;
  1155. uint16_t output;
  1156.  
  1157. syncPhaseAcc += syncPhaseInc;
  1158. if (syncPhaseAcc < syncPhaseInc) {
  1159. // Time to start the next grain
  1160. grainPhaseAcc = 0;
  1161. grainAmp = 0x7fff;
  1162. grain2PhaseAcc = 0;
  1163. grain2Amp = 0x7fff;
  1164. LED_PORT ^= 1 << LED_BIT; // Faster than using digitalWrite
  1165. }
  1166.  
  1167. // Increment the phase of the grain oscillators
  1168. grainPhaseAcc += grainPhaseInc;
  1169. grain2PhaseAcc += grain2PhaseInc;
  1170.  
  1171. // Convert phase into a triangle wave
  1172. value = (grainPhaseAcc >> 7) & 0xff;
  1173. if (grainPhaseAcc & 0x8000) value = ~value;
  1174. // Multiply by current grain amplitude to get sample
  1175. output = value * (grainAmp >> 8);
  1176.  
  1177. // Repeat for second grain
  1178. value = (grain2PhaseAcc >> 7) & 0xff;
  1179. if (grain2PhaseAcc & 0x8000) value = ~value;
  1180. output += value * (grain2Amp >> 8);
  1181.  
  1182. // Make the grain amplitudes decay by a factor every sample (exponential decay)
  1183. grainAmp -= (grainAmp >> 8) * grainDecay;
  1184. grain2Amp -= (grain2Amp >> 8) * grain2Decay;
  1185.  
  1186. // Scale output to the available range, clipping if necessary
  1187. output >>= 9;
  1188. if (output > 255) output = 255;
  1189.  
  1190. // Output to PWM (this is faster than using analogWrite)
  1191. PWM_VALUE = output;
  1192.  
  1193. }
  1194.  
  1195. void setBacklight(uint8_t red, uint8_t green , uint8_t blue) {
  1196. // normalize the red LED - its brighter than the rest!
  1197. red = map(red, 0, 255, 0, 100);
  1198. green = map(green, 0, 255, 0, 150);
  1199.  
  1200. red = map(red, 0, 255, 0, brightness);
  1201. green = map(green, 0, 255, 0, brightness);
  1202. blue = map(blue, 0, 255, 0, brightness);
  1203.  
  1204. // common anode so invert!
  1205. red = map(red, 0, 255, 255, 0);
  1206. green = map(green, 0, 255, 255, 0);
  1207. blue = map(blue, 0, 255, 255, 0);
  1208. Serial.print("R = "); Serial.print(red, DEC);
  1209. Serial.print(" G = "); Serial.print(green, DEC);
  1210. Serial.print(" B = "); Serial.println(blue, DEC);
  1211. analogWrite(REDLITE, red);
  1212. analogWrite(GREENLITE, green);
  1213. analogWrite(BLUELITE, blue);
  1214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement