#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "control.h"
#include "stepper.h"
#include "relay.h"
#include "display.h"
#include "endstop.h"
#include "pin.h"
#include "button.h"
const int X_STEPS_PER_REVOLUTION = 200;
const int X_MAX_STROKE = 1000;
const int X_MAX_ACCELERATION = 50;
const int Y_STEPS_PER_REVOLUTION = 200;
const int Y_MAX_STROKE = 1000;
const int Y_MAX_ACCELERATION = 50;
int readEncoderX();
int readEncoderY();
int readEncoderZ();
int readEncoderA();
int x_axis_position;
int drilling_acceleration;
int y_axis_position;
int drilling_depth;
int start_y = 50;
int speed = 500;
int num_points = 10;
int drill_points[10][2] = {{0,0}, {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}, {8,8}, {9,9}};
int drill_time = 1000;
int end_y = 100;
int end_x = 100;
int Y_ACCELERATION = 1000;
//LiquidCrystal_I2C lcd(0x27, 20, 4);
//int x_axis_position, y_axis_position, drilling_depth, drilling_acceleration;
void setup() {
// Initialize LCD
lcd.begin(20, 4);
lcd.backlight();
lcd.clear();
// Initialize endstop pins
pinMode(X_ENDSTOP_PIN, INPUT_PULLUP);
pinMode(Y_ENDSTOP_PIN, INPUT_PULLUP);
// Initialize relay pins
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
// Initialize button pins
pinMode(START_BUTTON, INPUT_PULLUP);
pinMode(PAUSE_BUTTON, INPUT_PULLUP);
pinMode(STOP_BUTTON, INPUT_PULLUP);
}
// Initialize stepper motors
void initStepperX() {
Stepper stepperX(X_STEP_PIN, X_DIR_PIN, X_ENABLE_PIN, X_ENDSTOP_PIN, X_STEPS_PER_REVOLUTION, X_MAX_STROKE, X_MAX_ACCELERATION);
}
void initStepperY() {
Stepper stepperY(Y_STEP_PIN, Y_DIR_PIN, Y_ENABLE_PIN, Y_ENDSTOP_PIN, Y_STEPS_PER_REVOLUTION, Y_MAX_STROKE, Y_MAX_ACCELERATION);
}
void initStepperX(int x_axis_position, int drilling_acceleration) {
}
void initStepperY(int start_y, int speed) {
}
void loop() {
// Read values from encoder
x_axis_position = readEncoderX();
y_axis_position = readEncoderY();
drilling_depth = readEncoderZ();
drilling_acceleration = readEncoderA();
// Display values on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("X axis position: ");
lcd.println(x_axis_position);
lcd.setCursor(0, 1);
lcd.print("Y axis position: ");
lcd.println(y_axis_position);
lcd.setCursor(0, 2);
lcd.print("Drilling depth: ");
lcd.println(drilling_depth);
lcd.setCursor(0, 3);
lcd.print("Acceleration: ");
lcd.println(drilling_acceleration);
// Wait for start button to be pressed
while (!digitalRead(START_BUTTON)) {
// Wait for start button to be released
while (!digitalRead(START_BUTTON)) {}
}
// Turn on spindle motor
digitalWrite(RELAY1_PIN, HIGH);
// Turn on air cylinder
digitalWrite(RELAY2_PIN, HIGH);
// Move X axis to starting position
initStepperX(x_axis_position, drilling_acceleration);
// Loop for each drilling position
for (int i = 0; i < x_axis_position; i++) {
// Check for endstop
if (digitalRead(X_ENDSTOP_PIN) == LOW) {
break;
}
// Move Y axis to start drilling
initStepperY(start_y, speed);
// Turn on relay for spindle motor
digitalWrite(RELAY1_PIN, HIGH);
delay(500);
// Start drilling process
for (int i = 0; i < num_points; i++) {
initStepperX(drill_points[i][0], speed);
delay(500);
initStepperY(drill_points[i][1], speed);
delay(500);
// Turn off relay for spindle motor
digitalWrite(RELAY1_PIN, LOW);
delay(drill_time);
// Turn on relay for spindle motor
digitalWrite(RELAY1_PIN, HIGH);
delay(500);
// Move Y axis up 50mm
initStepperY(drill_points[i][1] + 50, speed);
delay(500);
}
// Turn off relay for spindle motor
digitalWrite(RELAY1_PIN, LOW);
// Move Y axis to end position
initStepperY(end_y, speed);
delay(500);
// Turn on relay for air cylinder
digitalWrite(RELAY2_PIN, HIGH);
delay(500);
// Remove product
initStepperY(end_x, speed);
delay(500);
// Turn off relay for air cylinder
digitalWrite(RELAY2_PIN, LOW);
delay(500);
}
void loop() {
// Store the number of drilling points, drilling acceleration and depth values in program memory
int numOfDrillingPoints = readFromEncoder();
int drillingAccelerationX = getXAcceleration();
int drillingAccelerationY = getYAcceleration();
int drillingDepth = getDrillingDepth();
// Turn on relay 1 to turn on spindle motor
digitalWrite(RELAY1_PIN, HIGH);
// Turn on relay 2 to clamp product
digitalWrite(RELAY2_PIN, HIGH);
for (int i = 0; i < numOfDrillingPoints; i++)
// Move X axis to the position to be drilled
moveStepperX(drillingAccelerationX, drillingDepth);
// Move Y axis to start drilling
moveStepperY(drillingAccelerationY, drillingDepth);
// Check if pause button is pressed
if (checkPauseButton())
// Turn off spindle motor
digitalWrite(RELAY1_PIN, LOW);
// Turn off relay for air cylinder
digitalWrite(RELAY2_PIN, LOW);
delay(500);
// Wait until pause button is released
while (checkPauseButton());
// Turn on relay 1 to turn on spindle motor
digitalWrite(RELAY1_PIN, HIGH);
// Turn on relay 2 to clamp product again
digitalWrite(RELAY2_PIN, HIGH);
// Check if stop button is pressed
if (checkStopButton())
// Turn off spindle motor
digitalWrite(RELAY1_PIN, LOW);
// Turn off relay for air cylinder
digitalWrite(RELAY2_PIN, LOW);
delay(500);
// Move Y axis up 50mm to clear the drilled material
moveStepperY(drillingAccelerationY, 50);
// Move Y axis back to starting position
moveStepperY(0, Y_MAX_STROKE, Y_ACCELERATION);
delay(500);
// Turn off spindle motor
digitalWrite(RELAY1_PIN, LOW);
// Turn off relay for air cylinder
digitalWrite(RELAY2_PIN, LOW);
delay(500);
}
can anyone rewrite this main.cpp to make it error free. I canβt fix it
Actually, there are 2 errors:
1st: Your sketch contains two loop()
functions!
2nd: The first loop()
is missing a closing curling bracket β}β at the end of the function.
1 Like