-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsingleSteps.ino
98 lines (73 loc) · 1.91 KB
/
singleSteps.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <LiquidCrystal.h>
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
#define PIN_STEP 2
#define PIN_DIR 3
#define FORWARD HIGH
#define BACKWARD LOW
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int actual_direction;
int step_count;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Step controller");
pinMode(PIN_STEP, OUTPUT);
pinMode(PIN_DIR, OUTPUT);
actual_direction = FORWARD;
digitalWrite(PIN_DIR, actual_direction);
step_count = 0;
update_lcd();
}
void loop() {
int button = read_buttons();
if(button == btnLEFT || button == btnRIGHT) {
if((actual_direction == FORWARD && button == btnLEFT) ||
(actual_direction == BACKWARD && button == btnRIGHT)) change_direction();
make_step();
update_lcd();
delay(200);
}
}
void update_lcd() {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(7, 1);
lcd.print("--");
if(actual_direction == FORWARD) {
for(int i = 0; i < step_count; i++) lcd.print("-");
lcd.print(">");
} else {
for(int i = 0; i < step_count; i++) {
lcd.setCursor(6 - i, 1);
lcd.print("-");
}
lcd.setCursor(7 - step_count - 1, 1);
lcd.print("<");
}
}
void make_step() {
digitalWrite(PIN_STEP, HIGH);
digitalWrite(PIN_STEP, LOW);
step_count++;
if(step_count == 7) step_count = 0;
}
void change_direction() {
if(actual_direction == FORWARD) actual_direction = BACKWARD;
else actual_direction = FORWARD;
digitalWrite(PIN_DIR, actual_direction);
step_count = -1;
}
int read_buttons() {
int adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
}