-
Notifications
You must be signed in to change notification settings - Fork 0
/
claw-machine.ino
166 lines (132 loc) · 3.36 KB
/
claw-machine.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <AccelStepper.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
AccelStepper Xaxis(1, 2, 5); // pin 2 = step, pin 5 = direction
AccelStepper Yaxis(1, 3, 6);
AccelStepper Zaxis(1, 4, 7);
const byte enablePin = 8;
constexpr uint32_t stepsPerMm = 4000;
const int XaxisRightEndstop = 9;
const int XaxisLeftEndstop = 43;
const int YaxisEndstop = 10;
const int YaxisNearEndstop = 11;
const int upButton = 24;
const int rightButton = 25;
const int downButton = 26;
const int leftButton = 27;
const int cranePin = 32;
int cranePosition = 0;
Servo crane;
const int actionButton = 50;
const int audioVolume = 30;
const int playerRx = A11;
const int playerTx = A10;
SoftwareSerial playerMP3Serial(playerRx, playerTx);
DFRobotDFPlayerMini playerMP3;
void setup()
{
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
Xaxis.setMaxSpeed(3000);
Xaxis.setAcceleration(3000);
Xaxis.setSpeed(3000);
Yaxis.setMaxSpeed(3000);
Yaxis.setAcceleration(3000);
Yaxis.setSpeed(3000);
Yaxis.setPinsInverted(true);
Zaxis.setMaxSpeed(1000);
Zaxis.setAcceleration(1000);
Zaxis.setSpeed(1000);
crane.attach(cranePin);
craneClose();
delay(500);
craneOpen();
pinMode(upButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(YaxisEndstop, INPUT);
pinMode(XaxisLeftEndstop, INPUT);
pinMode(XaxisRightEndstop, INPUT);
pinMode(YaxisNearEndstop, INPUT);
digitalWrite(upButton, HIGH);
digitalWrite(rightButton, HIGH);
digitalWrite(downButton, HIGH);
digitalWrite(leftButton, HIGH);
digitalWrite(actionButton, HIGH);
digitalWrite(YaxisEndstop, HIGH);
digitalWrite(XaxisRightEndstop, HIGH);
digitalWrite(XaxisLeftEndstop, HIGH);
digitalWrite(YaxisNearEndstop, HIGH);
playerMP3Serial.begin(9600);
if (!playerMP3.begin(playerMP3Serial)) {
while(true){
delay(0);
}
}
playerMP3.volume(audioVolume);
playBgm();
}
void loop() {
if (digitalRead(rightButton) == LOW && digitalRead(XaxisRightEndstop) == HIGH) {
Xaxis.move(50);
}
if (digitalRead(leftButton) == LOW && digitalRead(XaxisLeftEndstop) == HIGH) {
Xaxis.move(-50);
}
if (digitalRead(upButton) == LOW && digitalRead(YaxisEndstop) == HIGH) {
Yaxis.move(50);
}
if (digitalRead(downButton) == LOW && digitalRead(YaxisNearEndstop) == HIGH) {
Yaxis.move(-50);
}
if (digitalRead(actionButton) == LOW) {
craneAction();
}
Xaxis.run();
Yaxis.run();
Zaxis.run();
}
void craneClose()
{
crane.write(35);
}
void craneOpen()
{
crane.write(100);
}
void craneAction()
{
Xaxis.stop();
Yaxis.stop();
playerMP3.advertise(1);
Zaxis.runToNewPosition(1320);
delay(100);
craneClose();
delay(350);
Zaxis.runToNewPosition(0);
Xaxis.run();
Yaxis.run();
delay(1000);
Xaxis.setAcceleration(1000);
Yaxis.setAcceleration(1000);
Xaxis.runToNewPosition(0);
Yaxis.runToNewPosition(0);
// bring X and Y to initial position
while (digitalRead(YaxisNearEndstop) == HIGH) {
Yaxis.runToNewPosition(Yaxis.currentPosition() - 200);
}
while (digitalRead(XaxisLeftEndstop) == HIGH) {
Xaxis.runToNewPosition(Xaxis.currentPosition() - 200);
}
Xaxis.setAcceleration(3000);
Yaxis.setAcceleration(3000);
Yaxis.setCurrentPosition(0);
Xaxis.setCurrentPosition(0);
delay(1000);
craneOpen();
}
void playBgm() {
playerMP3.loopFolder(1);
}