From a4225751e5a0530bb9e6728805c924ce101f8b4e Mon Sep 17 00:00:00 2001 From: --global <--global> Date: Sun, 7 May 2023 20:37:20 +0300 Subject: [PATCH] working on new clock --- software/GToE/GToE.ino | 154 +++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 98 deletions(-) diff --git a/software/GToE/GToE.ino b/software/GToE/GToE.ino index 65a8658..eda4bd7 100644 --- a/software/GToE/GToE.ino +++ b/software/GToE/GToE.ino @@ -45,14 +45,15 @@ channel channels[6] = { //array of channel settings int outsClocksCounts[6]; int playingModes[6]; //actual channel modes array updated from channels each beat -int clockCount = 0; -int pulseCount = 0; int pulsePeriod; -bool isPlaying = 0; -int needToResetChannel; +bool isPlaying = false; + bool beatCounted = false; bool pulseCounted = false; +unsigned int tickCount = 0; +unsigned int pulseCount = 0; + unsigned int masterClockMode = 0; // 0 - internal, 1 - external 24ppqn, 2 - external beat bool externalPulseCounted = true; unsigned long lastExtPulseTime; @@ -127,7 +128,7 @@ void setup() { updateScreen(); updateTiming(); - FlexiTimer2::set(1, 1.0/1000, clock); // 1.0/1000 = 1ms period + FlexiTimer2::set(1, 1.0/1000, clock); // 1.0/1000 = 1ms period. If other than 1ms updateTiming() might need tweaking FlexiTimer2::start(); } @@ -139,106 +140,64 @@ void clock() { if (isPlaying) { // Action on each pulse - if (clockCount == 0 && !pulseCounted) { - - //modulation - for (int i = 0; i<6; i++) { - int mod; - if (!channels[i].modulationChannel) { - mod = a1Input; - } else { - mod = a2Input; - } - mod = map (mod, 0, 1023, 0, channels[i].modulationRange); - playingModes[i] = clockModes[channels[i].mode - mod]; //subtrackting because the innitiall array is backwards - } - - //divider - if (pulseCount == 0 && !beatCounted) { - for (int i = 0; i<6; i++) { - playingModes[i] = clockModes[channels[i].mode]; //updated here to prevent sync problems for multipliers - if (playingModes[i] > 0) { - if (outsClocksCounts[i] == 0) { //Pulse on 0 - if (channels[i].random == 0 || random(10) > channels[i].random) { - digitalWrite(outsPins[i], HIGH); - } - } - if (outsClocksCounts[i] < (playingModes[i] - 1)) { - outsClocksCounts[i]++; - } else { - outsClocksCounts[i] = 0; - } - } - } - beatCounted = true; - } - - //multiplier - for (int i = 0; i<6; i++) { - if (playingModes[i] < 0) { - if (outsClocksCounts[i] == 0) { //Pulse on 0 - if (channels[i].random == 0 || random(10) > channels[i].random) { //random - digitalWrite(outsPins[i], HIGH); - } - } - if (outsClocksCounts[i] < (PPQN / abs(playingModes[i])) - 1) { - outsClocksCounts[i]++; - } else { - outsClocksCounts[i] = 0; - } - } - } + if (tickCount == 0 && !pulseCounted) { + sendTriggers(); pulseCounted = true; } - switch (masterClockMode) { - case 0: //internal - if (clockCount == 0) { - pulseCount++; - beatCounted = false; - pulseCounted = false; - } - break; - case 1: //external 24ppqn - if (externalPulseCounted == false) { - pulseCount++; - externalPulseCounted = true; - beatCounted = false; - pulseCounted = false; - } - break; - case 2: //external beat - if (externalPulseCounted == false) { - externalPulseCounted = true; - clockCount == 0; - lastExtPulseTime = newExtPulseTime; - newExtPulseTime = millis(); - if (lastExtPulseTime) { - updateTiming(); - } - } - if (clockCount == 0) { - pulseCount++; - beatCounted = false; - pulseCounted = false; - } - break; - } - if (clockCount < pulsePeriod) { - clockCount++; + //this part gets the Pulse and Ticks ticking + //it's placed after the triggers to avoid problems on the start (when pulseCount==0) + if (tickCount < pulsePeriod) { + tickCount++; } else { - clockCount = 0; - } - if (pulseCount >= PPQN) { - pulseCount = 0; + tickCount = 0; + if (pulseCount < PPQN) { + pulseCount++; + beatCounted = false; //reset pulse and beat count AFTER the pulse is incremented + pulseCounted = false; + } else { + pulseCount = 0; //Beat happens here + } } - // pull low all outputs after set pulse length - if (clockCount >= PULSE_LENGTH) { - for (int i = 0; i<6; i++) { - digitalWrite(outsPins[i], LOW); + } +} + +void sendTriggers() { + //divider + if (pulseCount == 0 && !beatCounted) { + for (int i = 0; i<6; i++) { + playingModes[i] = clockModes[channels[i].mode]; //updated here to prevent sync problems for multipliers + if (playingModes[i] > 0) { + if (outsClocksCounts[i] == 0) { //Pulse on 0 + if (channels[i].random == 0 || random(10) > channels[i].random) { + digitalWrite(outsPins[i], HIGH); + } + } + if (outsClocksCounts[i] < (playingModes[i] - 1)) { + outsClocksCounts[i]++; + } else { + outsClocksCounts[i] = 0; + } } } + beatCounted = true; + } + + //multiplier + for (int i = 0; i<6; i++) { + if (playingModes[i] < 0) { + if (outsClocksCounts[i] == 0) { //Pulse on 0 + if (channels[i].random == 0 || random(10) > channels[i].random) { //random + digitalWrite(outsPins[i], HIGH); + } + } + if (outsClocksCounts[i] < (PPQN / abs(playingModes[i])) - 1) { + outsClocksCounts[i]++; + } else { + outsClocksCounts[i] = 0; + } + } } } @@ -328,7 +287,6 @@ void checkInputs() { } else if (channels[displayTab-1].mode > (sizeof(clockModes)/sizeof(int)) - 1) { channels[displayTab-1].mode = (sizeof(clockModes)/sizeof(int)) - 1; } - needToResetChannel = displayTab-1; } else if (displayTab != 0 && insideTab == 1) { //random channels[displayTab-1].random = channels[displayTab-1].random + change; if (channels[displayTab-1].random > 9 || channels[displayTab-1].random < 0) {