From 9c05e49b2187cb7d80fa3c2c651ddb33c07d4de0 Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 11 Jan 2021 15:31:01 +0100 Subject: glasslathe2: make steps based on frequency --- linz/glasslathe2/src/main.cpp | 67 +++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 24 deletions(-) (limited to 'linz/glasslathe2/src') diff --git a/linz/glasslathe2/src/main.cpp b/linz/glasslathe2/src/main.cpp index 491c6ca..a106cbc 100644 --- a/linz/glasslathe2/src/main.cpp +++ b/linz/glasslathe2/src/main.cpp @@ -13,7 +13,7 @@ #define MOTOR_FREQUENCY_PIN FREQUENCYTIMER2_PIN #define MOTOR_DIRECTION_PIN 3 -#define POT_JITTER_THRESHOLD 5 +#define POT_JITTER_THRESHOLD 10 float fscale(int inputValue, float originalMin, float originalMax, float newBegin, float newEnd, float curve); @@ -22,6 +22,7 @@ Bounce direction_button; struct { int direction = 0; + int period = 7000; int potValue = 1023; } current, target; bool timer_enabled = false; @@ -30,7 +31,7 @@ void setup() { pinMode(MOTOR_FREQUENCY_PIN, OUTPUT); direction_button.attach(DIRECTION_PIN, INPUT_PULLUP); - target.direction = current.direction = direction_button.read(); + target.direction = current.direction = !direction_button.read(); pinMode(MOTOR_DIRECTION_PIN, OUTPUT); digitalWrite(MOTOR_DIRECTION_PIN, target.direction); @@ -44,7 +45,7 @@ void loop() direction_button.update(); if (direction_button.changed()) { - target.direction = direction_button.read(); + target.direction = !direction_button.read(); Serial.print("target direction: "); Serial.println(target.direction, DEC); } @@ -53,37 +54,55 @@ void loop() if (abs(newPotValue - target.potValue) > POT_JITTER_THRESHOLD) { target.potValue = newPotValue; - Serial.print("target potValue: "); - Serial.println(target.potValue, DEC); + target.period = (int)fscale(target.potValue / 10, 0, 102, 120, 7000, -5); + if (target.period > 6500) + target.period = 7000; + Serial.print("target period: "); + Serial.println(target.period, DEC); } - if (current.potValue != target.potValue || current.direction != target.direction) + const int step_delay = 200; + if (current.period != target.period || current.direction != target.direction) { - if (!timer_enabled && target.potValue != 1023) + if (!timer_enabled && target.period != 7000) { FrequencyTimer2::enable(); timer_enabled = true; Serial.println("enable motor"); - delay(100); + delay(step_delay); } - int step = (current.potValue < 10) ? 2 - : (current.potValue < 100) ? 5 - : (current.potValue < 200) ? 10 - : 20; - int potValue = (current.direction == target.direction) ? target.potValue : 1023; - step = min(abs(potValue - current.potValue), step); - if (potValue < current.potValue) + /*int step = (current.potValue < 20) ? 1 + : (current.potValue < 100) ? 2 + : (current.potValue < 300) ? 3 + : (current.potValue < 400) ? 5 + : (current.potValue < 500) ? 10 + : 20;*/ + int step = (current.period < 150) ? 1 + : (current.period < 200) ? 2 + : (current.period < 250) ? 3 + : (current.period < 300) ? 5 + : (current.period < 400) ? 10 + : (current.period < 500) ? 25 + : (current.period < 1000) ? 50 + : (current.period < 1500) ? 100 + : (current.period < 2000) ? 300 + : (current.period < 3000) ? 500 + : 1000; + int period = (current.direction == target.direction) ? target.period : 7000; + step = min(abs(period - current.period), step); + if (period < current.period) step *= -1; - current.potValue += step; + current.period += step; - int period = (int)fscale(current.potValue / 10, 0, 102, 62, 7000, -5); - FrequencyTimer2::setPeriod(period); - Serial.print("current potValue: "); - Serial.println(current.potValue, DEC); - delay(100); + FrequencyTimer2::setPeriod(current.period); + //Serial.print("current potValue: "); + //Serial.print(current.potValue, DEC); + Serial.print(" current period: "); + Serial.println(current.period, DEC); + delay(step_delay); - if (current.potValue == 1023 && current.direction != target.direction) + if (current.period == 7000 && current.direction != target.direction) { FrequencyTimer2::disable(); timer_enabled = false; @@ -94,7 +113,7 @@ void loop() Serial.print("changed direction to: "); Serial.println(current.direction, DEC); - delay(100); + delay(step_delay); } } else if (timer_enabled && analogRead(POTI_PIN) == 1023) @@ -102,7 +121,7 @@ void loop() FrequencyTimer2::disable(); timer_enabled = false; Serial.println("disable motor"); - delay(100); + delay(step_delay); } #if 0 -- cgit v1.2.3