-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino.cpp
63 lines (55 loc) · 1.95 KB
/
arduino.cpp
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
#include <Servo.h>
#include <Stepper.h>
#define STEPS 200 // Steps per revolution (adjust for your stepper)
Servo myServo;
Stepper stepperMotor(STEPS, 8, 10, 9, 11); // Change pins if needed
const int servoPin = 6;
int servoAngle = 90; // Start at middle position
int stepperDirection = 0; // 0 = Stop, 1 = CW, -1 = CCW
void setup() {
Serial.begin(9600);
myServo.attach(servoPin);
//myServo.write(servoAngle); // Set initial position
stepperMotor.setSpeed(40); // Adjust stepper speed (RPM)
Serial.println("Waiting for input...");
}
void loop() {
// Step 1: Read Serial Input
if (Serial.available() > 0) {
char command = Serial.read();
Serial.print("Received: ");
Serial.println(command);
// Servo Control (+45° / -45° with X and O Buttons)
if (command == 'X' && servoAngle < 180) {
servoAngle += 50; // Increase by 45°
Serial.print("Rotating Servo to: ");
Serial.println(servoAngle);
myServo.write(servoAngle);
delay(500);
}
else if (command == 'O' && servoAngle > 0) {
servoAngle -= 65; // Decrease by 45°
Serial.print("Rotating Servo to: ");
Serial.println(servoAngle);
myServo.write(servoAngle);
delay(500);
}
// Stepper Motor Control (Joystick)
if (command == 'F') { // Move Stepper Forward (Clockwise)
stepperDirection = 1;
}
else if (command == 'B') { // Move Stepper Backward (Counterclockwise)
stepperDirection = -1;
}
else if (command == 'S') { // Stop Stepper
stepperDirection = 0;
}
}
// Step 2: Move Stepper Motor Continuously While Joystick is Held
if (stepperDirection == 1) {
stepperMotor.step(5); // Small step forward
}
else if (stepperDirection == -1) {
stepperMotor.step(-5); // Small step backward
}
}