Loading...
Logo
Processing Forum
Hi All. I want to create a project simular to this link: PROJECT 

I want to use the same detection method but instead, i will control the servo and also the graphics in processing. But i am struggling to find a way to convert the code to processing. 

Or Do i need not change the code but rather ask the arduino to send a message to processing?

Copy code
  1. /* Theremin Test
  2.  *
  3.  * Therremin with TTL Oscillator 4MHz
  4.  * Timer1 for freauency measurement
  5.  * Timer2 for gate time
  6.  * connect Oscillator on digital pin 5
  7.  * connect Speaker with 1K Resistor in series on pin 8

  8.  * KHM 2008 /  Martin Nawrath
  9.  * Kunsthochschule fuer Medien Koeln
  10.  * Academy of Media Arts Cologne

  11.  */
  12. #include <Stdio.h>
  13. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  14. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

  15. //! Macro that clears all Timer/Counter1 interrupt flags.
  16. #define CLEAR_ALL_TIMER1_INT_FLAGS    (TIFR1 = TIFR1)

  17. int pinLed = 13;                 // LED connected to digital pin 13
  18. int pinFreq = 5;

  19. void setup()
  20. {
  21.   pinMode(pinLed, OUTPUT);      // sets the digital pin as output
  22.   pinMode(pinFreq, INPUT);
  23.   pinMode(8, OUTPUT);

  24.   Serial.begin(57600);        // connect to the serial port

  25.   // hardware counter setup ( refer atmega168.pdf chapter 16-bit counter1)
  26.   TCCR1A=0;                   // reset timer/counter1 control register A
  27.   TCCR1B=0;                   // reset timer/counter1 control register A
  28.   TCNT1=0;                    // counter value = 0
  29.   // set timer/counter1 hardware as counter , counts events on pin T1 ( arduino pin 5)
  30.   // normal mode, wgm10 .. wgm13 = 0
  31.   sbi (TCCR1B ,CS10);         // External clock source on T1 pin. Clock on rising edge.
  32.   sbi (TCCR1B ,CS11);
  33.   sbi (TCCR1B ,CS12);

  34.   // timer2 setup / is used for frequency measurement gatetime generation
  35.   // timer 2 presaler set to 256 / timer 2 clock = 16Mhz / 256 = 62500 Hz
  36.   cbi (TCCR2B ,CS20);
  37.   sbi (TCCR2B ,CS21);
  38.   sbi (TCCR2B ,CS22);

  39.   //set timer2 to CTC Mode
  40.   cbi (TCCR2A ,WGM20);
  41.   sbi (TCCR2A ,WGM21);
  42.   cbi (TCCR2B ,WGM22);
  43.   OCR2A = 124;                  // CTC at top of OCR2A / timer2 interrupt when coun value reaches OCR2A value

  44.   // interrupt control

  45.   sbi (TIMSK2,OCIE2A);          // enable Timer2 Interrupt

  46. }

  47. volatile byte i_tics;
  48. volatile byte f_ready ;
  49. volatile byte mlt ;
  50. unsigned int ww;

  51. int cal;
  52. int cal_max;

  53. char st1[32];
  54. long freq_in;
  55. long freq_zero;
  56. long freq_cal;

  57. unsigned int dds;
  58. int tune;

  59. int cnt=0;

  60. void loop()
  61. {
  62.   cnt++;
  63.   // add=analogRead(0);

  64.   f_meter_start();

  65.   tune=tune+1;
  66.   while (f_ready==0) {            // wait for period length end (100ms) by interrupt
  67.     PORTB=((dds+=tune) >> 15);    // kind of DDS tonegenerator / connect speaker to portb.0 = arduino pin8
  68.   }
  69.  tune = freq_in-freq_zero;
  70.  // use the tune value here for your own purposes like control of servos, midi etc.

  71.   // startup
  72.   if (cnt==10) {
  73.     freq_zero=freq_in;
  74.     freq_cal=freq_in;
  75.     cal_max=0;
  76.     Serial.print("** START **");
  77.   }

  78.   // autocalibration
  79.   if (cnt % 20 == 0) {   // try autocalibrate after n cycles
  80.     Serial.print("*");
  81.     if (cal_max <= 2) {
  82.       freq_zero=freq_in;
  83.       Serial.print(" calibration");
  84.     }
  85.     freq_cal=freq_in;
  86.     cal_max=0;
  87.     Serial.println("");
  88.   }
  89.   cal = freq_in-freq_cal;
  90.   if ( cal < 0) cal*=-1;  // absolute value
  91.   if (cal > cal_max) cal_max=cal;

  92.   digitalWrite(pinLed,1);  // let LED blink
  93.   Serial.print(cnt);
  94.   Serial.print("  "); 

  95.   if ( tune < 0) tune*=-1;  // absolute value
  96.    sprintf(st1, " d",tune);
  97.   Serial.print(st1);
  98.   Serial.print("  "); 

  99.   Serial.print(freq_in);
  100.   Serial.print("  ");
  101. /*
  102.   Serial.print(freq_zero);
  103.   Serial.print("  ");
  104.   Serial.print(cal_max);
  105. */
  106.   Serial.println("");
  107.   digitalWrite(pinLed,0);

  108. }
  109. //******************************************************************
  110. void f_meter_start() {
  111.   f_ready=0;                      // reset period measure flag
  112.   i_tics=0;                        // reset interrupt counter
  113.   sbi (GTCCR,PSRASY);              // reset presacler counting
  114.   TCNT2=0;                         // timer2=0
  115.   TCNT1=0;                         // Counter1 = 0
  116.   cbi (TIMSK0,TOIE0);              // dissable Timer0 again // millis and delay
  117.   sbi (TIMSK2,OCIE2A);             // enable Timer2 Interrupt
  118.   TCCR1B = TCCR1B | 7;             //  Counter Clock source = pin T1 , start counting now
  119. }

  120. //******************************************************************
  121. // Timer2 Interrupt Service is invoked by hardware Timer2 every 2ms = 500 Hz
  122. //  16Mhz / 256 / 125 / 500 Hz
  123. // here the gatetime generation for freq. measurement takes place: 

  124. ISR(TIMER2_COMPA_vect) {

  125.   if (i_tics==50) {         // multiple 2ms = gate time = 100 ms
  126.                             // end of gate time, measurement ready
  127.     TCCR1B = TCCR1B & ~7;   // Gate Off  / Counter T1 stopped
  128.     cbi (TIMSK2,OCIE2A);    // disable Timer2 Interrupt
  129.     sbi (TIMSK0,TOIE0);     // ensable Timer0 again // millis and delay
  130.     f_ready=1;              // set global flag for end count period

  131.                             // calculate now frequeny value
  132.     freq_in=0x10000 * mlt;  // mukt #ovverflows by 65636
  133.     freq_in += TCNT1;       // add counter1 value
  134.     mlt=0;

  135.   }
  136.   i_tics++;                 // count number of interrupt events
  137.   if (TIFR1 & 1) {          // if Timer/Counter 1 overflow flag
  138.     mlt++;                  // count number of Counter1 overflows
  139.     sbi(TIFR1,TOV1);        // clear Timer/Counter 1 overflow flag
  140.   }
  141. }