Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- PWM Clock: Simple clock for analog synth modules.
- One potentiometer controls the speed (Freq) and the other controls the sustain/length of the pulse (PWM).
- Pulse:
- 5v _____________ _____________
- | | | | |
- | | | | |
- | | | | |
- |_____|_____________|_____|_____________|__
- 0 1 2 3 4
- Note: The freq knob must be wired in reverse:
- 5+ - pin1
- A0 - pin2 (W)
- 0+ - pin3
- */
- const int led = 13;
- const int freqPin = 0; // 10k revese log potentiometer
- const int pwmPin = 1; // 10k log potentiometer
- const int tempoMin = 30; // The minimum tempo 30bpm = 2se
- enum {sample_state,high_state,low_state}; //Enumerate(give values to) the variables so they are 0,1,2 respectivly
- unsigned int state = sample_state;
- unsigned int val1,val2,val3,hiVal,loVal;
- float timer;
- double mVal = 0;
- double bpm = 30;
- /*----------------------------------------Main loop--------------------------------------*/
- void setup() {
- Serial.begin(9600);
- pinMode(13,OUTPUT);
- }
- void loop() {
- switch(state){ //There are 3 different states that take place, take sample from inputs, set high duration, set low duration
- case sample_state:
- val1 = analogRead(pwmPin);
- val2 = 1023 -analogRead(pwmPin);
- val3 = analogRead(freqPin);
- mVal = map(val3,0,1023,60,2000); //Mapped from 60-2000millisec
- hiVal = map(val1,0,1023,0,mVal);
- loVal = map(val2,0,1023,0,mVal) +1;
- bpm = (2000 / mVal) * tempoMin; // tempoMin = 30
- Serial.print(bpm) ;
- Serial.print(" :BPM ");
- state = high_state; //We have inputs, next we go on to high state mode
- digitalWrite(led,HIGH); //Set the pin high as the next state is just a timer
- timer=millis(); //Set timer to the time when this code block finishes
- break;
- case high_state:
- if(millis()>=timer+hiVal){ //Sit in this state until the timer threshold reaches the end of the high state
- state=low_state; //Move on to low state tmer
- digitalWrite(led,LOW); //Set pin low
- timer=millis(); //Reset timer
- }
- break;
- case low_state:
- if(millis()>=timer+loVal){ //Sit in this state until the timer threashold reaches end of the low state
- state=sample_state; //Cycle completed, go back to taking a new sample and repeat cycle
- }
- break;
- default: //This should never happen
- Serial.println("state variable not valid");
- while(1);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement