-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
201 lines (157 loc) · 3.86 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//screen prerequesets
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// defining stepper pin numbers
const int stepPinX = 3;
const int dirPinX = 52;
const int stepPinY = 2;
const int dirPinY = 53;
//defining magnet pin
const int magPin = 22;
//buzzer pin
const int buzzPin = 50;
//limit swtiches
const int limitX = 45;
const int limitY = 46;
//3 buttons
const int leftButton = 40;
const int rightButton = 41;
const int goButton = 42;
//steps between squares
const int stepsPerSquare
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
// Write to display
lcd.setCursor(4,2);
lcd.print("Initializing");
//start serial connection
Serial.begin(9600);
// Setting stepper pins as output
pinMode(stepPinX,OUTPUT);
pinMode(dirPinX,OUTPUT);
pinMode(stepPinY,OUTPUT);
pinMode(dirPinY,OUTPUT);
// setting magent pin as output
pinMode(magPin,OUTPUT);
//limit switches
pinMode(limitX,INPUT_PULLUP);
pinMode(limitY,INPUT_PULLUP);
// initialize the LCD
lcd.begin();
// Turn on the blacklight
lcd.setBacklight((uint8_t)1);
//stepper home
if (autoHome() != true){
lcd.setCursor(3,2);
lcd.print("Failure During");
lcd.setCursor(5,2);
lcd.print("Auto Home");
buzz(200,3);
}
}
void loop() {
moveStepsX(200,1,500);
moveStepsY(200,1,500);
delay(500);
moveStepsX(200,0,500);
moveStepsY(200,0,500);
delay(500);
buzz(100,2);
}
//Auto Home
bool autoHome(){
//moving x axis to home position
while(readX() != true){
moveStepsX(1,1,800);
lcd.setCursor(1,2);
lcd.print("calibrating X axis");
}
lcd.setCursor(3,2);
lcd.print("X limit reached");
buzz(100,1);
//moving y axis to home position
while (readY() != true){
moveStepsY(1,1,800);
lcd.setCursor(1,2);
lcd.print("calibrating Y axis");
}
lcd.setCursor(3,2);
lcd.print("Y limit reached");
buzz(100,1);
}
//X stepper move function
void moveStepsX(int steps,int dir, int stepSpeed){
/*
steps
direction, 0 = clockwise 1 = Counterwise
speed
*/
if (dir == 0){
digitalWrite(dirPinX,HIGH); // CLockwise
Serial.println("Right");
}else if (dir == 1){
digitalWrite(dirPinX,LOW); // Cunterclockwise
Serial.println("Left");
}
//for loop move steps, 200 == full rotation
for(int x = 0; x < steps; x++) {
digitalWrite(stepPinX,HIGH);
delayMicroseconds(stepSpeed);
digitalWrite(stepPinX,LOW);
delayMicroseconds(stepSpeed);
}
}
//Y stepper move funcrion
void moveStepsY(int steps,int dir, int stepSpeed){
/*
steps
direction, 0 = clockwise 1 = Counterwise
speed
*/
if (dir == 0){
digitalWrite(dirPinY,HIGH); // CLockwise
Serial.println("Right");
}else if (dir == 1){
digitalWrite(dirPinY,LOW); // Cunterclockwise
Serial.println("Left");
}
//for loop move steps, 200 == full rotation
for(int x = 0; x < steps; x++) {
digitalWrite(stepPinY,HIGH);
delayMicroseconds(stepSpeed);
digitalWrite(stepPinY,LOW);
delayMicroseconds(stepSpeed);
}
}
//buzzer function
void buzz(int buzzTime,int buzzes){
for (int x = 0; x < buzzes; x++){
//debug
Serial.println("Buzzed");
//buzzes
digitalWrite(buzzPin,HIGH);
delay(buzzTime);
digitalWrite(buzzPin,LOW);
//dont delay on last buzz
if(buzzes != 1){
delay(buzzTime);
}
}
}
//read x limit switch
bool readX(){
if (digitalRead(limitX) == 1){
return true;
}else if (digitalRead(limitX) == 0){
return false;
}
}
//read y limit switch
bool readY(){
if (digitalRead(limitY) == 1){
return true;
}else if (digitalRead(limitY) == 0){
return false;
}
}