Guest User

Untitled

a guest
Jun 23rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <math.h>
  2.  
  3. #define outpin 4 // hook up this pin to audio out in series with a 1 uf cap and 1k resistor.
  4.  
  5. // analog inputs, these number are for the analog pin inputs A0, A1 etc
  6. // use 5 - 50k pots. Looking at the pot shaft, with pot leads facing up, hook up left leads to +5V,
  7. // middle terminals to A0, A1 etc, and right terminals to ground
  8. #define pitchPot 0
  9. #define modPot 1
  10. #define speedPot 2
  11. #define phasePot 3
  12. #define jukePot 4
  13.  
  14. int randomWalkLowRange; // for random walk function
  15. int randomWalkHighRange;
  16.  
  17. int val, runlevel;
  18. int n = LOW;
  19.  
  20. int ptime, range, strt, inc;
  21. int k, x, dur, freq, t;
  22. int i, j;
  23. int sens, adder;
  24.  
  25. float ps, fpitch; // variable for pow pitchShift routine
  26.  
  27. float noteval;
  28. //note values
  29. float AAA = 13520;
  30. float A = 14080;
  31. float AS = 14917.2;
  32. float B = 15804.3;
  33. float C = 16744;
  34. float CS = 17739.7;
  35. float D = 18794.5;
  36. float DS = 19912.1;
  37. float E = 21096.2;
  38. float F = 22350.6;
  39. float FS = 23679.6;
  40. float G = 25087.7;
  41. float GS = 26579.5;
  42. float AA2 = 28160;
  43. float A2S = 29834.5;
  44. float B2 = 31608.5;
  45. float C2 = 33488.1;
  46. float C2S = 35479.4;
  47. float D2 = 37589.1;
  48. float D2S = 39824.3;
  49. float E2 = 42192.3;
  50. float F2 = 44701.2;
  51. float F2S = 47359.3;
  52. float G2 = 50175.4;
  53. float G2S = 53159;
  54. float AA3 = 56320;
  55.  
  56. //rhythm values
  57. int wh = 1024;
  58. int h = 512;
  59. int dq = 448;
  60. int q = 256;
  61. int qt = 170;
  62. int de = 192;
  63. int e = 128;
  64. int et = 85;
  65. int dsx = 96;
  66. int sx = 64;
  67. int thx = 32;
  68.  
  69.  
  70.  
  71. float majScale[] = {
  72. A, B, CS, D, E, FS, GS, AA2, B2, C2S, D2, E2, F2S, G2S, AA3};
  73.  
  74. float minScale[] = {
  75. A, B, C, D, E, F, GS, AA2, B2, C2, D2, E2, F2, G2S, AA3};
  76.  
  77. float creme[] = { // make sure this array and the following one have same number of entries - bottom array is time values
  78. A, CS, D, CS, D, E, CS, D, CS, B, A};
  79. float creme2[] = { // make sure this array and the following one have same number of entries - bottom array is time values
  80. A, CS, A, B, CS, CS, A, B, CS, B, A};
  81. float creme3[] = { // make sure this array and the following one have same number of entries - bottom array is time values
  82. AAA, CS, A, B, AAA, AAA, A, B, CS, B, A};
  83. float cremeDur[] = {
  84. q, q, qt, qt, qt, q, q, qt, qt, qt, q};
  85.  
  86. void setup() {
  87. pinMode(outpin, OUTPUT);
  88.  
  89. Serial.begin(9600);
  90.  
  91. Serial.println("start");
  92. pinMode(outpin, INPUT); // turn off audio out
  93. }
  94.  
  95.  
  96. void loop(){
  97. doCreme5(); // pitchPot (0) & speedPot (2) & modPot(1)
  98. }
  99.  
  100. // pitchPot (0) & speedPot (2) & modPot(1)
  101. void doCreme5(){
  102. int LowRange = 0;
  103. int HighRange = 10;
  104.  
  105. for(x= 0; x<=HighRange; x++){
  106. noteval = creme2[x] * 2 / ((float)analogRead(pitchPot)*5);
  107. dur = ((cremeDur[x] * (float)analogRead(speedPot)) / 35.0);
  108. freqout(noteval, dur);
  109. delay(analogRead(modPot));
  110. }
  111. }
  112.  
  113. void freqout(int freq, int t)
  114. {
  115. //calculate 1/2 period in us
  116. unsigned int cycles;
  117. long i, hperiod;
  118. long highPeriod, lowPeriod;
  119. int phase;
  120.  
  121. t = max(t,1); // check to prevent 0 time
  122. hperiod = (500000 / freq) - 7; // subtract 7 us to make up for digitalWrite overhead - determined empirically
  123. phase = analogRead(phasePot)/50*20;
  124. highPeriod = hperiod * phase / 1023;
  125. lowPeriod = ((hperiod * (1023 - phase)) / 1023) - 1; // - 1 to make up for fractional microsecond in digitaWrite overhead
  126. // calculate cycles
  127. cycles = ((long)freq * (long)t) / 1000; // calculate cycles
  128.  
  129. if (highPeriod <= 0 || lowPeriod <= 0){
  130. pinMode(outpin, INPUT);
  131. return;
  132. }
  133.  
  134. pinMode(outpin, OUTPUT); // turn on output pin
  135.  
  136.  
  137. for (i=0; i<= cycles; i++){ // play note for t ms
  138. digitalWrite(outpin, HIGH);
  139. delayMicroseconds(highPeriod);
  140. digitalWrite(outpin, LOW);
  141. delayMicroseconds(lowPeriod);
  142. }
  143. pinMode(outpin, INPUT); // shut off pin to avoid noise from other operations
  144. }
Add Comment
Please, Sign In to add comment