Go Down

Topic: problem with multi stepper (Read 284 times) previous topic - next topic

vtec35

Mar 18, 2018, 01:38 pm Last Edit: Mar 18, 2018, 01:44 pm by vtec35
hi every body, sorry for my english but french forum have no answer about my problem

i would like use arduino for automate some machine at my work with stepper motor, but it s very hard to find a librairy for work


i have do this code only for test with arduino mega+ 2 stepper,and 2 button for home position and lcd+button screen

Code: [Select]
#include <AccelStepper.h>
#include <MultiStepper.h>
AccelStepper stepper(1, 12, 11);
AccelStepper stepper1(1,53, 52);

#include <LiquidCrystal.h>

// Variables pour utiliser l'ecran
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Definir les pins utilisees par le LCD
int lcd_key = 0;
int adc_key_in = 0;
int run=0;
int run1=0;
int timermessage=0;
int timermessage1=0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

/************************************************************************************************************/
// Fonction lire les boutons
/************************************************************************************************************/
int read_LCD_buttons()
{
  adc_key_in = analogRead(0); // Lire les valeurs du capteurs
  // Les valeurs renvoyees sont sensees etre: 0, 144, 329, 504, 741
  // Il y a une erreur possible de 50
if (adc_key_in > 1000) return btnNONE; // Nous découpons les valeurs possibles en zone pour chaque bouton
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 144) return btnUP;
if (adc_key_in < 329) return btnDOWN;
if (adc_key_in < 504) return btnLEFT;
if (adc_key_in < 741) return btnSELECT;
  return btnNONE; // On renvoie cela si l'on est au dessus de 850
}
int s0=0;
int v0=0;
int s1=0;
int v1=0;

// Variables du programme Adrien
// ENTREES
boolean entree=digitalRead(13);
boolean entree1=digitalRead(51);

// SORTIES


int g1 = 0;
int g2 = 0;
void printG1()
{
  char bufG1[10];
  lcd.setCursor(0, 0);
  sprintf(bufG1, "g1=%d ", g1);
  lcd.print(bufG1);
}

void printG2()
{
  char bufG2[20];
  lcd.setCursor(0, 1);
  sprintf(bufG2, "g2=%d ", g2);
  lcd.print(bufG2);
}

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2); // Demarrer la librairie ecran
   //pinmode
  pinMode(13,INPUT);
  pinMode(51,INPUT);   
 

}

void loop() {


  if (run==1)
  {
    stepper.setAcceleration(2000); //pas/s²
    stepper.setMaxSpeed(v0); //vitesse en hertz 
    stepper.moveTo(s0);
    stepper.run();
    v0=2000;
  }
  else
  {
    stepper.setAcceleration(2000); //pas/s²
    stepper.setMaxSpeed(v0); //vitesse en hertz 
    stepper.run();
    stepper.stop();
    stepper.runToPosition();
    v0=2000;
  }


    if (run1==1)
  {
    stepper1.setAcceleration(2000); //pas/s²
    stepper1.setMaxSpeed(v1); //vitesse en hertz 
    stepper1.moveTo(s1);
    stepper1.run();
    v1=2000;
  }
  else
  {
    stepper1.setAcceleration(2000); //pas/s²
    stepper1.setMaxSpeed(v1); //vitesse en hertz 
    stepper1.run();
    stepper1.stop();
    stepper1.runToPosition();
    v1=2000;
  }

//timer message 1

int time=millis();
int time1=millis();
int message;
int message1;

if (g1 != 60)
    {
     timermessage= time + 3000;
    }
else if (g1==60 && time > timermessage)
       {
        message=1;
       }
  else
       {
        message=0;
       }

if (g2 != 60)
    {
     timermessage1= time1 + 3000;
    }
else if (g2==60 && time1 > timermessage1)
       {
        message1=1;
       }
  else
       {
        message1=0;
       }

if (g1==0 )  //btnUp
  {
        run=1;
        s0=stepper.currentPosition()+50000;
        g1=10;
        printG1();
       
  }
  else if (g1==10 and stepper.isRunning() )  //btnUp
  {
        g1=20;
        printG1();
  }
  else if (g1==20 and digitalRead(13)==LOW)  //btndown
  {
        run=0;
        g1=30;
        printG1();
  }
  else if (g1==30 and !stepper.isRunning())
  {
    run=1;
    s0=stepper.currentPosition()-50000;
    g1=40;
    printG1();
  }
    else if (g1==40 and stepper.isRunning())
  {
    g1=50;
    printG1();
  }
    else if (g1==50 and digitalRead(13)==HIGH)
  {
    run=0;
    g1=60;
    printG1();
  }
    else if (g1==60 and message==1)
 {   
        g1=0;
        printG1();
  }




//
if (g2==0 )
  {
        run1=1;
        s1=stepper1.currentPosition()+50000;
        g2=10;
        printG2();
       
  }
  else if (g2==10 and stepper1.isRunning() ) 
  {
        g2=20;
        printG2();
  }
  else if (g2==20 and digitalRead(51)==LOW)
  {
        run1=0;
        g2=30;
        printG2();
  }
  else if (g2==30 and !stepper1.isRunning())
  {
    run1=1;
    s1=stepper1.currentPosition()-50000;
    g2=40;
    printG2();
  }
    else if (g2==40 and stepper1.isRunning())
  {
    g2=50;
    printG2();
  }
    else if (g2==50 and digitalRead(51)==HIGH)
  {
    run1=0;
    g2=60;
    printG2();
  }
    else if (g2==60 and message1==1)
 {   
        g2=0;
        printG2();
  }

}




the problem is:

i have an interruption on the stepper signal when i active 1 button...

at this test:
else if (g1==20 and digitalRead(13)==LOW)
or
else if (g2==20 and digitalRead(51)==LOW)

or g1 50 or g2=50

wildbill

Your sketch is very hard to comprehend what with variable names like v0, g0 etc. Magic numbers aren't helping either.

However, I do notice this when I compile it with compiler warnings set to "All":

Code: [Select]

test.ino:237:21: warning: 'message1' may be used uninitialized in this function [-Wmaybe-uninitialized]
   else if (g2 == 60 and message1 == 1)



vtec35

#2
Mar 18, 2018, 03:04 pm Last Edit: Mar 18, 2018, 03:10 pm by vtec35
hi sorry


my programm is "state machine"

i advance step by step

g1 is for control the stepper, and g2 for the stepper1

message and message1 are resultat to timer and timer1 (non blocking timer), for not use delay()

v0 is speed value for stepper, and v1 for stepper1
s0 is position for stepper and s1 for stepper1


at the begin i have do this sketch only for 1 stepper, and worked fine, after i have just duplicate it, and change all variable name for 2 stepper in //


this sketch is just a test for test if 2 stepper can work in //

vtec35

i have simplify the sketch and put note

Code: [Select]
#include <AccelStepper.h>
#include <LiquidCrystal.h>

// Variables pour utiliser l'ecran
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Definir les pins utilisees par le LCD
int lcd_key = 0;
int adc_key_in = 0;

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

/************************************************************************************************************/
// Fonction lire les boutons
/************************************************************************************************************/
int read_LCD_buttons()
{
  adc_key_in = analogRead(0); // Lire les valeurs du capteurs
  // Les valeurs renvoyees sont sensees etre: 0, 144, 329, 504, 741
  // Il y a une erreur possible de 50
if (adc_key_in > 1000) return btnNONE; // Nous découpons les valeurs possibles en zone pour chaque bouton
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 144) return btnUP;
if (adc_key_in < 329) return btnDOWN;
if (adc_key_in < 504) return btnLEFT;
if (adc_key_in < 741) return btnSELECT;
  return btnNONE; // On renvoie cela si l'on est au dessus de 850
}
int g1 = 0;
int g2 = 0;
int s0=0;
int s1=0;
int run=0;
int run1=0;
int timer=0;
int timer1=0;
int timermessage=0;
int timermessage1=0;

AccelStepper stepper(1, 12, 11);
AccelStepper stepper1(1,53, 52);

// Variables du programme Adrien
// ENTREES


// SORTIES



void printG1()
{
  char bufG1[10];
  lcd.setCursor(0, 0);
  sprintf(bufG1, "g1=%d ", g1);
  lcd.print(bufG1);
}

void printG2()
{
  char bufG2[20];
  lcd.setCursor(0, 1);
  sprintf(bufG2, "g2=%d ", g2);
  lcd.print(bufG2);
}

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2); // Demarrer la librairie ecran
   //pinmode
  pinMode(13,INPUT);
  pinMode(51,INPUT);   
 

}

void loop() {


  if (run==1)
  {
    stepper.setAcceleration(2000); //pas/s²
    stepper.setMaxSpeed(2000); //vitesse en hertz 
    stepper.moveTo(s0);
    stepper.run();
  }
  else
  {
    stepper.setAcceleration(2000); //pas/s²
    stepper.setMaxSpeed(2000); //vitesse en hertz 
    stepper.run();
    stepper.stop();
    stepper.runToPosition();
  }


    if (run1==1)
  {
    stepper1.setAcceleration(2000); //pas/s²
    stepper1.setMaxSpeed(2000); //vitesse en hertz 
    stepper1.moveTo(s1);
    stepper1.run();
  }
  else
  {
    stepper1.setAcceleration(2000); //pas/s²
    stepper1.setMaxSpeed(2000); //vitesse en hertz 
    stepper1.run();
    stepper1.stop();
    stepper1.runToPosition();
  }

//timer message 1

int time=millis();
int time1=millis();


if (g1 != 60)
    {
     timermessage= time + 3000;
    }
else if (g1==60 && time > timermessage)
       {
        timer=1;
       }
  else
       {
        timer=0;
       }

if (g2 != 60)
    {
     timermessage1= time1 + 3000;
    }
else if (g2==60 && time1 > timermessage1)
       {
        timer1=1;
       }
  else
       {
        timer1=0;
       }

//loop for stepper

if (g1==0 ) //start stepper
  {
        run=1;
        s0=stepper.currentPosition()+50000;
        g1=10;
        printG1();
       
  }
  else if (g1==10 and stepper.isRunning() ) //when stepper is running
  {
        g1=20;
        printG1();
  }
  else if (g1==20 and digitalRead(13)==LOW)  // when input is actived stop the stepper with decceleration
  {
        run=0;
        g1=30;
        printG1();
  }
  else if (g1==30 and !stepper.isRunning()) //when stepper is stopped reverse the stepper
  {
        run=1;
        s0=stepper.currentPosition()-50000;
        g1=40;
        printG1();
  }
  else if (g1==40 and stepper.isRunning()) //when stepper is running
  {
       g1=50;
       printG1();
  }
  else if (g1==50 and digitalRead(13)==HIGH) // when the input is not actived, stop the stepper with decceleration
  {
       run=0;
       g1=60;
       printG1();
  }
  else if (g1==60 and timer==1)  //when the timer is finish, we can restart the loop
 {   
       g1=0;
       printG1();
  }




// loop for stepper1
if (g2==0 )
  {
        run1=1;
        s1=stepper1.currentPosition()+50000;
        g2=10;
        printG2();
       
  }
  else if (g2==10 and stepper1.isRunning() ) 
  {
        g2=20;
        printG2();
  }
  else if (g2==20 and digitalRead(51)==LOW)
  {
        run1=0;
        g2=30;
        printG2();
  }
  else if (g2==30 and !stepper1.isRunning())
  {
    run1=1;
    s1=stepper1.currentPosition()-50000;
    g2=40;
    printG2();
  }
    else if (g2==40 and stepper1.isRunning())
  {
    g2=50;
    printG2();
  }
    else if (g2==50 and digitalRead(51)==HIGH)
  {
    run1=0;
    g2=60;
    printG2();
  }
    else if (g2==60 and timer1==1)
 {   
        g2=0;
        printG2();
  }

}

Go Up