[Endstop] Axis hitting the endstop and keep it triggered in an infinite loop

Hi,

I'm beginning with Arduino but I've built a sort of CNC using the typical 3 axis (X,Y,Z).
I've uploaded grbllite into an arduino Uno and i'm controlling it with a Raspberry Pi (Rasbian)

Every axis is moving well, and when I press an endstop the axis stops. Good.

My problem is when homing :

When i send G28 X0 for example, the X axis hits the endstop, but then it stays in position and trigger it forever. I can't send another command.

What i need is when X hits the endstop, it goes back a little and stops. Then i would add G92 and have my 0.

I've searched the web for hours trying to copy paste every line including the endstop but i didn't find any explanation.

Endstops are mecanical, connected to the cnc shield on the X- Y- and Z- position.

Here is the code relative to the endstop:

const int X_END_STOP = 8;
const int Y_END_STOP = 9;
const int Z_END_STOP = 10;


int pin_ENDSTOP[3] = {9, 10, 11}; // les broches de endstop des moteurs X,Y,Z
const int APPUI = LOW; // s

pinMode(pin_ENDSTOP[X], INPUT_PULLUP); // met broche en entr�e avec rappel au + actif
  pinMode(pin_ENDSTOP[Y], INPUT_PULLUP); // met broche en entr�e avec rappel au + actif
  pinMode(pin_ENDSTOP[Z], INPUT_PULLUP); // met broche en entr�e avec rappel au + actif

  
    digitalWrite(pin_ENDSTOP[X], HIGH); // met broche avec rappel au + actif
    digitalWrite(pin_ENDSTOP[Y], HIGH); // met broche avec rappel au + actif
    digitalWrite(pin_ENDSTOP[Z], HIGH); // met broche avec rappel au + actif

// lecture des endstops
  if (digitalRead(pin_ENDSTOP[X]) == APPUI) {
    Serial.println("Appui ENDSTOP X");
    delay(100); // anti-rebond
  } // fin si appui

  if (digitalRead(pin_ENDSTOP[Y]) == APPUI) {
    Serial.println("Appui ENDSTOP Y");
    delay(100); // anti-rebond
  } // fin si appui

  if (digitalRead(pin_ENDSTOP[Z]) == APPUI) {
    Serial.println("Appui ENDSTOP Z");
    delay(100); // anti-rebond
  } // fin si appui
// lecture des endstops
  if (digitalRead(pin_ENDSTOP[X]) == APPUI) {
    Serial.println("Appui ENDSTOP X");
    delay(100); // anti-rebond
  } // fin si appui

  if (digitalRead(pin_ENDSTOP[Y]) == APPUI) {
    Serial.println("Appui ENDSTOP Y");
    delay(100); // anti-rebond
  } // fin si appui

  if (digitalRead(pin_ENDSTOP[Z]) == APPUI) {
    Serial.println("Appui ENDSTOP Z");
    delay(100); // anti-rebond
  } // fin si appui

//---- G28 ---
void G28() {
  Serial.println(F("G28"));
  feedrate(frmax);// on utilise le feedrate rapide

  int  test = 0.0; // variable pour test
  // -- axe X
  test = parsenumber('X', -1); // nouvelle position X - on passe -1 en parametre - sera idem si pas present - note X seul idem X0
  output("X :", test);

  if (test != -1) { // si X : on va chercher le endstop X

    searchEndstop(X); // fonction de recherche endstop
    px = 0.0; // RAZ position X

  } // fin if

  // -- axe Y
  test = parsenumber('Y', -1); // nouvelle position Y - on passe -1 en parametre - sera idem si pas present - note Y seul idem Y0
  output("Y :", test);

  if (test != -1) { // si Y : on va chercher le endstop Y

    searchEndstop(Y); // fonction de recherche endstop
    py = 0.0; // RAZ position Y

  } // fin if

  // -- axe Z
  test = parsenumber('Z', -1); // nouvelle position Z - on passe -1 en parametre - sera idem si pas present - note Z seul idem Z0
  output("Z :", test);

  if (test != -1) { // si X : on va chercher le endstop X

    searchEndstop(Z); // fonction de recherche endstop
    pz = 0.0; // RAZ position X

  } // fin if


} // fin G28

//--- fonction de recherche d'un endstop
void searchEndstop(int axisIn) {

  int search = true;

  while (search) {
    onestep(axisIn, -1); // 1 pas en arriere
    tick(); // delay de feedrate

    if (digitalRead(pin_ENDSTOP[axisIn]) == APPUI) { // on teste le endstop
      Serial.println("Appui ENDSTOP"); // debug
      delay(100); // anti-rebond
      search = false; // pour sortir du while
    } // fin si appui

  } // fin while

  last_Dir[axisIn] = -1; // MAJ lastDir[axisIn]


} // fin searchEndstop
 // d�tection des endstop de s�curit� - revoir pour d�tection tous les npas...
      if (digitalRead(pin_ENDSTOP[x]) == APPUI) { // on teste le endstop x - si appui, on sort de la fonction line de mouvement

        Serial.println("Appui endstop X : arret des mouvements");
        return; // on sort de la fonction line

      } // fin if endstop x

      if (digitalRead(pin_ENDSTOP[y]) == APPUI) { // on teste le endstop y - si appui, on sort de la fonction line de mouvement

        Serial.println("Appui endstop Y : arret des mouvements");
        return;  // on sort de la fonction line

      } // fin if endstop y


    } // fin for

  } // fin if

