summaryrefslogtreecommitdiffstats
path: root/linz/glasslathe2/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'linz/glasslathe2/src/main.cpp')
-rw-r--r--linz/glasslathe2/src/main.cpp67
1 files changed, 43 insertions, 24 deletions
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 @@
13#define MOTOR_FREQUENCY_PIN FREQUENCYTIMER2_PIN 13#define MOTOR_FREQUENCY_PIN FREQUENCYTIMER2_PIN
14#define MOTOR_DIRECTION_PIN 3 14#define MOTOR_DIRECTION_PIN 3
15 15
16#define POT_JITTER_THRESHOLD 5 16#define POT_JITTER_THRESHOLD 10
17 17
18float fscale(int inputValue, float originalMin, float originalMax, 18float fscale(int inputValue, float originalMin, float originalMax,
19 float newBegin, float newEnd, float curve); 19 float newBegin, float newEnd, float curve);
@@ -22,6 +22,7 @@ Bounce direction_button;
22 22
23struct { 23struct {
24 int direction = 0; 24 int direction = 0;
25 int period = 7000;
25 int potValue = 1023; 26 int potValue = 1023;
26} current, target; 27} current, target;
27bool timer_enabled = false; 28bool timer_enabled = false;
@@ -30,7 +31,7 @@ void setup() {
30 pinMode(MOTOR_FREQUENCY_PIN, OUTPUT); 31 pinMode(MOTOR_FREQUENCY_PIN, OUTPUT);
31 32
32 direction_button.attach(DIRECTION_PIN, INPUT_PULLUP); 33 direction_button.attach(DIRECTION_PIN, INPUT_PULLUP);
33 target.direction = current.direction = direction_button.read(); 34 target.direction = current.direction = !direction_button.read();
34 pinMode(MOTOR_DIRECTION_PIN, OUTPUT); 35 pinMode(MOTOR_DIRECTION_PIN, OUTPUT);
35 digitalWrite(MOTOR_DIRECTION_PIN, target.direction); 36 digitalWrite(MOTOR_DIRECTION_PIN, target.direction);
36 37
@@ -44,7 +45,7 @@ void loop()
44 direction_button.update(); 45 direction_button.update();
45 if (direction_button.changed()) 46 if (direction_button.changed())
46 { 47 {
47 target.direction = direction_button.read(); 48 target.direction = !direction_button.read();
48 Serial.print("target direction: "); 49 Serial.print("target direction: ");
49 Serial.println(target.direction, DEC); 50 Serial.println(target.direction, DEC);
50 } 51 }
@@ -53,37 +54,55 @@ void loop()
53 if (abs(newPotValue - target.potValue) > POT_JITTER_THRESHOLD) 54 if (abs(newPotValue - target.potValue) > POT_JITTER_THRESHOLD)
54 { 55 {
55 target.potValue = newPotValue; 56 target.potValue = newPotValue;
56 Serial.print("target potValue: "); 57 target.period = (int)fscale(target.potValue / 10, 0, 102, 120, 7000, -5);
57 Serial.println(target.potValue, DEC); 58 if (target.period > 6500)
59 target.period = 7000;
60 Serial.print("target period: ");
61 Serial.println(target.period, DEC);
58 } 62 }
59 63
60 if (current.potValue != target.potValue || current.direction != target.direction) 64 const int step_delay = 200;
65 if (current.period != target.period || current.direction != target.direction)
61 { 66 {
62 if (!timer_enabled && target.potValue != 1023) 67 if (!timer_enabled && target.period != 7000)
63 { 68 {
64 FrequencyTimer2::enable(); 69 FrequencyTimer2::enable();
65 timer_enabled = true; 70 timer_enabled = true;
66 Serial.println("enable motor"); 71 Serial.println("enable motor");
67 delay(100); 72 delay(step_delay);
68 } 73 }
69 74
70 int step = (current.potValue < 10) ? 2 75 /*int step = (current.potValue < 20) ? 1
71 : (current.potValue < 100) ? 5 76 : (current.potValue < 100) ? 2
72 : (current.potValue < 200) ? 10 77 : (current.potValue < 300) ? 3
73 : 20; 78 : (current.potValue < 400) ? 5
74 int potValue = (current.direction == target.direction) ? target.potValue : 1023; 79 : (current.potValue < 500) ? 10
75 step = min(abs(potValue - current.potValue), step); 80 : 20;*/
76 if (potValue < current.potValue) 81 int step = (current.period < 150) ? 1
82 : (current.period < 200) ? 2
83 : (current.period < 250) ? 3
84 : (current.period < 300) ? 5
85 : (current.period < 400) ? 10
86 : (current.period < 500) ? 25
87 : (current.period < 1000) ? 50
88 : (current.period < 1500) ? 100
89 : (current.period < 2000) ? 300
90 : (current.period < 3000) ? 500
91 : 1000;
92 int period = (current.direction == target.direction) ? target.period : 7000;
93 step = min(abs(period - current.period), step);
94 if (period < current.period)
77 step *= -1; 95 step *= -1;
78 current.potValue += step; 96 current.period += step;
79 97
80 int period = (int)fscale(current.potValue / 10, 0, 102, 62, 7000, -5); 98 FrequencyTimer2::setPeriod(current.period);
81 FrequencyTimer2::setPeriod(period); 99 //Serial.print("current potValue: ");
82 Serial.print("current potValue: "); 100 //Serial.print(current.potValue, DEC);
83 Serial.println(current.potValue, DEC); 101 Serial.print(" current period: ");
84 delay(100); 102 Serial.println(current.period, DEC);
103 delay(step_delay);
85 104
86 if (current.potValue == 1023 && current.direction != target.direction) 105 if (current.period == 7000 && current.direction != target.direction)
87 { 106 {
88 FrequencyTimer2::disable(); 107 FrequencyTimer2::disable();
89 timer_enabled = false; 108 timer_enabled = false;
@@ -94,7 +113,7 @@ void loop()
94 Serial.print("changed direction to: "); 113 Serial.print("changed direction to: ");
95 Serial.println(current.direction, DEC); 114 Serial.println(current.direction, DEC);
96 115
97 delay(100); 116 delay(step_delay);
98 } 117 }
99 } 118 }
100 else if (timer_enabled && analogRead(POTI_PIN) == 1023) 119 else if (timer_enabled && analogRead(POTI_PIN) == 1023)
@@ -102,7 +121,7 @@ void loop()
102 FrequencyTimer2::disable(); 121 FrequencyTimer2::disable();
103 timer_enabled = false; 122 timer_enabled = false;
104 Serial.println("disable motor"); 123 Serial.println("disable motor");
105 delay(100); 124 delay(step_delay);
106 } 125 }
107 126
108#if 0 127#if 0