forked from david-a-krupp/3D-Microstat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController_Arduino_Script
More file actions
99 lines (83 loc) · 2.38 KB
/
Copy pathController_Arduino_Script
File metadata and controls
99 lines (83 loc) · 2.38 KB
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
99
#include <AccelStepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin Definitions
#define MOTOR1_STEP_PIN 2 // Large axis
#define MOTOR1_DIR_PIN 3
#define MOTOR2_STEP_PIN 4 // Small axis
#define MOTOR2_DIR_PIN 5
#define BUTTON_PIN 7 // Start/Stop button
// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Stepper Setup
AccelStepper motor1(AccelStepper::DRIVER, MOTOR1_STEP_PIN, MOTOR1_DIR_PIN);
AccelStepper motor2(AccelStepper::DRIVER, MOTOR2_STEP_PIN, MOTOR2_DIR_PIN);
// State
bool running = false;
unsigned long startTime = 0;
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Microstat Ready");
lcd.setCursor(0, 1);
lcd.print("Press Button...");
// Button setup
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Set motor speeds for 1/16 microstepping
// Large axis: 10 RPM => ~533 steps/sec
// Small axis: 4.5 RPM => ~240 steps/sec
motor1.setMaxSpeed(533);
motor1.setSpeed(533);
motor2.setMaxSpeed(240);
motor2.setSpeed(240);
}
void loop() {
static bool lastButtonState = HIGH;
bool buttonState = digitalRead(BUTTON_PIN);
// Toggle running state on button press
if (lastButtonState == HIGH && buttonState == LOW) {
running = !running;
if (running) {
// Just started: record time
startTime = millis();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Microstat Running");
} else {
// Stopped: show ready screen again
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Microstat Ready");
lcd.setCursor(0, 1);
lcd.print("Press Button...");
}
delay(300); // debounce
}
lastButtonState = buttonState;
if (running) {
motor1.runSpeed();
motor2.runSpeed();
// Calculate elapsed time in ms
unsigned long elapsedMillis = millis() - startTime;
// Convert to days, hours, mins, secs
unsigned long totalSecs = elapsedMillis / 1000;
unsigned int days = totalSecs / 86400;
unsigned int hours = (totalSecs % 86400) / 3600;
unsigned int mins = (totalSecs % 3600) / 60;
unsigned int secs = totalSecs % 60;
// Display on LCD line 2
lcd.setCursor(0, 1);
lcd.print(days);
lcd.print("d ");
if (hours < 10) lcd.print('0');
lcd.print(hours);
lcd.print(":");
if (mins < 10) lcd.print('0');
lcd.print(mins);
lcd.print(":");
if (secs < 10) lcd.print('0');
lcd.print(secs);
}
}