Thank you guys for your replys! But I´m really looking for a way to customize the GRBL code to include some phisical buttons to a single arduino with the GRBL installed on it, and wanna know wich files I should edit to include my code?
Is there a simple way to do it? As I said, I´m not a programmer and I´m actually using CHAT-GPT to help me generate those codes. And asking to it about to edit the GRBL´s source code, it gave me these answers:
- Download and Prepare the GRBL Source Code First, download the GRBL source code from the official GRBL repository on GitHub. Extract the content and open it in your Arduino development environment.
- Configure the Pins for the Buttons You can define the button pins in the config.h file or directly in the main firmware file. We will define the pins directly in the code for simplicity.
- Add the Button Reading Logic You need to add the button reading logic and G-code command sending in the main loop of GRBL. To do this, you can modify the report.c file (or another suitable file that contains the report_status_message function).
Here is an example of how to add this logic:
Step 1: Define the button pins and other constants At the beginning of the report.c file (or another suitable file), add the definitions for the button pins and other constants:
#include <Arduino.h> // Certifique-se de incluir a biblioteca Arduino
const int BUTTON_PIN = 4;
const int NEW_BUTTON_PIN_1 = 7;
const int NEW_BUTTON_PIN_2 = 8;
const int NEW_BUTTON_PIN_3 = 9;
const int ROTATE_BUTTON_PIN = 13;
const int EMERGENCY_STOP_PIN = 12;
const int ONE_ROTATE_BUTTON_PIN = 11;
const int TEN_ROTATE_BUTTON_PIN = 10;
const int MAX_SPEED = 4000; // mm/min
const int QUARTER_MAX_SPEED = MAX_SPEED / 4;
bool invertDirection = false; // Controla a inversão de sentido de ambos os motores
bool motorRunning = false;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(NEW_BUTTON_PIN_1, INPUT_PULLUP);
pinMode(NEW_BUTTON_PIN_2, INPUT_PULLUP);
pinMode(NEW_BUTTON_PIN_3, INPUT_PULLUP);
pinMode(ROTATE_BUTTON_PIN, INPUT_PULLUP);
pinMode(EMERGENCY_STOP_PIN, INPUT_PULLUP);
pinMode(ONE_ROTATE_BUTTON_PIN, INPUT_PULLUP);
pinMode(TEN_ROTATE_BUTTON_PIN, INPUT_PULLUP);
Serial.begin(115200);
}
void sendGcode(const char* gcode) {
Serial.println(gcode); // Para monitoramento
grblSerial.println(gcode); // Enviar comando para GRBL
delay(50); // Pequeno atraso para garantir que o comando foi enviado
}
void toggleMotors(bool run, int speed, bool invert) {
if (run) {
sendGcode("G91"); // Modo de coordenadas relativo
char gcode[50];
sprintf(gcode, "G1 F%d", speed); // Define a velocidade de avanço
sendGcode(gcode);
// Movimento relativo com base na direção atual dos motores
sprintf(gcode, "G1 X%d Y%d", invert ? -2000 : 2000, invert ? 2000 : -2000); // Exemplo de comando G-code
sendGcode(gcode);
sendGcode("G90"); // Retornar ao modo absoluto
} else {
sendGcode("M5"); // Parar os motores
}
}
void rotateMotors(int numVoltas) {
char gcode[50];
sprintf(gcode, "G1 F%d", QUARTER_MAX_SPEED); // Define a velocidade de avanço
sendGcode(gcode);
long numPassos = numVoltas * 200; // 200 passos por volta (exemplo)
sprintf(gcode, "G91 G1 X%d Y%d", invertDirection ? -numPassos : numPassos, invertDirection ? numPassos : -numPassos); // Exemplo de comando G-code
sendGcode(gcode);
// Retornar ao modo absoluto após a rotação
sendGcode("G90");
}
void emergencyStop() {
sendGcode("!"); // Comando de pausa em GRBL
motorRunning = false;
}
Step 2: Add the Button Reading Logic in the Main Loop
In the main loop (usually located in report.c or grbl_main.c), add the logic to read the button states and send the corresponding G-code commands:
Copy code
void loop() {
bool buttonState = digitalRead(BUTTON_PIN);
bool newButtonState1 = digitalRead(NEW_BUTTON_PIN_1);
bool newButtonState2 = digitalRead(NEW_BUTTON_PIN_2);
bool newButtonState3 = digitalRead(NEW_BUTTON_PIN_3);
bool rotateButtonState = digitalRead(ROTATE_BUTTON_PIN);
bool oneRotateButtonState = digitalRead(ONE_ROTATE_BUTTON_PIN);
bool tenRotateButtonState = digitalRead(TEN_ROTATE_BUTTON_PIN);
bool emergencyStopState = digitalRead(EMERGENCY_STOP_PIN);
unsigned long currentMillis = millis();
if (emergencyStopState == LOW && lastEmergencyStopState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
emergencyStop();
lastDebounceTime = currentMillis;
}
if (rotateButtonState == LOW && lastRotateButtonState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
invertDirection = !invertDirection; // Inverte o sentido de rotação armazenado
updateLEDs(); // Atualiza os LEDs
lastDebounceTime = currentMillis;
}
if (buttonState == LOW && lastButtonState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
// Comando customizado para o botão do pino 4
sendGcode("$G");
sendGcode("G10 P0 L20 X0 Y0 Z0");
sendGcode("G1 X-1000 Y1000 F1500");
sendGcode("G10 P0 L20 X0 Y0 Z0");
lastDebounceTime = currentMillis;
}
if (newButtonState1 == LOW && lastNewButtonState1 == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
motorRunning = !motorRunning;
toggleMotors(motorRunning, MAX_SPEED / 2, invertDirection);
lastDebounceTime = currentMillis;
}
if (newButtonState2 == LOW && lastNewButtonState2 == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
motorRunning = !motorRunning;
toggleMotors(motorRunning, MAX_SPEED / 4, invertDirection);
lastDebounceTime = currentMillis;
}
if (newButtonState3 == LOW && lastNewButtonState3 == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
motorRunning = !motorRunning;
toggleMotors(motorRunning, MAX_SPEED / 10, invertDirection);
lastDebounceTime = currentMillis;
}
if (oneRotateButtonState == LOW && lastOneRotateButtonState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
rotateMotors(1); // Exemplo: Girar 1 volta
lastDebounceTime = currentMillis;
}
if (tenRotateButtonState == LOW && lastTenRotateButtonState == HIGH && currentMillis - lastDebounceTime > debounceDelay) {
rotateMotors(10); // Exemplo: Girar 10 voltas
lastDebounceTime = currentMillis;
}
lastButtonState = buttonState;
lastNewButtonState1 = newButtonState1;
lastNewButtonState2 = newButtonState2;
lastNewButtonState3 = newButtonState3;
lastRotateButtonState = rotateButtonState;
lastOneRotateButtonState = oneRotateButtonState;
lastTenRotateButtonState = tenRotateButtonState;
lastEmergencyStopState = emergencyStopState;
}
step 3: Compile and upload the code
I already tried that, but was unable to compile and upload the grblUpload.ino file