stepper demo

main
radex 2024-02-20 18:10:48 +01:00
parent 0938fe70b1
commit 53d28add10
Signed by: radex
SSH Key Fingerprint: SHA256:hvqRXAGG1h89yqnS+cyFTLKQbzjWD4uXIqw7Y+0ws30
2 changed files with 16 additions and 0 deletions

View File

@ -32,6 +32,17 @@ void loop() {
debugScanI2C();
char debugKey = Serial.available() > 0 ? Serial.read() : 0;
if (debugKey == '1') {
setStepperEnabled(true);
} else if (debugKey == '2') {
setStepperEnabled(false);
} else if (debugKey == 'l') {
setStepperDirection(LEFT);
} else if (debugKey == 'r') {
setStepperDirection(RIGHT);
} else if (debugKey == '-') {
setTimerEnabled(!isTimerEnabled());
}
auto key = handleKeyboard();
if (key == KEY_NONE && debugKey) {
@ -88,5 +99,8 @@ void loop() {
if (encoderEvent) {
tick();
Serial.println(encoderEvent == ENCODER_CLOCKWISE ? "Clockwise" : "Counter-clockwise");
setTimerCadence(getTimerCadence() + (encoderEvent == ENCODER_CLOCKWISE ? -1 : 1));
Serial.println(OCR1A);
Serial.println(TCNT1);
}
}

View File

@ -64,6 +64,8 @@ uint16_t getTimerCadence() {
void setTimerCadence(uint16_t cadence) {
noInterrupts();
// if TCNT1 > OCR1A, it won't be reset until it reaches uint16_t's max value
TCNT1 = min(TCNT1, cadence - 1);
// interrupt at 62.5kHz / (OCR1A + 1)
OCR1A = cadence;
interrupts();