clearskyer
Member
Hi ! Im aiming at building a step sequencer ! I come here in a total state of humbleness, absolutely no code experience here hehe.
For now, im just trying to add an encoder to increase and decrease the BPM and its been already 2 full days on that with no result hehe.
1- Can anyone could help me with the issue in that code ?
2- Also i was wondering which path should i take to achieve the tighter, stable and efficient clock for a musical sequencer. For now im guessing between timerOne and uClock ? But maybe there are better ways ?
Teensy 4.1
Thanks a lot !
My code for now:
#include <TimerOne.h>
// Define the initial BPM and the corresponding interval in microseconds
int bpm = 120;
int interval_micros = (60000000 / bpm);
// Define the pins for the encoder inputs
#define ENCODER_PIN_A 33
#define ENCODER_PIN_B 34
// Define the pin for the clock output
#define CLOCK_PIN 23
// Initialize the timer interrupt
void timerISR() {
static boolean state = false;
digitalWrite(CLOCK_PIN, state);
state = !state;
}
void setup() {
pinMode(CLOCK_PIN, OUTPUT);
pinMode(ENCODER_PIN_A, INPUT_PULLUP);
pinMode(ENCODER_PIN_B, INPUT_PULLUP);
// Attach interrupt to the encoder pins
attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_A), encoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_B), encoderISR, CHANGE);
// Set up Timer1 to generate interrupts at the initial interval
Timer1.initialize(interval_micros);
Timer1.attachInterrupt(timerISR);
}
void loop() {
// do nothing
}
// Interrupt service routine for the encoder pins
void encoderISR() {
static int encoder_pos = 0;
int new_encoder_pos = encoder_pos;
if (digitalRead(ENCODER_PIN_A) == digitalRead(ENCODER_PIN_B)) {
new_encoder_pos--;
} else {
new_encoder_pos++;
}
if (new_encoder_pos > encoder_pos) {
// Encoder turned clockwise
bpm++;
} else if (new_encoder_pos < encoder_pos) {
// Encoder turned counterclockwise
bpm--;
}
encoder_pos = new_encoder_pos;
// Constrain the BPM to reasonable values
bpm = constrain(bpm, 30, 240);
// Update the interval based on the new BPM
interval_micros = (60000000 / bpm);
Timer1.setPeriod(interval_micros);
}
BTW: I know that the encoder physically working since I've tested it with the serial monitor.
For now, im just trying to add an encoder to increase and decrease the BPM and its been already 2 full days on that with no result hehe.
1- Can anyone could help me with the issue in that code ?
2- Also i was wondering which path should i take to achieve the tighter, stable and efficient clock for a musical sequencer. For now im guessing between timerOne and uClock ? But maybe there are better ways ?
Teensy 4.1
Thanks a lot !
My code for now:
#include <TimerOne.h>
// Define the initial BPM and the corresponding interval in microseconds
int bpm = 120;
int interval_micros = (60000000 / bpm);
// Define the pins for the encoder inputs
#define ENCODER_PIN_A 33
#define ENCODER_PIN_B 34
// Define the pin for the clock output
#define CLOCK_PIN 23
// Initialize the timer interrupt
void timerISR() {
static boolean state = false;
digitalWrite(CLOCK_PIN, state);
state = !state;
}
void setup() {
pinMode(CLOCK_PIN, OUTPUT);
pinMode(ENCODER_PIN_A, INPUT_PULLUP);
pinMode(ENCODER_PIN_B, INPUT_PULLUP);
// Attach interrupt to the encoder pins
attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_A), encoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_B), encoderISR, CHANGE);
// Set up Timer1 to generate interrupts at the initial interval
Timer1.initialize(interval_micros);
Timer1.attachInterrupt(timerISR);
}
void loop() {
// do nothing
}
// Interrupt service routine for the encoder pins
void encoderISR() {
static int encoder_pos = 0;
int new_encoder_pos = encoder_pos;
if (digitalRead(ENCODER_PIN_A) == digitalRead(ENCODER_PIN_B)) {
new_encoder_pos--;
} else {
new_encoder_pos++;
}
if (new_encoder_pos > encoder_pos) {
// Encoder turned clockwise
bpm++;
} else if (new_encoder_pos < encoder_pos) {
// Encoder turned counterclockwise
bpm--;
}
encoder_pos = new_encoder_pos;
// Constrain the BPM to reasonable values
bpm = constrain(bpm, 30, 240);
// Update the interval based on the new BPM
interval_micros = (60000000 / bpm);
Timer1.setPeriod(interval_micros);
}
BTW: I know that the encoder physically working since I've tested it with the serial monitor.