NoiseMachine

USE Pin 6 and connect to speaker with ground.

Bare essential code for getting noise:



// SOUND SYSTEM



#define SAMPLE_MAX (65535.0)

#define SAMPLE_FREQUENCY (8000.0)

#define TIMER1_FREQUENCY 2000000

#define UPDATE_RATE 8000


// by keeping t global we can use it to drive the visualiser as well

long t;



// iterate the grains and LFO

SIGNAL(TIMER1_COMPA_vect) {

 OCR1A += (TIMER1_FREQUENCY / UPDATE_RATE);


 t++;

 OCR0A = ((-t&4095)*(255&t*(t&t>>13))>>12)+(127&t*(234&t>>8&t>>3)>>(3&t>>14)); // by tejeez


}


void setupSound() {

 TCCR1A = 0x0;             // set the timer prescaler to 8 = 16/8 = 2MHz

 TCCR1B = 0x02;            // set the timer prescaler to 8 = 16/8 = 2MHz

 TIMSK1 |= (1 << OCIE1A);  // Enable output compare match interrupt on OCR1A


 TCCR0A = 0B10000011;  //-8 bit audio PWM

 TCCR0B = 0x01;        // Set to clock frequency, no prescaler

 OCR0A = 127;          // set in the middle - do we need this ? probably not.

 DDRD |= 1 << 6;       // Set digital pin 6 to output - channels 2 and 3


 DDRB &= (~15);                                        // set digital pins 8,9,10,11 as inputs

 DDRD |= ((1 << 2) | (1 << 3) | (1 << 4) | (1 << 5));  // set digital pins 2,3,4,5 as outputs for the visualiser

 DDRC &= (15);

}




void setup() {

 setupSound();

}



void loop() {


}




Code with 4 potentiometers that control parameters





int song = 0;

int param1 = 0;

int param2 = 0;

int param3 = 0;

unsigned char s1 = 0;

unsigned char p1 = 0;

unsigned char p2 = 0;

unsigned char p3 = 0;




// SOUND SYSTEM



#define SAMPLE_MAX (65535.0)

#define SAMPLE_FREQUENCY (8000.0)

#define TIMER1_FREQUENCY 2000000

#define UPDATE_RATE 8000


// by keeping t global we can use it to drive the visualiser as well

long t;



// iterate the grains and LFO

SIGNAL(TIMER1_COMPA_vect) {

 OCR1A += (TIMER1_FREQUENCY / UPDATE_RATE);


 t++;


 switch (s1) {

   default:

   case 0:

     OCR0A = 0;

     break;

   case 1:


     OCR0A = ((-t & 4095) * (p1 & t * (t & t >> 13)) >> 12) + (p2 & t * (p3 & t >> 8 & t >> 3) >> (3 & t >> 14));  // by tejeez


     break;

   case 2:

     OCR0A = t * (t >> p1 & t >> 8 & p2 & t >> p3);  // by tejeez

     break;

   case 3:

     OCR0A = t * ((t >> p1 | t >> p2) & p3 & t >> 6);  // by visy

     break;

   case 4:

     OCR0A = (t * (t >> p1 | t >> p2)) >> (t >> p3);  // by tejeez

     break;

   case 5:

     OCR0A = ((t * (t >> p1 | t >> 9) & p2 & t >> 8)) ^ (t & t >> p3 | t >> 6);  // by xpansive

     break;

   case 6:


     OCR0A = ((t & p1) ? ((t * (t ^ t % p2) | (t >> 4)) >> 1) : (t >> 3) | ((t & param3) ? t << 2 : t));  // by skurk (raer's version)

     break;

   case 7:

     OCR0A = (t >> 7 | t | t >> 6) * p1 + p2 * (t & t >> p3 | t >> 6);  // by viznut, xpansive, varjohukka

     break;

   case 8:

     OCR0A = t * p1 & (t >> p2) | t * 3 & (t * p3 >> 10);  // by miiro

     break;

   case 9:

     OCR0A = (t | (t >> p1 | t >> p2)) * t & (t >> p3 | t >> 9);  // by red

     break;

   case 10:

     long v;

     OCR0A = v = (v >> 1) + (v >> 4) + t * (((t >> p1) | (t >> 6)) & (p2 & (t >> p3)));  // by pyryp

     break;

   case 11:

     OCR0A = (t >> 6 | t | t >> (t >> p1)) * p2 + ((t >> p3) & 7);  //by viznut

     break;

   case 12:

     OCR0A = (t * (4 | p1 & t >> 13) >> ((~t >> 11) & 1) & p1) + ((t) * (t >> p3 & t >> 13) * ((~t >> 9) & 3) & 127);  // by stimmer

     break;

   case 13:

     OCR0A = (t >> p3 | t | t >> (t >> p1)) * p2;

     break;

   case 14:

     OCR0A = (sin((float)t * p1) + 1.0f) * (float)p2;

     break;

   case 15:

     // free to use

     break;

     // any more and we need another bit from PORTB

     //  OCR0A = OCR0A/4;

 }

}


void setupSound() {

 TCCR1A = 0x0;             // set the timer prescaler to 8 = 16/8 = 2MHz

 TCCR1B = 0x02;            // set the timer prescaler to 8 = 16/8 = 2MHz

 TIMSK1 |= (1 << OCIE1A);  // Enable output compare match interrupt on OCR1A


 TCCR0A = 0B10000011;  //-8 bit audio PWM

 TCCR0B = 0x01;        // Set to clock frequency, no prescaler

 OCR0A = 127;          // set in the middle - do we need this ? probably not.

 DDRD |= 1 << 6;       // Set digital pin 6 to output - channels 2 and 3


 DDRB &= (~15);                                        // set digital pins 8,9,10,11 as inputs

 DDRD |= ((1 << 2) | (1 << 3) | (1 << 4) | (1 << 5));  // set digital pins 2,3,4,5 as outputs for the visualiser

 DDRC &= (15);

}



void updateP() {

 p1 = param1 / (1024 / 255);

 p2 = param2 / (1024 / 255);

 p3 = param3 / (1024 / 255);

 s1 = song / (1025 / 14);

}


void setup() {

 pinMode(13, OUTPUT);

 digitalWrite(13, LOW);


 setupSound();

}



void loop() {

 // **** Main update call for the guino

 song = 500;//analogRead(0);

 param1 =  500;//analogRead(1);

 param2 =  500;//analogRead(2);

 param3 =  500;// analogRead(3);



 p1 = param1 / (1024 / 255);

 p2 = param2 / (1024 / 255);

 p3 = param3 / (1024 / 255);

 s1 = song / (1025 / 14);


 unsigned char output = OCR0A;

}