Advertisement
Gorgozoth

Optical Theremin Arduino Code

Dec 13th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | Music | 0 0
  1. // Optical Theramin
  2.  
  3. //pin definitions
  4. #define PHONES 9   // speaker is connected to digital pin 9
  5. #define PHOTOCELL 0 //phototransistor analog in pin 0
  6.  
  7. //variable definitions
  8. long val = 0;        //stores the raw value from phototransistor
  9. long maxread = 0;    //maximum value from calibration phase
  10. long minread = 1000;  // minimum value from calibration phase
  11. double f = 0;         // frequency of sound
  12. double normf = 0;    // normalized frequency
  13. double logf = 0;      // logarithm of normalized frequency
  14. int ilogf = 0;        // rounded logarithm
  15. int i = 0;            // loop dummy variable
  16. double factor = 0;    // scaling factor for calibration
  17. double shift = 0;     // shift for calibration
  18. long maxfreq = 440;  // maximum desired frequency after calibration
  19. long minfreq = 220;   // minimum desired frequency after calibration
  20.  
  21.  
  22. //note interval numbers
  23. //double gap = 1.148698355;  //ratio of consecutive notes (pentatonic)
  24.                              // it's the 5th root of 2
  25. //double gap = 1.059463094;  //ratio of consecutive notes (chromatic)
  26.                               // it's the 12th root of 2
  27. double gap = 1.09050773267; //ratio of consecutive notes (standard)
  28.                               // it's the 8th root of 2
  29.                              
  30. void setup()
  31. {
  32.   pinMode(PHONES, OUTPUT);    // sets the digital pin as output
  33.  
  34. // calibration loop to determine a reasonable range of light levels (minread to maxread)
  35. // and map that to frequencies between minfreq and maxfreq
  36.   for (i = 0; i< 500; i++) {  // calibration loop runs for 5 seconds
  37.     val = analogRead(PHOTOCELL);   // read phototransistor
  38.     tone(PHONES, val);          // play raw tone to guide calibration
  39.     if (val > maxread) {        // as the values climb, store the largest
  40.       maxread = val;
  41.     }
  42.     if (val < minread) {        // as the values drop, store the smallest
  43.       minread = val;
  44.     }  
  45.     delay(500);                  // reasonable delay
  46.   }
  47.  
  48.   //Now we use the calibration to calculate scale and shift parameters
  49.   factor = (double)(maxfreq - minfreq) / (double)(maxread - minread); // scale parameter think of it as m in y=mx+b
  50.  
  51.  
  52.   shift = factor * minread - minfreq;  //shift parameter: offset
  53. }
  54.  
  55. void loop()
  56. {
  57.   val = analogRead(PHOTOCELL);   // read phototransistor
  58.   f = factor * val - shift;     // this linearly maps the frequency to
  59.                                 // a value between minfreq and maxfreq
  60.                                 // according to the calibration result
  61.  
  62.   normf = f / (double) minfreq;  // Dividing an exponential function by the min value
  63.   logf = log(normf) / log(gap); // allows us to take the log (base gap) and the result
  64.   ilogf = round(logf);           // is the number of notes above the lowest, once we round it.
  65.   f = minfreq * pow(gap,ilogf);  // undoing the log
  66.  
  67.  
  68.   tone(PHONES, f);              // this produces the tone signal
  69.  
  70. }
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement