Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #define outpin 4 // hook up this pin to audio out in series with a 1 uf cap and 1k resistor.
- // analog inputs, these number are for the analog pin inputs A0, A1 etc
- // use 5 - 50k pots. Looking at the pot shaft, with pot leads facing up, hook up left leads to +5V,
- // middle terminals to A0, A1 etc, and right terminals to ground
- #define pitchPot 0
- #define modPot 1
- #define speedPot 2
- #define phasePot 3
- #define jukePot 4
- int randomWalkLowRange; // for random walk function
- int randomWalkHighRange;
- int val, runlevel;
- int n = LOW;
- int ptime, range, strt, inc;
- int k, x, dur, freq, t;
- int i, j;
- int sens, adder;
- float ps, fpitch; // variable for pow pitchShift routine
- float noteval;
- //note values
- float AAA = 13520;
- float A = 14080;
- float AS = 14917.2;
- float B = 15804.3;
- float C = 16744;
- float CS = 17739.7;
- float D = 18794.5;
- float DS = 19912.1;
- float E = 21096.2;
- float F = 22350.6;
- float FS = 23679.6;
- float G = 25087.7;
- float GS = 26579.5;
- float AA2 = 28160;
- float A2S = 29834.5;
- float B2 = 31608.5;
- float C2 = 33488.1;
- float C2S = 35479.4;
- float D2 = 37589.1;
- float D2S = 39824.3;
- float E2 = 42192.3;
- float F2 = 44701.2;
- float F2S = 47359.3;
- float G2 = 50175.4;
- float G2S = 53159;
- float AA3 = 56320;
- //rhythm values
- int wh = 1024;
- int h = 512;
- int dq = 448;
- int q = 256;
- int qt = 170;
- int de = 192;
- int e = 128;
- int et = 85;
- int dsx = 96;
- int sx = 64;
- int thx = 32;
- float majScale[] = {
- A, B, CS, D, E, FS, GS, AA2, B2, C2S, D2, E2, F2S, G2S, AA3};
- float minScale[] = {
- A, B, C, D, E, F, GS, AA2, B2, C2, D2, E2, F2, G2S, AA3};
- float creme[] = { // make sure this array and the following one have same number of entries - bottom array is time values
- A, CS, D, CS, D, E, CS, D, CS, B, A};
- float creme2[] = { // make sure this array and the following one have same number of entries - bottom array is time values
- A, CS, A, B, CS, CS, A, B, CS, B, A};
- float creme3[] = { // make sure this array and the following one have same number of entries - bottom array is time values
- AAA, CS, A, B, AAA, AAA, A, B, CS, B, A};
- float cremeDur[] = {
- q, q, qt, qt, qt, q, q, qt, qt, qt, q};
- void setup() {
- pinMode(outpin, OUTPUT);
- Serial.begin(9600);
- Serial.println("start");
- pinMode(outpin, INPUT); // turn off audio out
- }
- void loop(){
- doCreme5(); // pitchPot (0) & speedPot (2) & modPot(1)
- }
- // pitchPot (0) & speedPot (2) & modPot(1)
- void doCreme5(){
- int LowRange = 0;
- int HighRange = 10;
- for(x= 0; x<=HighRange; x++){
- noteval = creme2[x] * 2 / ((float)analogRead(pitchPot)*5);
- dur = ((cremeDur[x] * (float)analogRead(speedPot)) / 35.0);
- freqout(noteval, dur);
- delay(analogRead(modPot));
- }
- }
- void freqout(int freq, int t)
- {
- //calculate 1/2 period in us
- unsigned int cycles;
- long i, hperiod;
- long highPeriod, lowPeriod;
- int phase;
- t = max(t,1); // check to prevent 0 time
- hperiod = (500000 / freq) - 7; // subtract 7 us to make up for digitalWrite overhead - determined empirically
- phase = analogRead(phasePot)/50*20;
- highPeriod = hperiod * phase / 1023;
- lowPeriod = ((hperiod * (1023 - phase)) / 1023) - 1; // - 1 to make up for fractional microsecond in digitaWrite overhead
- // calculate cycles
- cycles = ((long)freq * (long)t) / 1000; // calculate cycles
- if (highPeriod <= 0 || lowPeriod <= 0){
- pinMode(outpin, INPUT);
- return;
- }
- pinMode(outpin, OUTPUT); // turn on output pin
- for (i=0; i<= cycles; i++){ // play note for t ms
- digitalWrite(outpin, HIGH);
- delayMicroseconds(highPeriod);
- digitalWrite(outpin, LOW);
- delayMicroseconds(lowPeriod);
- }
- pinMode(outpin, INPUT); // shut off pin to avoid noise from other operations
- }
Add Comment
Please, Sign In to add comment