/* * DACmulti2a * Arduino Diecimila sketch * August 4, 2009, G. Forrest Cook * Released under the GPLv3 license. * * Modulate an 8 bit DAC with one of three arbitrary waveforms. * Use the reset button to step to the next waveform. * The EEPROM remembers the previously selected waveform on power-up. * An R-C array is used on an analog input pin to detect the first boot. * An RGB LED displays Yellow, Cyan or Magenta for pattern 1, 2 or 3. */ #include byte eepromval; int RedPin = 8; int GreenPin = 9; // RGB LEDs (PB0-2) int BluePin = 10; int WaveNum = 0; int dtime; int indx = 0; #define YELLOW 3 #define CYAN 6 #define MAGENTA 5 #define STEPS1 256 // 1.5 exp sine wave #define STEPS2 256 // 1.7 exp sine wave #define STEPS3 256 // 2.1 exp sine wave byte wavetable1[STEPS1] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 21, 22, 24, 25, 27, 29, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 51, 53, 55, 58, 60, 62, 65, 68, 70, 73, 75, 78, 81, 84, 87, 90, 93, 95, 98, 102, 105, 108, 111, 114, 117, 120, 123, 127, 130, 133, 136, 140, 143, 146, 150, 153, 156, 159, 163, 166, 169, 172, 176, 179, 182, 185, 188, 191, 194, 197, 200, 203, 206, 209, 211, 214, 217, 219, 222, 224, 227, 229, 231, 233, 235, 237, 239, 241, 242, 244, 245, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 254, 254, 255, 254, 254, 254, 254, 253, 252, 252, 251, 250, 249, 248, 247, 245, 244, 242, 241, 239, 237, 235, 233, 231, 229, 227, 224, 222, 219, 217, 214, 211, 209, 206, 203, 200, 197, 194, 191, 188, 185, 182, 179, 176, 172, 169, 166, 163, 159, 156, 153, 150, 146, 143, 140, 136, 133, 130, 127, 123, 120, 117, 114, 111, 108, 105, 102, 98, 95, 93, 90, 87, 84, 81, 78, 75, 73, 70, 68, 65, 62, 60, 58, 55, 53, 51, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 29, 27, 25, 24, 22, 21, 19, 18, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0}; byte wavetable2[STEPS2] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 24, 25, 27, 28, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 50, 52, 54, 56, 59, 61, 64, 66, 69, 71, 74, 77, 80, 82, 85, 88, 91, 94, 97, 100, 103, 106, 109, 113, 116, 119, 122, 126, 129, 132, 136, 139, 142, 146, 149, 152, 156, 159, 163, 166, 169, 173, 176, 179, 183, 186, 189, 192, 195, 198, 202, 205, 207, 210, 213, 216, 219, 221, 224, 226, 229, 231, 233, 235, 237, 239, 241, 243, 244, 246, 247, 248, 250, 251, 252, 252, 253, 254, 254, 254, 255, 255, 255, 254, 254, 254, 253, 252, 252, 251, 250, 248, 247, 246, 244, 243, 241, 239, 237, 235, 233, 231, 229, 226, 224, 221, 219, 216, 213, 210, 207, 205, 202, 198, 195, 192, 189, 186, 183, 179, 176, 173, 169, 166, 163, 159, 156, 152, 149, 146, 142, 139, 136, 132, 129, 126, 122, 119, 116, 113, 109, 106, 103, 100, 97, 94, 91, 88, 85, 82, 80, 77, 74, 71, 69, 66, 64, 61, 59, 56, 54, 52, 50, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 28, 27, 25, 24, 22, 21, 19, 18, 17, 16, 14, 13, 12, 11, 10, 9, 8, 7, 7, 6, 5, 4, 4, 3, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0}; byte wavetable3[STEPS3] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 22, 23, 24, 26, 27, 29, 30, 32, 34, 36, 37, 39, 41, 43, 45, 47, 49, 51, 54, 56, 58, 61, 63, 66, 68, 71, 74, 76, 79, 82, 85, 88, 91, 94, 97, 100, 103, 107, 110, 113, 116, 120, 123, 127, 130, 134, 137, 141, 144, 148, 152, 155, 159, 162, 166, 170, 173, 177, 180, 184, 187, 191, 194, 197, 201, 204, 207, 210, 213, 216, 219, 222, 225, 228, 230, 233, 235, 237, 239, 241, 243, 245, 246, 248, 249, 251, 252, 253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 253, 252, 251, 249, 248, 246, 245, 243, 241, 239, 237, 235, 233, 230, 228, 225, 222, 219, 216, 213, 210, 207, 204, 201, 197, 194, 191, 187, 184, 180, 177, 173, 170, 166, 162, 159, 155, 152, 148, 144, 141, 137, 134, 130, 127, 123, 120, 116, 113, 110, 107, 103, 100, 97, 94, 91, 88, 85, 82, 79, 76, 74, 71, 68, 66, 63, 61, 58, 56, 54, 51, 49, 47, 45, 43, 41, 39, 37, 36, 34, 32, 30, 29, 27, 26, 24, 23, 22, 20, 19, 18, 17, 15, 14, 13, 12, 11, 10, 9, 9, 8, 7, 6, 6, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0}; void setup() { DDRB = 7; // Set port B to outputs for RGB LED. DDRD = 255; // Set port D to all outputs for DAC. if (analogRead(0) > 512) // Not first boot, step values. { eepromval = EEPROM.read(0); if (eepromval == 0) { EEPROM.write(0, 1); WaveNum = 1; PORTB = CYAN; } else if (eepromval == 1) { EEPROM.write(0, 2); WaveNum = 2; PORTB = MAGENTA; } else { EEPROM.write(0, 0); WaveNum = 0; PORTB = YELLOW; } } else // first boot, don't change state. { eepromval = EEPROM.read(0); if (eepromval == 0) { WaveNum = 0; PORTB = YELLOW; } else if (eepromval == 1) { WaveNum = 1; PORTB = CYAN; } else { WaveNum = 2; PORTB = MAGENTA; } } } void loop() { if (WaveNum == 0) // Pick one of the wave tables and loop infinitely. { while (1) { PORTD = wavetable1[indx]; dtime = (analogRead(1) + 5) * 8; delayMicroseconds(dtime); indx++; if (indx >= STEPS1) indx = 0; } } else if (WaveNum == 1) { while (1) { PORTD = wavetable2[indx]; dtime = (analogRead(1) + 5) * 8; delayMicroseconds(dtime); indx++; if (indx >= STEPS2) indx = 0; } } else { while (1) { PORTD = wavetable3[indx]; dtime = (analogRead(1) + 5) * 8; delayMicroseconds(dtime); indx++; if (indx >= STEPS3) indx = 0; } } }