1. /*
  2.  PWM Clock: Simple clock for analog synth modules.
  3.  One potentiometer controls the speed (Freq) and the other controls the sustain/length of the pulse (PWM).
  4.  
  5.  
  6.  
  7.  
  8.  Pulse:
  9.  5v     _____________       _____________
  10.  |     |             |     |             |
  11.  |     |             |     |             |
  12.  |     |             |     |             |
  13.  |_____|_____________|_____|_____________|__
  14.  0         1         2         3         4
  15.  
  16.  
  17.  
  18.  
  19.  Note: The freq knob must be wired in reverse:
  20.  5+  - pin1
  21.  A0  - pin2 (W)
  22.  0+  - pin3
  23.  */
  24.  
  25. const int led = 13;
  26. const int freqPin = 0;                       // 10k revese log potentiometer
  27. const int pwmPin = 1;                        // 10k log potentiometer
  28. const int tempoMin = 30;                     // The minimum tempo 30bpm = 2se
  29. enum {sample_state,high_state,low_state};    //Enumerate(give values to) the variables so they are 0,1,2 respectivly
  30. unsigned int state = sample_state;
  31. unsigned int val1,val2,val3,hiVal,loVal;
  32. float timer;
  33. double mVal = 0;
  34. double bpm = 30;
  35.  
  36. /*----------------------------------------Main loop--------------------------------------*/
  37. void setup() {
  38.         Serial.begin(9600);
  39.         pinMode(13,OUTPUT);
  40. }
  41.  
  42. void loop() {
  43.         switch(state){                                                   //There are 3 different states that take place, take sample from inputs, set high duration, set low duration
  44.                 case sample_state:
  45.                         val1 = analogRead(pwmPin);
  46.                         val2 = 1023 -analogRead(pwmPin);
  47.                         val3 = analogRead(freqPin);
  48.  
  49.                         mVal = map(val3,0,1023,60,2000);                //Mapped from 60-2000millisec
  50.                         hiVal = map(val1,0,1023,0,mVal);
  51.                         loVal = map(val2,0,1023,0,mVal) +1;
  52.                        
  53.                         bpm = (2000 / mVal) * tempoMin;                 // tempoMin = 30
  54.                         Serial.print(bpm) ;
  55.                         Serial.print(" :BPM    ");
  56.                        
  57.                         state = high_state;                             //We have inputs, next we go on to high state mode
  58.                         digitalWrite(led,HIGH);                         //Set the pin high as the next state is just a timer
  59.                         timer=millis();                                 //Set timer to the time when this code block finishes
  60.                         break;
  61.                 case high_state:                                                                      
  62.                         if(millis()>=timer+hiVal){                      //Sit in this state until the timer threshold reaches the end of the high state      
  63.                           state=low_state;                              //Move on to low state tmer
  64.                           digitalWrite(led,LOW);                        //Set pin low
  65.                           timer=millis();                               //Reset timer
  66.                         }
  67.                         break;
  68.                 case low_state:                        
  69.                         if(millis()>=timer+loVal){                      //Sit in this state until the timer threashold reaches end of the low state
  70.                           state=sample_state;                           //Cycle completed, go back to taking a new sample and repeat cycle
  71.                         }
  72.                         break;
  73.                 default:                                                //This should never happen
  74.                         Serial.println("state variable not valid");
  75.                         while(1);
  76.                         break;
  77.         }
  78.        
  79. }