Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Auduino, the Lo-Fi granular synthesiser
- //
- // by Peter Knight, Tinker.it http://tinker.it
- // PROGMEM additions by Duane B http://rcarduino.blogspot.com
- // Additional scales, LCD, customization and tweaks by Patrick Enright
- //
- // Help: http://code.google.com/p/tinkerit/wiki/Auduino
- // More help: http://groups.google.com/group/auduino
- //
- // Analog in 0: Grain 1 pitch
- // Analog in 1: Grain 2 decay
- // Analog in 2: Grain 1 decay
- // Analog in 3: Grain 2 pitch
- // Analog in 4: Grain repetition frequency
- //
- // Digital 3: Audio out (Digital 11 on ATmega8)
- //
- // Changelog:
- // 19 Nov 2008: Added support for ATmega8 boards
- // 21 Mar 2009: Added support for ATmega328 boards
- // 7 Apr 2009: Fixed interrupt vector for ATmega328 boards
- // 8 Apr 2009: Added support for ATmega1280 boards (Arduino Mega)
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <LiquidCrystal.h>
- #include <Wire.h>
- #define REDLITE 5
- #define GREENLITE 6
- #define BLUELITE 11
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(0, 1, 4, 7, 8, 12);
- // you can change the overall brightness by range 0 -> 255
- int brightness = 255;
- int scalePin =5;
- 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
- //const char scaleName = "Scale Name";
- uint16_t syncPhaseAcc;
- volatile uint16_t syncPhaseInc;
- uint16_t grainPhaseAcc;
- volatile uint16_t grainPhaseInc;
- uint16_t grainAmp;
- volatile uint8_t grainDecay;
- uint16_t grain2PhaseAcc;
- volatile uint16_t grain2PhaseInc;
- uint16_t grain2Amp;
- volatile uint8_t grain2Decay;
- // Map Analogue channels
- #define SYNC_CONTROL (4)
- #define GRAIN_FREQ_CONTROL (0)
- #define GRAIN_DECAY_CONTROL (2)
- #define GRAIN2_FREQ_CONTROL (3)
- #define GRAIN2_DECAY_CONTROL (1)
- // Changing these will also requires rewriting audioOn()
- #if defined(__AVR_ATmega8__)
- //
- // On old ATmega8 boards.
- // Output is on pin 11
- //
- #define LED_PIN 13
- #define LED_PORT PORTB
- #define LED_BIT 5
- #define PWM_PIN 11
- #define PWM_VALUE OCR2
- #define PWM_INTERRUPT TIMER2_OVF_vect
- #elif defined(__AVR_ATmega1280__)
- //
- // On the Arduino Mega
- // Output is on pin 3
- //
- #define LED_PIN 13
- #define LED_PORT PORTB
- #define LED_BIT 7
- #define PWM_PIN 3
- #define PWM_VALUE OCR3C
- #define PWM_INTERRUPT TIMER3_OVF_vect
- #else
- //
- // For modern ATmega168 and ATmega328 boards
- // Output is on pin 3
- //
- #define PWM_PIN 3
- #define PWM_VALUE OCR2B
- #define LED_PIN 13
- #define LED_PORT PORTB
- #define LED_BIT 5
- #define PWM_INTERRUPT TIMER2_OVF_vect
- #endif
- #ifndef _NOTETABLES_
- #define _NOTETABLES_
- #include "avr/pgmspace.h"
- #define MIDI_NOTES 128
- // used to convert midi note numbers into the increments required to generate the note in the ISR
- PROGMEM unsigned int midiNoteToWavePhaseIncrement[MIDI_NOTES] =
- {
- 66 // C 0, 8.18,66.98,66 C
- ,70 // C# 1, 8.66,70.96,70
- ,75 // D 2, 9.18,75.18,75
- ,79 // D# 3, 9.72,79.65,79
- ,84 // E 4, 10.30,84.38,84
- ,89 // F 5, 10.91,89.40,89
- ,94 // F# 6, 11.56,94.72,94
- ,100 // G 7, 12.25,100.35,100
- ,106 // G# 8, 12.98,106.32,106
- ,112 // A 9, 13.75,112.64,112
- ,119 // A# 10, 14.57,119.34,119
- ,126 // B 11, 15.43,126.43,126
- ,133 // C0 12, 16.35,133.95,133 C0
- ,141 // C#0 13, 17.32,141.92,141
- ,150 // D0 14, 18.35,150.35,150
- ,159 // D#0 15, 19.45,159.29,159
- ,168 // E0 16, 20.60,168.77,168
- ,178 // F0 17, 21.83,178.80,178
- ,189 // F#0 18, 23.12,189.43,189
- ,200 // G0 19, 24.50,200.70,200
- ,212 // G#0 20, 25.96,212.63,212
- ,225 // A0 21, 27.50,225.28,225
- ,238 // A#0 22, 29.14,238.67,238
- ,252 // B0 23, 30.87,252.86,252
- ,267 // C1 24, 32.70,267.90,267 C1
- ,283 // C#1 25, 34.65,283.83,283
- ,300 // D1 26, 36.71,300.71,300
- ,318 // D#1 27, 38.89,318.59,318
- ,337 // E1 28, 41.20,337.53,337
- ,357 // F1 29, 43.65,357.60,357
- ,378 // F#1 30, 46.25,378.87,378
- ,401 // G1 31, 49.00,401.40,401
- ,425 // G#1 32, 51.91,425.27,425
- ,450 // A1 33, 55.00,450.55,450
- ,477 // A#1 34, 58.27,477.34,477
- ,505 // B1 35, 61.74,505.73,505
- ,535 // C2 36, 65.41,535.80,535 C2
- ,567 // C#2 37, 69.30,567.66,567
- ,601 // D2 38, 73.42,601.42,601
- ,637 // D#2 39, 77.78,637.18,637
- ,675 // E2 40, 82.41,675.07,675
- ,715 // F2 41, 87.31,715.21,715
- ,757 // F#2 42, 92.50,757.74,757
- ,802 // G2 43, 98.00,802.79,802
- ,850 // G#2 44, 103.83,850.53,850
- ,901 // A2 45, 110.00,901.11,901
- ,954 // A#2 46, 116.54,954.69,954
- ,1011 // B2 47, 123.47,1011.46,1011
- ,1071 // C3 48, 130.81,1071.60,1071 C3
- ,1135 // C#3 49, 138.59,1135.32,1135
- ,1202 // D3 50, 146.83,1202.83,1202
- ,1274 // D#3 51, 155.56,1274.36,1274
- ,1350 // E3 52, 164.81,1350.13,1350
- ,1430 // F3 53, 174.61,1430.42,1430
- ,1515 // F#3 54, 185.00,1515.47,1515
- ,1605 // G3 55, 196.00,1605.59,1605
- ,1701 // G#3 56, 207.65,1701.06,1701
- ,1802 // A3 57, 220.00,1802.21,1802
- ,1909 // A#3 58, 233.08,1909.38,1909
- ,2022 // B3 59, 246.94,2022.92,2022
- ,2143 // C4 60, 261.63,2143.20,2143 C4
- ,2270 // C#4 61, 277.18,2270.64,2270
- ,2405 // D4 62, 293.66,2405.66,2405
- ,2548 // D#4 63, 311.13,2548.71,2548
- ,2700 // E4 64, 329.63,2700.27,2700
- ,2860 // F4 65, 349.23,2860.83,2860
- ,3030 // F#4 66, 369.99,3030.95,3030
- ,3211 // G4 67, 392.00,3211.18,3211
- ,3402 // G#4 68, 415.30,3402.12,3402
- ,3604 // A4 69, 440.00,3604.42,3604
- ,3818 // A#4 70, 466.16,3818.75,3818
- ,4045 // B4 71, 493.88,4045.83,4045
- ,4286 // C5 72, 523.25,4286.41,4286 C5
- ,4541 // C#5 73, 554.37,4541.29,4541
- ,4811 // D5 74, 587.33,4811.33,4811
- ,5097 // D#5 75, 622.25,5097.42,5097
- ,5400 // E5 76, 659.26,5400.53,5400
- ,5721 // F5 77, 698.46,5721.67,5721
- ,6061 // F#5 78, 739.99,6061.89,6061
- ,6422 // G5 79, 783.99,6422.36,6422
- ,6804 // G#5 80, 830.61,6804.25,6804
- ,7208 // A5 81, 880.00,7208.85,7208
- ,7637 // A#5 82, 932.33,7637.51,7637
- ,8091 // B5 83, 987.77,8091.66,8091
- ,8572 // C6 84, 1046.50,8572.82,8572 C6
- ,9082 // C#6 85, 1108.73,9082.58,9082
- ,9622 // D6 86, 1174.66,9622.66,9622
- ,10194 // D#6 87, 1244.51,10194.85,10194
- ,10801 // E6 88, 1318.51,10801.07,10801
- ,11443 // F6 89, 1396.91,11443.33,11443
- ,12123 // F#6 90, 1479.98,12123.79,12123
- ,12844 // G6 91, 1567.98,12844.71,12844
- ,13608 // G#6 92, 1661.22,13608.50,13608
- ,14417 // A6 93, 1760.00,14417.70,14417
- ,15275 // A#6 94, 1864.65,15275.02,15275
- ,16183 // B6 95, 1975.53,16183.31,16183
- ,17145 // C7 96, 2093.00,17145.63,17145 C7
- ,18165 // C#7 97, 2217.46,18165.16,18165
- ,19245 // D7 98, 2349.32,19245.31,19245
- ,20389 // D#7 99, 2489.01,20389.70,20389
- ,21602 // E7 100, 2637.02,21602.14,21602
- ,22886 // F7 101, 2793.83,22886.67,22886
- ,24247 // F#7 102, 2959.95,24247.58,24247
- ,25689 // G7 103, 3135.96,25689.42,25689
- ,27216 // G#7 104, 3322.44,27216.99,27216
- ,28835 // A7 105, 3520.00,28835.39,28835
- ,30550 // A#7 106, 3729.31,30550.04,30550
- ,32366 // B7 107, 3951.06,32366.63,32366
- ,34291 // C8 108, 4186.01,34291.26,34291 C8
- ,36330 // C#8 109, 4434.92,36330.32,36330
- ,38490 // D8 110, 4698.64,38490.65,38490
- ,40779 // D#8 111, 4978.03,40779.41,40779
- ,43204 // E8 112, 5274.04,43204.25,43204
- ,45773 // F8 113, 5587.65,45773.32,45773
- ,48495 // F#8 114, 5919.91,48495.14,48495
- ,51378 // G8 115, 6271.92,51378.79,51378
- ,54433 // G#8 116, 6644.87,54433.96,54433
- ,57670 // A8 117, 7040.00,57670.76,57670
- ,61100 // A#8 118, 7458.62,61100.07,61100
- ,64733 // B8 119, 7902.13,64733.26,64733
- ,3046 // C9 120, 8372.02,68582.53,3046 C9
- ,7124 // C#9 121, 8869.84,72660.64,7124
- ,11445 // D9 122, 9397.27,76981.30,11445
- ,16022 // D#9 123, 9956.06,81558.77,16022
- ,20872 // E9 124, 10548.07,86408.50,20872
- ,26010 // F9 125, 11175.30,91546.65,26010
- ,31454 // F#9 126, 11839.81,96990.28,31454
- ,31454 // G9 127, 11839.81,96990.28,31454 // possibly incorrect, however no scale will access notes this high
- };
- unsigned int getMidiNotePhaseIncrement(unsigned char sNote)
- {
- return pgm_read_word(midiNoteToWavePhaseIncrement + (sNote));
- }
- // Major keys
- // Key of C ........C D E F G A B
- PROGMEM unsigned char majorC[29] = {
- 48, 50, 52, 53, 55, 57, 59,
- 60, 62, 64, 65, 67, 69, 71,
- 72, 74, 76, 77, 79, 81, 83,
- 84, 86, 88, 89, 91, 93, 95, 96
- };
- unsigned int getMajorC(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorC + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of G ........G A B C D E F#
- PROGMEM unsigned char majorG[29] = {
- 55, 57, 59, 48, 50, 52, 54,
- 67, 69, 71, 72, 74, 76, 78,
- 79, 81, 83, 84, 86, 88, 90,
- 91, 93, 95, 96, 98, 100, 102, 103,
- };
- unsigned int getMajorG(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorG + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of D ........D E F# G A B C#
- PROGMEM unsigned char majorD[29] = {
- 50, 52, 54, 55, 57, 59, 61,
- 62, 64, 66, 67, 69, 71, 73,
- 74, 76, 78, 79, 81, 83, 85,
- 86, 88, 90, 91, 93, 95, 97, 98,
- };
- unsigned int getMajorD(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorD + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of A ........A B C# D E F# G#
- PROGMEM unsigned char majorA[29] = {
- 57, 59, 61, 62, 64, 66, 68,
- 69, 71, 73, 74, 76, 78, 80,
- 81, 83, 85, 86, 88, 90, 92,
- 93, 95, 97, 98, 100, 102, 104, 105,
- };
- unsigned int getMajorA(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorA + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of E ........E F# G# A B C# D#
- PROGMEM unsigned char majorE[29] = {
- 52, 54, 56, 57, 59, 61, 63,
- 64, 66, 68, 69, 71, 73, 75,
- 76, 78, 80, 81, 83, 85, 87,
- 88, 90, 92, 93, 95, 97, 99, 100
- };
- unsigned int getMajorE(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorE + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of B ........B C# D# E F# G# A#
- PROGMEM unsigned char majorB[29] = {
- 59, 61, 63, 64, 66, 68, 70,
- 71, 73, 75, 76, 78, 80, 82,
- 83, 85, 87, 88, 90, 92, 94,
- 95, 97, 99, 100, 102, 104, 106, 107
- };
- unsigned int getMajorB(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorB + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F# ........F# G# A# B C# D# E#
- PROGMEM unsigned char majorFs[29] = {
- 54, 56, 58, 59, 61, 63, 65,
- 66, 68, 70, 71, 73, 75, 77,
- 78, 80, 82, 83, 85, 87, 89,
- 90, 92, 94, 95, 97, 99, 101, 102,
- };
- unsigned int getMajorFs(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorFs + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Db ........C# D# F F# G# A# C
- PROGMEM unsigned char majorDb[29] = {
- 49, 51, 53, 54, 56, 58, 48,
- 61, 63, 65, 66, 68, 70, 60,
- 73, 75, 77, 78, 80, 82, 72,
- 85, 87, 89, 90, 92, 94, 84, 97
- };
- unsigned int getMajorDb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorDb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Ab ........G# A# C C# D# F G
- PROGMEM unsigned char majorAb[29] = {
- 56, 58, 48, 61, 63, 65, 67,
- 68, 70, 60, 73, 75, 77, 79,
- 80, 82, 72, 85, 87, 89, 91,
- 92, 94, 84, 97, 99, 101, 103, 104
- };
- unsigned int getMajorAb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorAb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Eb ........D# F G G# A# C D
- PROGMEM unsigned char majorEb[29] = {
- 51, 53, 55, 56, 58, 48, 62,
- 63, 65, 67, 68, 70, 60, 74,
- 75, 77, 79, 80, 82, 72, 86,
- 87, 89, 91, 92, 94, 84, 98, 99
- };
- unsigned int getMajorEb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorEb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Bb ........A# C D D# F G A
- PROGMEM unsigned char majorBb[29] = {
- 58, 48, 62, 63, 65, 67, 69,
- 70, 60, 74, 75, 77, 79, 81,
- 82, 72, 86, 87, 89, 91, 93,
- 94, 84, 98, 99, 101, 103, 105, 106
- };
- unsigned int getMajorBb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorBb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F ........F G A A# C D E
- PROGMEM unsigned char majorF[29] = {
- 53, 55, 57, 58, 48, 62, 64,
- 65, 67, 69, 70, 60, 74, 76,
- 77, 79, 81, 82, 72, 86, 88,
- 89, 91, 93, 94, 84, 98, 100, 101
- };
- unsigned int getMajorF(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorF + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Half Whole Diminished keys
- // Key of C ........C,C#,Eb,E,F#,G,A,A#
- PROGMEM unsigned char dimC[25] = {
- 48, 49, 51, 52, 54, 55, 57, 58,
- 60, 61, 63, 64, 66, 67, 69, 70,
- 72, 73, 75, 76, 78, 79, 81, 82, 84,
- };
- unsigned int getDimC(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimC + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of G ........G,G#,A#,B,C#,D,E,F
- PROGMEM unsigned char dimG[25] = {
- 55, 56, 58, 59, 61, 62, 64, 65,
- 67, 68, 70, 71, 73, 74, 76, 77,
- 79, 80, 82, 83, 85, 86, 88, 89, 91,
- };
- unsigned int getDimG(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimG + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of D ........D,Eb,F,F#,G#,A,B,C
- PROGMEM unsigned char dimD[25] = {
- 50, 51, 53, 54, 56, 57, 59, 60,
- 62, 63, 65, 66, 68, 69, 71, 72,
- 74, 75, 77, 78, 80, 81, 83, 84, 86,
- };
- unsigned int getDimD(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimD + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of A ........A,A#,C,C#,Eb,E,F#,G
- PROGMEM unsigned char dimA[25] = {
- 57, 58, 60, 61, 63, 64, 66, 67,
- 69, 70, 72, 73, 75, 76, 78, 79,
- 81, 82, 84, 85, 87, 88, 90, 91, 93,
- };
- unsigned int getDimA(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimA + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of E ........E,F,G,G#,A#,B,C#,D
- PROGMEM unsigned char dimE[25] = {
- 52, 53, 55, 56, 58, 59, 61, 62,
- 64, 65, 67, 68, 70, 71, 73, 74,
- 76, 77, 79, 80, 82, 83, 85, 86, 88,
- };
- unsigned int getDimE(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimE + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of B ........B,C,D,Eb,F,F#,G#,A
- PROGMEM unsigned char dimB[25] = {
- 59, 60, 62, 63, 65, 66, 68, 69,
- 71, 72, 74, 75, 77, 78, 80, 81,
- 83, 84, 86, 87, 89, 90, 92, 93, 93,
- };
- unsigned int getDimB(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimB + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F# ........F#,G,A,A#,C,C#,Eb,E
- PROGMEM unsigned char dimFs[25] = {
- 54, 55, 57, 58, 60, 61, 63, 64,
- 66, 67, 69, 70, 72, 73, 75, 76,
- 78, 79, 81, 82, 84, 85, 87, 88, 90,
- };
- unsigned int getDimFs(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimFs + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Db ........C#,D,E,F,G,G#,A#,B
- PROGMEM unsigned char dimDb[25] = {
- 49, 50, 52, 53, 55, 56, 58, 59,
- 61, 62, 64, 65, 67, 68, 70, 71,
- 73, 74, 76, 77, 79, 80, 82, 83, 85,
- };
- unsigned int getDimDb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimDb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Ab ........G#,A,B,C,D,Eb,F,F#
- PROGMEM unsigned char dimAb[25] = {
- 56, 57, 59, 60, 62, 63, 65, 66,
- 68, 69, 71, 72, 74, 75, 77, 78,
- 80, 81, 83, 84, 86, 87, 89, 90, 92,
- };
- unsigned int getDimAb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimAb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Eb ........Eb,E,F#,G,A,A#,C,C#
- PROGMEM unsigned char dimEb[25] = {
- 51, 52, 54, 55, 57, 58, 60, 61,
- 63, 64, 66, 67, 69, 70, 72, 73,
- 75, 76, 78, 79, 81, 82, 84, 85, 87,
- };
- unsigned int getDimEb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimEb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Bb ........A#,B,C#,D,E,F,G,G#
- PROGMEM unsigned char dimBb[25] = {
- 58, 59, 61, 62, 64, 65, 67, 68,
- 70, 71, 73, 74, 76, 77, 79, 80,
- 82, 83, 85, 86, 88, 89, 91, 92, 94
- };
- unsigned int getDimBb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimBb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F ........F,F#,G#,A,B,C,D,Eb
- PROGMEM unsigned char dimF[25] = {
- 53, 54, 56, 57, 59, 60, 62, 63,
- 65, 66, 68, 69, 71, 72, 74, 75,
- 77, 78, 80, 81, 83, 84, 86, 87, 89,
- };
- unsigned int getDimF(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,25);
- uint8_t sMidiIndex = pgm_read_byte(dimF + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Minor Pentatonic Keys
- // Key of C ........C,Eb,F,G,A#,C
- PROGMEM unsigned char pentC[26] = {
- 36, 39, 41, 43, 46,
- 48, 51, 53, 55, 58,
- 60, 63, 65, 67, 70,
- 72, 75, 77, 79, 82,
- 84, 87, 89, 91, 94, 96,
- };
- unsigned int getPentC(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentC + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of G ........G,A#,C,D,F,G
- PROGMEM unsigned char pentG[26] = {
- 43, 46, 48, 50, 53,
- 55, 58, 60, 62, 65,
- 67, 70, 72, 74, 77,
- 79, 82, 84, 86, 89,
- 91, 94, 96, 98, 101, 103,
- };
- unsigned int getPentG(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentG + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of D ........D,F,G,A,C,D
- PROGMEM unsigned char pentD[26] = {
- 38, 41, 43, 45, 48,
- 50, 53, 55, 57, 60,
- 62, 65, 67, 69, 72,
- 74, 77, 79, 81, 84,
- 86, 89, 91, 93, 96, 98,
- };
- unsigned int getPentD(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentD + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of A ........A,C,D,E,G,A
- PROGMEM unsigned char pentA[26] = {
- 45, 48, 50, 52, 55,
- 57, 60, 62, 64, 67,
- 69, 72, 74, 76, 79,
- 81, 84, 86, 88, 91,
- 93, 96, 98, 100, 103, 105,
- };
- unsigned int getPentA(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentA + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of E ........E,G,A,B,D,E
- PROGMEM unsigned char pentE[26] = {
- 40, 43, 45, 47, 50,
- 52, 55, 57, 59, 62,
- 64, 67, 69, 71, 74,
- 76, 79, 81, 83, 86,
- 88, 91, 93, 95, 98, 100,
- };
- unsigned int getPentE(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentE + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of B ........B,D,E,F#,A,
- PROGMEM unsigned char pentB[26] = {
- 47, 50, 52, 54, 57,
- 59, 62, 64, 66, 69,
- 71, 74, 76, 78, 81,
- 83, 86, 88, 90, 93,
- 95, 98, 100, 102, 105, 107,
- };
- unsigned int getPentB(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentB + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of F# ........F#,A,B,C#,E,F#
- PROGMEM unsigned char pentFs[26] = {
- 42, 45, 47, 49, 52,
- 54, 57, 59, 61, 64,
- 66, 69, 71, 73, 76,
- 78, 81, 83, 85, 88,
- 90, 93, 95, 97, 100, 102,
- };
- unsigned int getPentFs(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentFs + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of Db ........C#,E,F#,G#,B,C#
- PROGMEM unsigned char pentDb[26] = {
- 37, 40, 42, 44, 47,
- 49, 52, 54, 56, 59,
- 61, 64, 66, 68, 71,
- 73, 76, 78, 80, 83,
- 85, 88, 90, 92, 95, 97,
- };
- unsigned int getPentDb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentDb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of Ab ........G#,B,C#,Eb,F#,G#
- PROGMEM unsigned char pentAb[26] = {
- 44, 47, 49, 51, 54,
- 56, 59, 61, 63, 66,
- 68, 71, 73, 75, 78,
- 80, 83, 85, 87, 90,
- 92, 95, 97, 99, 102, 104,
- };
- unsigned int getPentAb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentAb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of Eb ........Eb,F#,G#,A#,C#,Eb
- PROGMEM unsigned char pentEb[26] = {
- 39, 42, 44, 46, 49,
- 51, 66, 68, 70, 61,
- 63, 78, 80, 82, 73,
- 75, 78, 92, 94, 85,
- 87, 90, 92, 94, 97, 99
- };
- unsigned int getPentEb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentEb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of Bb ........A#,C#,Eb,F,G#,A#
- PROGMEM unsigned char pentBb[26] = {
- 46, 49, 51, 53, 56,
- 58, 61, 63, 65, 68,
- 70, 73, 75, 77, 80,
- 82, 85, 87, 89, 92,
- 94, 97, 99, 101, 104, 106,
- };
- unsigned int getPentBb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentBb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- // Key of F ........F,G#,A#,C,Eb,F
- PROGMEM unsigned char pentF[26] = {
- 41, 44, 46, 48, 51,
- 53, 56, 58, 60, 63,
- 65, 68, 70, 72, 75,
- 77, 80, 82, 84, 87,
- 89, 92, 94, 96, 99, 101,
- };
- unsigned int getPentF(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,26);
- uint8_t sMidiIndex = pgm_read_byte(pentF + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement +
- sMidiIndex);
- }
- #endif
- // Smooth logarithmic mapping
- //
- uint16_t antilogTable[] = {
- 64830,64132,63441,62757,62081,61413,60751,60097,59449,58809,58176,57549,56929,56316,55709,55109,
- 54515,53928,53347,52773,52204,51642,51085,50535,49991,49452,48920,48393,47871,47356,46846,46341,
- 45842,45348,44859,44376,43898,43425,42958,42495,42037,41584,41136,40693,40255,39821,39392,38968,
- 38548,38133,37722,37316,36914,36516,36123,35734,35349,34968,34591,34219,33850,33486,33125,32768
- };
- uint16_t mapPhaseInc(uint16_t input) {
- return (antilogTable[input & 0x3f]) >> (input >> 6);
- }
- void audioOn() {
- #if defined(__AVR_ATmega8__)
- // ATmega8 has different registers
- TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
- TIMSK = _BV(TOIE2);
- #elif defined(__AVR_ATmega1280__)
- TCCR3A = _BV(COM3C1) | _BV(WGM30);
- TCCR3B = _BV(CS30);
- TIMSK3 = _BV(TOIE3);
- #else
- // Set up PWM to 31.25kHz, phase accurate
- TCCR2A = _BV(COM2B1) | _BV(WGM20);
- TCCR2B = _BV(CS20);
- TIMSK2 = _BV(TOIE2);
- #endif
- }
- void setup() {
- pinMode(PWM_PIN,OUTPUT);
- audioOn();
- pinMode(LED_PIN,OUTPUT);
- // set up the LCD's number of rows and columns:
- lcd.begin(16, 2);
- // Print a message to the LCD.
- //lcd.print("Auduino Granular");
- //lcd.setCursor(0,1);
- //lcd.print(" Synthesizer");
- pinMode(REDLITE, OUTPUT);
- pinMode(GREENLITE, OUTPUT);
- pinMode(BLUELITE, OUTPUT);
- brightness = 100;
- }
- void loop() {
- // The loop is pretty simple - it just updates the parameters for the oscillators.
- //
- // Avoid using any functions that make extensive use of interrupts, or turn interrupts off.
- // They will cause clicks and poops in the audio.
- /*
- for (int i = 0; i < 255; i++) {
- setBacklight(i, 0, 255-i);
- delayMicroseconds(50);
- }
- for (int i = 0; i < 255; i++) {
- setBacklight(255-i, i, 0);
- delayMicroseconds(50);
- }
- for (int i = 0; i < 255; i++) {
- setBacklight(0, 255-i, i);
- delayMicroseconds(50);
- }
- */
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- // Reads the scale select pin, converts input to 3 choices.+
- // Reads the converted input to select a scale from the +
- // corresponding case. This then reads from the key pin +
- // and selects from a choice of 12 keys from a nested case.+
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- int scale = analogRead(scalePin);
- scale = constrain(scale, 0, 1023);
- int scaleSelect = map(scale, 0, 1024, 0, 3);
- int key = analogRead(keyPin);
- key = constrain(key, 0, 1023);
- int keySelect = map(key, 0, 1024, 0, 11);
- switch (scaleSelect) {
- case 0:
- switch (keySelect) {
- case 0:
- syncPhaseInc = getMajorC(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("C Major/a Minor ");
- break;
- case 1:
- syncPhaseInc = getMajorG(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("G Major/e Minor ");
- break;
- case 2:
- syncPhaseInc = getMajorD(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("D Major/b Minor ");
- break;
- case 3:
- syncPhaseInc = getMajorA(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("A Major/f# Minor");
- break;
- case 4:
- syncPhaseInc = getMajorE(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("E Major/c# Minor");
- break;
- case 5:
- syncPhaseInc = getMajorB(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("B Major/g# Minor");
- break;
- case 6:
- syncPhaseInc = getMajorFs(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("F# Maj / d# Min ");
- break;
- case 7:
- syncPhaseInc = getMajorDb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Db Maj / bb Min ");
- break;
- case 8:
- syncPhaseInc = getMajorAb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Ab Major/f Minor");
- break;
- case 9:
- syncPhaseInc = getMajorEb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Eb Major/c Minor");
- break;
- case 10:
- syncPhaseInc = getMajorBb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Bb Major/g Minor");
- break;
- case 11:
- syncPhaseInc = getMajorF(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("F Major/d Minor ");
- break;
- }
- break;
- case 1:
- switch (keySelect) {
- case 0:
- syncPhaseInc = getDimC(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished C ");
- break;
- case 1:
- syncPhaseInc = getDimG(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished G ");
- break;
- case 2:
- syncPhaseInc = getDimD(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished D ");
- break;
- case 3:
- syncPhaseInc = getDimA(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished A ");
- break;
- case 4:
- syncPhaseInc = getDimE(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished E ");
- break;
- case 5:
- syncPhaseInc = getDimB(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished B ");
- break;
- case 6:
- syncPhaseInc = getDimFs(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished F# ");
- break;
- case 7:
- syncPhaseInc = getDimDb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished Db ");
- break;
- case 8:
- syncPhaseInc = getDimAb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished Ab ");
- break;
- case 9:
- syncPhaseInc = getDimEb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished Eb ");
- break;
- case 10:
- syncPhaseInc = getDimBb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished Bb ");
- break;
- case 11:
- syncPhaseInc = getDimF(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Diminished F ");
- break;
- }
- break;
- case 2:
- switch (keySelect) {
- case 0:
- syncPhaseInc = getPentC(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("C Pent Minor ");
- break;
- case 1:
- syncPhaseInc = getPentG(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("G Pent Minor ");
- break;
- case 2:
- syncPhaseInc = getPentD(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("D Pent Minor ");
- break;
- case 3:
- syncPhaseInc = getPentA(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("A Pent Minor ");
- break;
- case 4:
- syncPhaseInc = getPentE(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("E Pent Minor ");
- break;
- case 5:
- syncPhaseInc = getPentB(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("B Pent Minor ");
- break;
- case 6:
- syncPhaseInc = getPentFs(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("F# Pent Minor ");
- break;
- case 7:
- syncPhaseInc = getPentDb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Db Pent Minor ");
- break;
- case 8:
- syncPhaseInc = getPentAb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Ab Pent Minor ");
- break;
- case 9:
- syncPhaseInc = getPentEb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Eb Pent Minor ");
- break;
- case 10:
- syncPhaseInc = getPentBb(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("Bb Pent Minor ");
- break;
- case 11:
- syncPhaseInc = getPentF(analogRead(SYNC_CONTROL));
- lcd.setCursor(0, 1);
- lcd.print("F Pent Minor ");
- break;
- }
- break;
- }
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- // Once each time through the loop syncPhaseInc, which +
- // dictates the currently playing note, is read and its +
- // value is compared to possible values for each note. +
- // This is displayed on the LCD so the player knows what +
- // the hell they're actually playing. +
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- if (syncPhaseInc == 1071||syncPhaseInc == 2143 || syncPhaseInc == 4286 || syncPhaseInc == 8572 || syncPhaseInc == 17145){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: C");
- }
- else if (syncPhaseInc == 1135 || syncPhaseInc == 2270 || syncPhaseInc == 4541 || syncPhaseInc == 9082 || syncPhaseInc == 18165){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: C#");
- }
- else if (syncPhaseInc == 1202 || syncPhaseInc == 2405 || syncPhaseInc == 4811 || syncPhaseInc == 9622 || syncPhaseInc == 19245){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: D");
- }
- else if (syncPhaseInc == 1274 || syncPhaseInc == 2548 || syncPhaseInc == 5097 || syncPhaseInc == 10194 || syncPhaseInc == 20389){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: D#");
- }
- else if (syncPhaseInc == 1350 || syncPhaseInc == 2700 || syncPhaseInc == 5400 || syncPhaseInc == 10801 || syncPhaseInc == 21602){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: E");
- }
- else if (syncPhaseInc == 1430 || syncPhaseInc == 2860 || syncPhaseInc == 5721 || syncPhaseInc == 11443 || syncPhaseInc == 22886){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: F");
- }
- else if (syncPhaseInc == 1515 || syncPhaseInc == 3030 || syncPhaseInc == 6061 || syncPhaseInc == 12123 || syncPhaseInc == 24247){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: F#");
- }
- else if (syncPhaseInc == 1605 || syncPhaseInc == 3211 || syncPhaseInc == 6422 || syncPhaseInc == 12844 || syncPhaseInc == 25689){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: G");
- }
- else if (syncPhaseInc == 1701 || syncPhaseInc == 3402 || syncPhaseInc == 6804 || syncPhaseInc == 13608 || syncPhaseInc == 27216){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: G#");
- }
- else if (syncPhaseInc == 1802 || syncPhaseInc == 3604 || syncPhaseInc == 7208 || syncPhaseInc == 14417 || syncPhaseInc == 28835){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: A");
- }
- else if (syncPhaseInc == 1909 || syncPhaseInc == 3818 || syncPhaseInc == 7637 || syncPhaseInc == 15275 || syncPhaseInc == 30550){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: A#");
- }
- else if (syncPhaseInc == 2022 || syncPhaseInc == 4045 || syncPhaseInc == 8091 || syncPhaseInc == 16183 || syncPhaseInc == 32366){
- lcd.setCursor(0, 0);
- lcd.print("Current Note: B");
- }
- grainPhaseInc = mapPhaseInc(analogRead(GRAIN_FREQ_CONTROL)) / 2;
- grainDecay = analogRead(GRAIN_DECAY_CONTROL) / 8;
- grain2PhaseInc = mapPhaseInc(analogRead(GRAIN2_FREQ_CONTROL)) / 2;
- grain2Decay = analogRead(GRAIN2_DECAY_CONTROL) / 4;
- }
- SIGNAL(PWM_INTERRUPT)
- {
- uint8_t value;
- uint16_t output;
- syncPhaseAcc += syncPhaseInc;
- if (syncPhaseAcc < syncPhaseInc) {
- // Time to start the next grain
- grainPhaseAcc = 0;
- grainAmp = 0x7fff;
- grain2PhaseAcc = 0;
- grain2Amp = 0x7fff;
- LED_PORT ^= 1 << LED_BIT; // Faster than using digitalWrite
- }
- // Increment the phase of the grain oscillators
- grainPhaseAcc += grainPhaseInc;
- grain2PhaseAcc += grain2PhaseInc;
- // Convert phase into a triangle wave
- value = (grainPhaseAcc >> 7) & 0xff;
- if (grainPhaseAcc & 0x8000) value = ~value;
- // Multiply by current grain amplitude to get sample
- output = value * (grainAmp >> 8);
- // Repeat for second grain
- value = (grain2PhaseAcc >> 7) & 0xff;
- if (grain2PhaseAcc & 0x8000) value = ~value;
- output += value * (grain2Amp >> 8);
- // Make the grain amplitudes decay by a factor every sample (exponential decay)
- grainAmp -= (grainAmp >> 8) * grainDecay;
- grain2Amp -= (grain2Amp >> 8) * grain2Decay;
- // Scale output to the available range, clipping if necessary
- output >>= 9;
- if (output > 255) output = 255;
- // Output to PWM (this is faster than using analogWrite)
- PWM_VALUE = output;
- }
- void setBacklight(uint8_t red, uint8_t green , uint8_t blue) {
- // normalize the red LED - its brighter than the rest!
- red = map(red, 0, 255, 0, 100);
- green = map(green, 0, 255, 0, 150);
- red = map(red, 0, 255, 0, brightness);
- green = map(green, 0, 255, 0, brightness);
- blue = map(blue, 0, 255, 0, brightness);
- // common anode so invert!
- red = map(red, 0, 255, 255, 0);
- green = map(green, 0, 255, 255, 0);
- blue = map(blue, 0, 255, 255, 0);
- Serial.print("R = "); Serial.print(red, DEC);
- Serial.print(" G = "); Serial.print(green, DEC);
- Serial.print(" B = "); Serial.println(blue, DEC);
- analogWrite(REDLITE, red);
- analogWrite(GREENLITE, green);
- analogWrite(BLUELITE, blue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement