-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClockDivider.ino
61 lines (45 loc) · 1.45 KB
/
ClockDivider.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const int clockPin = 10;
const int outputPin = 13;
const int ratePin = A0; // select the input pin for the potentiometer
const int widthPin = A1; // select the input pin for the potentiometer
int pulseWidth = 100;
unsigned int count = 1;
byte clockHigh = 0;
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Clock Divider - Take one");
pinMode(clockPin, INPUT);
}
void trigger(){
digitalWrite(outputPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(pulseWidth); // wait for a second
digitalWrite(outputPin, LOW); // turn the LED off by making the voltage LOW
}
void loop() {
byte clockState = digitalRead(clockPin);
byte rateValue = analogRead(ratePin);
rateValue = map(rateValue, 0, 1023, 0, 64);
pulseWidth = analogRead( widthPin);
// check if clock was received, if so increment
if (clockState == HIGH && clockHigh == 0) {
Serial.println(count);
clockHigh = 1;
count++;
} else if (clockState == HIGH && clockHigh == 1) {
} else {
clockHigh = 0;
}
// clock divider
if (count / rateValue == 1 || count > rateValue){
// do something every 16th note
trigger(); // wait for a second
Serial.println("Trigger");
Serial.println(rateValue);
count = 1;
}
}