From 0988bbec7e2a8dfd36108ded1c5821c93b031354 Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 27 Dec 2020 12:14:24 +0100 Subject: glasslathe2 (mega 328) version --- linz/glasslathe2/.gitignore | 5 ++ linz/glasslathe2/platformio.ini | 15 ++++ linz/glasslathe2/src/main.cpp | 172 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 linz/glasslathe2/.gitignore create mode 100644 linz/glasslathe2/platformio.ini create mode 100644 linz/glasslathe2/src/main.cpp (limited to 'linz') diff --git a/linz/glasslathe2/.gitignore b/linz/glasslathe2/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/linz/glasslathe2/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/linz/glasslathe2/platformio.ini b/linz/glasslathe2/platformio.ini new file mode 100644 index 0000000..e5eb57b --- /dev/null +++ b/linz/glasslathe2/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:nanoatmega328new] +platform = atmelavr +board = nanoatmega328new +framework = arduino +lib_deps = paulstoffregen/FrequencyTimer2, Bounce2 \ No newline at end of file diff --git a/linz/glasslathe2/src/main.cpp b/linz/glasslathe2/src/main.cpp new file mode 100644 index 0000000..491c6ca --- /dev/null +++ b/linz/glasslathe2/src/main.cpp @@ -0,0 +1,172 @@ +#include +#include +#include + +// PIN A1 potentiometer +// PIN D2 direction in +// PIN D3 motor direction out +// PIN D10 +// PIN D11 motor frequency out + +#define POTI_PIN A1 +#define DIRECTION_PIN 2 +#define MOTOR_FREQUENCY_PIN FREQUENCYTIMER2_PIN +#define MOTOR_DIRECTION_PIN 3 + +#define POT_JITTER_THRESHOLD 5 + +float fscale(int inputValue, float originalMin, float originalMax, + float newBegin, float newEnd, float curve); + +Bounce direction_button; + +struct { + int direction = 0; + int potValue = 1023; +} current, target; +bool timer_enabled = false; + +void setup() { + pinMode(MOTOR_FREQUENCY_PIN, OUTPUT); + + direction_button.attach(DIRECTION_PIN, INPUT_PULLUP); + target.direction = current.direction = direction_button.read(); + pinMode(MOTOR_DIRECTION_PIN, OUTPUT); + digitalWrite(MOTOR_DIRECTION_PIN, target.direction); + + Serial.begin(9600); + FrequencyTimer2::setPeriod(7000); + FrequencyTimer2::disable(); +} + +void loop() +{ + direction_button.update(); + if (direction_button.changed()) + { + target.direction = direction_button.read(); + Serial.print("target direction: "); + Serial.println(target.direction, DEC); + } + + int newPotValue = analogRead(POTI_PIN); + if (abs(newPotValue - target.potValue) > POT_JITTER_THRESHOLD) + { + target.potValue = newPotValue; + Serial.print("target potValue: "); + Serial.println(target.potValue, DEC); + } + + if (current.potValue != target.potValue || current.direction != target.direction) + { + if (!timer_enabled && target.potValue != 1023) + { + FrequencyTimer2::enable(); + timer_enabled = true; + Serial.println("enable motor"); + delay(100); + } + + 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) + step *= -1; + current.potValue += 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); + + if (current.potValue == 1023 && current.direction != target.direction) + { + FrequencyTimer2::disable(); + timer_enabled = false; + Serial.println("disable motor"); + + current.direction = target.direction; + digitalWrite(MOTOR_DIRECTION_PIN, target.direction); + Serial.print("changed direction to: "); + Serial.println(current.direction, DEC); + + delay(100); + } + } + else if (timer_enabled && analogRead(POTI_PIN) == 1023) + { + FrequencyTimer2::disable(); + timer_enabled = false; + Serial.println("disable motor"); + delay(100); + } + +#if 0 + static unsigned long v = 0; + if (Serial.available()) { + char ch = Serial.read(); + switch(ch) { + case '0'...'9': + v = v * 10 + ch - '0'; + break; + case 'p': + FrequencyTimer2::setPeriod(v); + Serial.print("set "); + Serial.print((long)v, DEC); + Serial.print(" = "); + Serial.print((long)FrequencyTimer2::getPeriod(), DEC); + Serial.println(); + v = 0; + break; + case 'e': + FrequencyTimer2::enable(); + break; + case 'd': + FrequencyTimer2::disable(); + break; + break; + case 'n': + FrequencyTimer2::setOnOverflow(0); + break; + } + } +#endif +} + +float fscale(int inputValue, float originalMin, float originalMax, + float newBegin, float newEnd, float curve) +{ + // condition curve parameter + curve = constrain(curve, -10, 10); + curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output + curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function + + // Check for out of range inputValues + inputValue = constrain(inputValue, originalMin, originalMax); + + // Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine + if (originalMin > originalMax) + return 0; + + // Zero Refference the values + float originalRange = originalMax - originalMin; + float zeroRefCurVal = inputValue - originalMin; + float normalizedCurVal = zeroRefCurVal / originalRange; // normalize to 0 - 1 float + + float rangedValue = 0; + if (newEnd > newBegin) + { + float newRange = newEnd - newBegin; + rangedValue = (pow(normalizedCurVal, curve) * newRange) + newBegin; + } + else + { + float newRange = newBegin - newEnd; + rangedValue = newBegin - (pow(normalizedCurVal, curve) * newRange); + } + return rangedValue; +} -- cgit v1.2.3