Thanks for reading me and giving any advice.

There is nothing wrong with the code you posted except the calls being made to functions in the code you didn't post. So, there's no real way of determining what's going on.

Thank you for you answer; being limited to 9000 characters made me copy the text concerning the endstop only.

#define VERSION (02.2017)

//--- config g�n�rale ---
#define BAUD (115200) // debit de communication
#define MAX_BUF (64) // taille max du buffer de reception serie

#define VERBOSE 1 // pour messages  

//--- vitesse de deplacement --- // en (mm/sec)
#define MAX_FEEDRATE (60) // vitesse maximale mm/min
#define MIN_FEEDRATE (60) // vitesse minimale en mm/min

//-- params moteurs
#define X 0
#define Y 1
#define Z 2

#define STEPS_PER_TURN (200) // pas par tour du moteur
#define MICROSTEPS_MODE (16) // mode micropas utilis�
#define MM_PER_TURN 1.25 // mm par tour - fonction pas de vis - M6 : 1mm | M8 : 1.25mm
#define PULSE_US 5 // largeur du pulse moteur en �s

//-- params mouvements
#define X_STEPS_PER_MM (STEPS_PER_TURN*MICROSTEPS_MODE/MM_PER_TURN)
#define Y_STEPS_PER_MM (STEPS_PER_TURN*MICROSTEPS_MODE/MM_PER_TURN)
#define Z_STEPS_PER_MM (STEPS_PER_TURN*MICROSTEPS_MODE/MM_PER_TURN)


// limites mouvements en mm
/*
  #define X_MAX_POS 290
  #define X_MIN_POS 0
  #define Y_MAX_POS 210
  #define Y_MIN_POS 0
  #define Z_MAX_POS 50
  #define Z_MIN_POS 0

*/

// chaque moteur est control� par
// > une broche de pulse : front montant d�clenche pas suivant
// > une broche de dir : fixe le sens du moteur

//--- identifiant X,Y,Z pour indices tableaux
#define x (0)
#define y (1)
#define z (2)

//-- broches de pulse moteur

/*
  const int X_STEP=2;
  const int Y_STEP=3;
  const int Z_STEP=4;
*/

int pin_STEP[3] = {2, 3, 4}; // les broches de step des moteurs X,Y,Z


//-- broches de sens moteur

/*
  const int X_DIR=5;
  const int Y_DIR=6;
  const int Z_DIR=7;
*/

int pin_DIR[3] = {5, 6, 7}; // les broches de direction des moteurs X,Y,Z

//-- broche activation moteur

const int pin_ENABLE = 8; // broche activation moteur

//-- broches fin de course


const int X_END_STOP = 8;
const int Y_END_STOP = 9;
const int Z_END_STOP = 10;


int pin_ENDSTOP[3] = {9, 10, 11}; // les broches de endstop des moteurs X,Y,Z
const int APPUI = LOW; // s

//-- reception serie
char buffer[MAX_BUF];  // tableau de reception des caracteres
int sizeIn;  // nombre de caracteres presents dans le buffer

//--- position
// variables de position courante
float px = 0.0;
float py = 0.0;
float pz = 0.0;

float pxn, pyn, pzn; // variable nouvelle position courante

//-- vitesse
float fr = 60.0; // memo feedrate en mm/min - utilis�e pour G01
float frmax = 60.0; // feedrate utilis� pour G00 en mm/min
float currentfr = 0; // le feedrate courant actif - different de fr pour initialisation possible

//long step_delay_ms;
long step_delay_us = 0; // delai entre 2 pas

//-- parametrages fonctionnement
int mode = 90; // mode absolu/relatif - 90 = absolu 92 = relatif - par d�faut absolu
long line_number = 0;

int lastCmd = -1; // Variable m�morise dernier commande G appel�e

//-- variables de backslash --

int bs_Up[3] = {0, 0, 0}; // valeur de correction du backslash dans le sens mouvement positif
int bs_Down[3] = {0, 0, 0}; // valeur de correction du backslash dans le sens mouvement n�gatif

// 0,0,0 par d�faut pour permettre d�coupe, etc... utiliser vitesse minimale F1
// pour per�age utiliser 1200, 600, 600 avec M99 X1200 Y600 Z600 F1 et M99 X1200 Y600 Z600 F-1

