I have a loop moving x stepmotor and then y stepmotor.
Each motor is moved using a function move_x and move_y
The moving is done by triggering one pin from high to low with a delay in between that set the speed. It increment a counter in a FOR loop from 0 to wherever I wish to go.
The fonction moves the motors and it returns the position so I can move back to origin if required.
I am looking for the possibility that if during a movement, I press a pushbutton, it would return to origin and stop.
I just thought while writing this, I could add in the FOR loop of each function an if button is pressed call the function back_to_origin, but then how should restart from the top of main loop, just like if I had reset the arduino?
Many thanks for your help... pretty new in Arduino.. cool stuff!
If I understand well the Return function will go back to the next instruction.
I wish to get back to the start, almost like a hard reset.. in fact hard reset be put in software?
Thank you Delta_G for your answer.
I just tried it :
Loop is calling a function
In this function I add Return
It stop the function and go back to the next instruction in loop.
I wish to go back to top of the loop, or even run the setup again it that's what it takes.
Thank you all for your suggestion, I am open to make adjustment. Below is the code.
Loop converts my const into pulses for my steppers.
Then it calls the function depl_x to move the motor x direction
Then it calls the function depl_y to move the motor in y direction
it repeats these two several amount of times
Then move back to starting point
Note that x direction is changing everytime.
Thank you all for your suggestion, I am open to make adjustment. Below is the code.
Loop converts my const into pulses for my steppers.
Then it calls the function depl_x to move the motor x direction
Then it calls the function depl_y to move the motor in y direction
it repeats these two several amount of times
Then move back to starting point
Note that x direction is changing everytime.
// Révision 0 - mouvement et faire les passes x et y avec convertion des longueurs
// Révision 1 - Retour des positions actuels du curseurs afin de programmer le retour à zéro
// Configuration:
const int travelx_mm = 100; // Distance X MAX 1647
const int travely_mm = 300; // Distance Y MAX 757
const int hauteur_debut_mm = -150; // Hauteur du début de peinture
const int overlap_y_mm = 20; // overlap de peinture sur la hauteur
const int largeur_fan_mm = 300; // largeur du jet de peinture
const int spdx = 500; // Vitesse X (190 est le plus vite, 1000 est lent) MAX 365
const int spdy = 1500; // Vitesse Y (190 est le plus vite, 1000 est lent)
const int prepaint = 20; // Temps que la peinture met pour atteindre la cible (mS)
const int delais = 0; // Délais entre les mouvements (mS) (La valeur prepaint affecte aussi)
// *****************************************
// NE RIEN CHANGER EN DESSOUS DE CETTE LIGNE
// *****************************************
const int xstepPin = 9;
const int xdirPin = 8;
const int ystepPin = 7;
const int ydirPin = 6;
const int gunpin = 10;
const int pinbouton = 12;
void setup() {
// put your setup code here, to run once:
pinMode(xstepPin, OUTPUT);
pinMode(xdirPin, OUTPUT);
pinMode(ystepPin, OUTPUT);
pinMode(ydirPin, OUTPUT);
// pinMode(motenable,OUTPUT);
pinMode(gunpin, OUTPUT);
pinMode(pinbouton, INPUT);
// digitalWrite(motenable,LOW);
digitalWrite(gunpin, LOW);
}
void loop() {
int nb_y_max;
int direction_x;
int direction_y;
float increment_y;
float depl_initial_y;
float travelx ;
float travely ;
float hauteur_debut;
float overlap_y;
float largeur_fan;
float position_y;
float position_x;
travelx = conv_unit(travelx_mm); // Distance X
travely = conv_unit(travely_mm); // Distance Y
hauteur_debut = conv_unit(hauteur_debut_mm); // Hauteur du début de peinture
overlap_y = conv_unit(overlap_y_mm); // overlap de peinture sur la hauteur
largeur_fan = conv_unit(largeur_fan_mm); // largeur du jet de peinture
direction_x = 1;
direction_y = 1;
position_x = 0;
position_y = 0;
depl_initial_y = hauteur_debut + (largeur_fan / 2);
nb_y_max = (travely - depl_initial_y) / (largeur_fan - overlap_y);
increment_y = largeur_fan - overlap_y;
boolean etatBouton = digitalRead(pinbouton);
//test des conditions
if (etatBouton == HIGH) { //test si bouton appuyé
depl_y(depl_initial_y, direction_y); //positionnement au niveau du début de peinture
for (int nb_y = 0; nb_y < nb_y_max; nb_y++) { // Boucle qui effectue le déplacement +x +y -x +y ...
position_x = position_x + depl_x(travelx, direction_x);
position_y = position_y + depl_y(increment_y, direction_y);
direction_x = direction_x * -1;
} /// fin de la boucle for du déplacement;
// Return to origin (point 0,0)
position_x = depl_x(position_x, direction_x);
position_y = depl_y(position_y + depl_initial_y, -direction_y);
}
}
//**********************
//End of main loop
//**********************
// *******************
// Function to move the x motor and return the position
//********************
float depl_x(int distance_x, int dir_x) {
float x_pos;
if (dir_x == 1) {
digitalWrite(xdirPin, HIGH);
}
else
{
digitalWrite(xdirPin, LOW);
}
digitalWrite(gunpin, HIGH);
delay(prepaint);
for (int x = 0; x < distance_x; x++) {
digitalWrite(xstepPin, HIGH);
delayMicroseconds(spdx);
digitalWrite(xstepPin, LOW);
delayMicroseconds(spdx);
x_pos = x;
}
digitalWrite(gunpin, LOW);
delay(delais);
return (x_pos * dir_x);
}
//**********************
// *******************
// Function to move the y motor and return the position
//********************
float depl_y(float distance_y, int dir_y) {
float y_pos;
if (dir_y == 1) {
digitalWrite(ydirPin, HIGH) ;
}
else
{
digitalWrite(ydirPin, LOW);
}
for (int y = 0; y < distance_y; y++) {
digitalWrite(ystepPin, HIGH);
delayMicroseconds(spdy);
digitalWrite(ystepPin, LOW);
delayMicroseconds(spdy);
y_pos = y;
}
delay(delais);
return (y_pos * dir_y);
}
//**********************
// *******************
// Function to convert mm to pulse
//********************
float conv_unit(float chiffre) { // convertir les entrées mm en unité de pulse - 176mm = 1000 pulse
return ( chiffre * 1000 / 176);
}
//**********************
I suspect it will be easier to get the program to do what you want if you move most of the code from loop() into one or more functions. Then a RETURN from a function pretty much gets back to the start.