channel reset on divider change WIP

This commit is contained in:
2023-01-22 23:34:22 +02:00
parent 2f3c81a8a4
commit e274b66839
2 changed files with 36 additions and 17 deletions

8
README.MD Normal file
View File

@ -0,0 +1,8 @@
TODO:
- Add min and max values
- External clock
- Switch to U8G2 for screen
- Save state to EEPROM when stopped
Timer library available here
https://github.com/PaulStoffregen/FlexiTimer2

View File

@ -2,7 +2,7 @@
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include <RotaryEncoder.h> #include <RotaryEncoder.h>
#include <MsTimer2.h> #include <FlexiTimer2.h>
#include <EEPROM.h> #include <EEPROM.h>
#define SCREEN_ADDRESS 0x3C #define SCREEN_ADDRESS 0x3C
@ -13,18 +13,10 @@
#define ENC_D1_PIN 4 #define ENC_D1_PIN 4
#define ENC_D2_PIN 3 #define ENC_D2_PIN 3
#define START_STOP_BTN_PIN 14 #define START_STOP_BTN_PIN 14
//Output pins are defined in the array below //Output pins are defined in the array below
//TODO :
//Add min and max values
//External clock
//Switch to another timer lib (FlexiTimer?)
//Switch to U8G2 for screen
//Save state to EEPROM when stopped
int outsPins[6] = {5, 6, 7, 8, 9, 10}; int outsPins[6] = {5, 6, 7, 8, 9, 10};
int outsValues[6] = {1, 2, 4, -4, 1, 1}; //positive - divide, negative - multiply, 0 - off int outsValues[6] = {1, 2, 4, -4, 1, 1}; //positive - divide, negative - multiply, 0 - off
int outsPeriods[6]; int outsPeriods[6];
int outsClocksCounts[6] = {0, 0, 0, 0, 0, 0}; int outsClocksCounts[6] = {0, 0, 0, 0, 0, 0};
@ -36,7 +28,9 @@ int clockCount = 0;
int bpm = 120; int bpm = 120;
int bpmPeriod; int bpmPeriod;
int bpmClockCount;
bool isPlaying = 0; bool isPlaying = 0;
int needToResetChannel;
int displayTab = 0; int displayTab = 0;
int displayTabOld; int displayTabOld;
@ -67,8 +61,8 @@ void setup() {
updateScreen(); updateScreen();
updatePeriods(); updatePeriods();
MsTimer2::set(1, internalClock); // 1ms period FlexiTimer2::set(1, 1.0/1000, internalClock); // 1.0/1000 = 1ms period
MsTimer2::start(); FlexiTimer2::start();
} }
void loop() { void loop() {
@ -84,12 +78,22 @@ void internalClock() {
if (outsClocksCounts[i] == 0) { if (outsClocksCounts[i] == 0) {
digitalWrite(outsPins[i], HIGH); digitalWrite(outsPins[i], HIGH);
} }
if (outsClocksCounts[i] == 15) { if (outsClocksCounts[i] == 15) { //15ms pulse
digitalWrite(outsPins[i], LOW); digitalWrite(outsPins[i], LOW);
} }
outsClocksCounts[i]++; outsClocksCounts[i]++;
} }
} if (bpmClockCount < bpmPeriod) {
bpmClockCount++;
} else {
bpmClockCount = 0;
if (needToResetChannel>=0) {
outsClocksCounts[needToResetChannel] = 0;
updatePeriods();
needToResetChannel = -1;
}
}
}
} }
void updatePeriods() { void updatePeriods() {
@ -103,6 +107,12 @@ void updatePeriods() {
} }
} }
void resetClocks() {
for (int i = 0; i<6; i++) {
outsClocksCounts[i] = 0;
}
}
void checkInputs() { void checkInputs() {
clockMode = digitalRead(INPUT_CONNECTED_PIN); clockMode = digitalRead(INPUT_CONNECTED_PIN);
if (clockMode != clockModeOld) { if (clockMode != clockModeOld) {
@ -131,14 +141,15 @@ void checkInputs() {
bpm = bpm + change; bpm = bpm + change;
} else { } else {
outsValues[displayTab-1] = outsValues[displayTab-1] + change; outsValues[displayTab-1] = outsValues[displayTab-1] + change;
needToResetChannel = displayTab-1;
} }
updatePeriods();
updateScreen(); updateScreen();
encPositionOld = encPosition; encPositionOld = encPosition;
} }
if (!digitalRead(START_STOP_BTN_PIN) && !buttonPushed) { if (!digitalRead(START_STOP_BTN_PIN) && !buttonPushed) {
isPlaying = !isPlaying; isPlaying = !isPlaying;
resetClocks();
buttonPushed = true; buttonPushed = true;
} else if (digitalRead(START_STOP_BTN_PIN) && buttonPushed) { } else if (digitalRead(START_STOP_BTN_PIN) && buttonPushed) {
buttonPushed = false; buttonPushed = false;
@ -152,7 +163,7 @@ void updateScreen() {
display.setCursor(0,0); display.setCursor(0,0);
display.setTextSize(1); display.setTextSize(1);
if (displayTab == 0) { if (displayTab == 0) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);resetClocks();
display.print(F(" ")); display.print(F(" "));
display.setTextColor(SSD1306_WHITE); display.setTextColor(SSD1306_WHITE);
display.print(F(" bpm ")); display.print(F(" bpm "));
@ -198,4 +209,4 @@ void updateScreen() {
} }
display.display(); display.display();
} }