// by Les Hall 2013 // public domain code - use freely // global variables int outPins[] = {3, 4, 0, 1, 2}; unsigned int counter = 0; byte sum = 0; unsigned int numWaves = 16; unsigned int indexMax = 128; byte sineTable[128]; unsigned long nextTime = 0; unsigned long prevTime = 0; unsigned long deltaTime = 0; unsigned long noteDuration = 200000; // the setup routine runs once when you press reset: void setup() { for (int i = 0; i < 5; ++i) { pinMode( outPins[i], OUTPUT); } for (int i = 0; i < indexMax; ++i) { sineTable[i] = byte( 255.0 * ( sin(TWO_PI * float(i) / float(indexMax) ) + 1.0 ) / 2.0 ); } prevTime = micros(); } // the loop routine runs over and over again forever: void loop() { nextTime = micros(); deltaTime = nextTime - prevTime; if ( deltaTime >= noteDuration ) { prevTime = nextTime; sum = 0; for( int i = 0; i < 32; ++i ) { sum += bitRead(counter, i); } } unsigned int sineWave = sineTable[counter & 0x007F] * sum; for( int i = 0; i <= 4; ++i ) { if( bitRead(sineWave, i+6) == 0 ) { digitalWrite( outPins[i], LOW); } else { digitalWrite( outPins[i], HIGH); } } ++counter; delayMicroseconds(16 * int( sum ) ); }