int dir[3] = {0, 0, 0}; // direction courante du mouvement : -1 si sens n�g, +1 si sens +

int last_Dir[3] = {0, 0, 0}; // m�morisation derni�re direction du mouvement : -1 si sens n�g, +1 si sens +
// 0 au d�part = pas d'info sur dernier mouvement - un home X, Y est conseill�


// --- D�claration des objets utiles pour les fonctionnalit�s utilis�es ---


// ////////////////////////// 2. FONCTION SETUP = Code d'initialisation //////////////////////////
// La fonction setup() est ex�cut�e en premier et 1 seule fois, au d�marrage du programme

void setup()   { // debut de la fonction setup()

  // --- ici instructions � ex�cuter 1 seule fois au d�marrage du programme ---

  // ------- Initialisation fonctionnalit�s utilis�es -------

  Serial.begin(115200); // initialise connexion s�rie � 115200 bauds

  delay(1000);

  help(); // messages d'instruction

  ready(); // fonction qui r�initialise contenu buffer

  feedrate(60); // initialisation feedrate en mm/min

  // IMPORTANT : r�gler le terminal c�t� PC avec la m�me valeur de transmission


  // ------- Broches en sorties num�riques -------

  // broches de pulse moteur
  pinMode(pin_STEP[X], OUTPUT); // met broche en sortie
  pinMode(pin_STEP[Y], OUTPUT); // met broche en sortie
  pinMode(pin_STEP[Z], OUTPUT); // met broche en sortie

  digitalWrite(pin_STEP[X], LOW); // met broche niveau BAS
  digitalWrite(pin_STEP[Y], LOW); // met broche niveau BAS
  digitalWrite(pin_STEP[Z], LOW); // met broche niveau BAS


  // broches de dir moteur
  pinMode(pin_DIR[X], OUTPUT); // met broche en sortie
  pinMode(pin_DIR[Y], OUTPUT); // met broche en sortie
  pinMode(pin_DIR[Z], OUTPUT); // met broche en sortie

  digitalWrite(pin_DIR[X], LOW); // met broche niveau BAS
  digitalWrite(pin_DIR[Y], LOW); // met broche niveau BAS
  digitalWrite(pin_DIR[Z], LOW); // met broche niveau BAS

  // broches de endstop
  pinMode(pin_ENDSTOP[X], INPUT_PULLUP); // met broche en entr�e avec rappel au + actif
  pinMode(pin_ENDSTOP[Y], INPUT_PULLUP); // met broche en entr�e avec rappel au + actif
  pinMode(pin_ENDSTOP[Z], INPUT_PULLUP); // met broche en entr�e avec rappel au + actif

  
    digitalWrite(pin_ENDSTOP[X], HIGH); // met broche avec rappel au + actif
    digitalWrite(pin_ENDSTOP[Y], HIGH); // met broche avec rappel au + actif
    digitalWrite(pin_ENDSTOP[Z], HIGH); // met broche avec rappel au + actif
  

  //---- broche activation moteur
  pinMode(pin_ENABLE, OUTPUT); // broche en sortie
  digitalWrite(pin_ENABLE, LOW); // activation par defaut moteur - ENABLE ACTIF SUR NIVEAU BAS ++

  // ------- Broches en entr�es num�riques -------

  // ------- Activation si besoin du rappel au + (pullup) des broches en entr�es num�riques -------

  // ------- Initialisation des variables utilis�es -------

} // fin de la fonction setup()
// ********************************************************************************

////////////////////////////////// 3. FONCTION LOOP = Boucle sans fin = coeur du programme //////////////////
// la fonction loop() s'ex�cute sans fin en boucle aussi longtemps que l'Arduino est sous tension

void loop() { // debut de la fonction loop()


  // --- ici instructions � ex�cuter par le programme principal ---



  //---- code type r�ception chaine sur le port s�rie ---
  /*
    while (Serial.available()>0) { // tant qu'un octet en r�ception
  	octetReception=Serial.read(); // Lit le 1er octet re�u et le met dans la variable

  	if (octetReception==10) { // si Octet re�u est le saut de ligne
  		Serial.println (chaineReception); // affiche la chaine recue
  		chaineReception=""; //RAZ le String de r�ception
  		break; // sort de la boucle while
  	}
  	else { // si le caract�re re�u n'est pas un saut de ligne
  		chaineReception=chaineReception+char(octetReception); // ajoute le carat�re au String
  		//Serial.print(char(octetReception)); // affiche la chaine recue
  	}

Is the code you have posted an extract from grbllite or is it a program you have written yourself?

If it is part of grbllite then I reckon you need to get advice from its authors or from a CNC Forum where people are familiar with it.

If it is a program you have written yourself then please post the complete program - add your .ino file as an attachment. Better still write a short program that demonstrates the problem and post that.

...R