diff options
Diffstat (limited to 'linz/glasslathe2/src/main.cpp')
| -rw-r--r-- | linz/glasslathe2/src/main.cpp | 172 |
1 files changed, 172 insertions, 0 deletions
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 @@ | |||
| 1 | #include <Arduino.h> | ||
| 2 | #include <FrequencyTimer2.h> | ||
| 3 | #include <Bounce2.h> | ||
| 4 | |||
| 5 | // PIN A1 potentiometer | ||
| 6 | // PIN D2 direction in | ||
| 7 | // PIN D3 motor direction out | ||
| 8 | // PIN D10 <occupied by timer2> | ||
| 9 | // PIN D11 motor frequency out | ||
| 10 | |||
| 11 | #define POTI_PIN A1 | ||
| 12 | #define DIRECTION_PIN 2 | ||
| 13 | #define MOTOR_FREQUENCY_PIN FREQUENCYTIMER2_PIN | ||
| 14 | #define MOTOR_DIRECTION_PIN 3 | ||
| 15 | |||
| 16 | #define POT_JITTER_THRESHOLD 5 | ||
| 17 | |||
| 18 | float fscale(int inputValue, float originalMin, float originalMax, | ||
| 19 | float newBegin, float newEnd, float curve); | ||
| 20 | |||
| 21 | Bounce direction_button; | ||
| 22 | |||
| 23 | struct { | ||
| 24 | int direction = 0; | ||
| 25 | int potValue = 1023; | ||
| 26 | } current, target; | ||
| 27 | bool timer_enabled = false; | ||
| 28 | |||
| 29 | void setup() { | ||
| 30 | pinMode(MOTOR_FREQUENCY_PIN, OUTPUT); | ||
| 31 | |||
| 32 | direction_button.attach(DIRECTION_PIN, INPUT_PULLUP); | ||
| 33 | target.direction = current.direction = direction_button.read(); | ||
| 34 | pinMode(MOTOR_DIRECTION_PIN, OUTPUT); | ||
| 35 | digitalWrite(MOTOR_DIRECTION_PIN, target.direction); | ||
| 36 | |||
| 37 | Serial.begin(9600); | ||
| 38 | FrequencyTimer2::setPeriod(7000); | ||
| 39 | FrequencyTimer2::disable(); | ||
| 40 | } | ||
| 41 | |||
| 42 | void loop() | ||
| 43 | { | ||
| 44 | direction_button.update(); | ||
| 45 | if (direction_button.changed()) | ||
| 46 | { | ||
| 47 | target.direction = direction_button.read(); | ||
| 48 | Serial.print("target direction: "); | ||
| 49 | Serial.println(target.direction, DEC); | ||
| 50 | } | ||
| 51 | |||
| 52 | int newPotValue = analogRead(POTI_PIN); | ||
| 53 | if (abs(newPotValue - target.potValue) > POT_JITTER_THRESHOLD) | ||
| 54 | { | ||
| 55 | target.potValue = newPotValue; | ||
| 56 | Serial.print("target potValue: "); | ||
| 57 | Serial.println(target.potValue, DEC); | ||
| 58 | } | ||
| 59 | |||
| 60 | if (current.potValue != target.potValue || current.direction != target.direction) | ||
| 61 | { | ||
| 62 | if (!timer_enabled && target.potValue != 1023) | ||
| 63 | { | ||
| 64 | FrequencyTimer2::enable(); | ||
| 65 | timer_enabled = true; | ||
| 66 | Serial.println("enable motor"); | ||
| 67 | delay(100); | ||
| 68 | } | ||
| 69 | |||
| 70 | int step = (current.potValue < 10) ? 2 | ||
| 71 | : (current.potValue < 100) ? 5 | ||
| 72 | : (current.potValue < 200) ? 10 | ||
| 73 | : 20; | ||
| 74 | int potValue = (current.direction == target.direction) ? target.potValue : 1023; | ||
| 75 | step = min(abs(potValue - current.potValue), step); | ||
| 76 | if (potValue < current.potValue) | ||
| 77 | step *= -1; | ||
| 78 | current.potValue += step; | ||
| 79 | |||
| 80 | int period = (int)fscale(current.potValue / 10, 0, 102, 62, 7000, -5); | ||
| 81 | FrequencyTimer2::setPeriod(period); | ||
| 82 | Serial.print("current potValue: "); | ||
| 83 | Serial.println(current.potValue, DEC); | ||
| 84 | delay(100); | ||
| 85 | |||
| 86 | if (current.potValue == 1023 && current.direction != target.direction) | ||
| 87 | { | ||
| 88 | FrequencyTimer2::disable(); | ||
| 89 | timer_enabled = false; | ||
| 90 | Serial.println("disable motor"); | ||
| 91 | |||
| 92 | current.direction = target.direction; | ||
| 93 | digitalWrite(MOTOR_DIRECTION_PIN, target.direction); | ||
| 94 | Serial.print("changed direction to: "); | ||
| 95 | Serial.println(current.direction, DEC); | ||
| 96 | |||
| 97 | delay(100); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | else if (timer_enabled && analogRead(POTI_PIN) == 1023) | ||
| 101 | { | ||
| 102 | FrequencyTimer2::disable(); | ||
| 103 | timer_enabled = false; | ||
| 104 | Serial.println("disable motor"); | ||
| 105 | delay(100); | ||
| 106 | } | ||
| 107 | |||
| 108 | #if 0 | ||
| 109 | static unsigned long v = 0; | ||
| 110 | if (Serial.available()) { | ||
| 111 | char ch = Serial.read(); | ||
| 112 | switch(ch) { | ||
| 113 | case '0'...'9': | ||
| 114 | v = v * 10 + ch - '0'; | ||
| 115 | break; | ||
| 116 | case 'p': | ||
| 117 | FrequencyTimer2::setPeriod(v); | ||
| 118 | Serial.print("set "); | ||
| 119 | Serial.print((long)v, DEC); | ||
| 120 | Serial.print(" = "); | ||
| 121 | Serial.print((long)FrequencyTimer2::getPeriod(), DEC); | ||
| 122 | Serial.println(); | ||
| 123 | v = 0; | ||
| 124 | break; | ||
| 125 | case 'e': | ||
| 126 | FrequencyTimer2::enable(); | ||
| 127 | break; | ||
| 128 | case 'd': | ||
| 129 | FrequencyTimer2::disable(); | ||
| 130 | break; | ||
| 131 | break; | ||
| 132 | case 'n': | ||
| 133 | FrequencyTimer2::setOnOverflow(0); | ||
| 134 | break; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | #endif | ||
| 138 | } | ||
| 139 | |||
| 140 | float fscale(int inputValue, float originalMin, float originalMax, | ||
| 141 | float newBegin, float newEnd, float curve) | ||
| 142 | { | ||
| 143 | // condition curve parameter | ||
| 144 | curve = constrain(curve, -10, 10); | ||
| 145 | curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output | ||
| 146 | curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function | ||
| 147 | |||
| 148 | // Check for out of range inputValues | ||
| 149 | inputValue = constrain(inputValue, originalMin, originalMax); | ||
| 150 | |||
| 151 | // Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine | ||
| 152 | if (originalMin > originalMax) | ||
| 153 | return 0; | ||
| 154 | |||
| 155 | // Zero Refference the values | ||
| 156 | float originalRange = originalMax - originalMin; | ||
| 157 | float zeroRefCurVal = inputValue - originalMin; | ||
| 158 | float normalizedCurVal = zeroRefCurVal / originalRange; // normalize to 0 - 1 float | ||
| 159 | |||
| 160 | float rangedValue = 0; | ||
| 161 | if (newEnd > newBegin) | ||
| 162 | { | ||
| 163 | float newRange = newEnd - newBegin; | ||
| 164 | rangedValue = (pow(normalizedCurVal, curve) * newRange) + newBegin; | ||
| 165 | } | ||
| 166 | else | ||
| 167 | { | ||
| 168 | float newRange = newBegin - newEnd; | ||
| 169 | rangedValue = newBegin - (pow(normalizedCurVal, curve) * newRange); | ||
| 170 | } | ||
| 171 | return rangedValue; | ||
| 172 | } | ||
