Eines de l'usuari

Eines del lloc


tutorials:arduino:05_arduino_motors:20_func_stepper

Funcions i llibreries per al motor pas a pas

La funció bàsica per moure el motor un nombre determinat de passos, amb una direcció i una velocitat:

motor_g0_x.ino
 
void motor_g0_x(int pasos, boolean dir, int velocitat){
// Recorda configurar els pins 8,9,10,11 com OUTPUT al SETUP
int Pin0=8;
int Pin1=9;
int Pin2=10;
int Pin3=11;
int _step=0;
int count=0;
 
 
while(1){
 
   if (pasos<count) {
        return;
        }
 
  switch(_step){ 
   case 0: 
     digitalWrite(Pin0, LOW);  
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, HIGH); 
   break;  
   case 1: 
     digitalWrite(Pin0, LOW);  
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, HIGH); 
   break;  
   case 2: 
     digitalWrite(Pin0, LOW);  
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, LOW); 
   break;  
   case 3: 
     digitalWrite(Pin0, LOW);  
     digitalWrite(Pin1, HIGH); 
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, LOW); 
   break;  
   case 4: 
     digitalWrite(Pin0, LOW);  
     digitalWrite(Pin1, HIGH); 
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
   break;  
   case 5: 
     digitalWrite(Pin0, HIGH);  
     digitalWrite(Pin1, HIGH); 
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
   break;  
     case 6: 
     digitalWrite(Pin0, HIGH);  
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
   break;  
   case 7: 
     digitalWrite(Pin0, HIGH);  
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, HIGH); 
   break;  
   default: 
     digitalWrite(Pin0, LOW);  
     digitalWrite(Pin1, LOW); 
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
   break;  
 } 
 if(dir){ 
   _step++; 
   count++;
 }else{ 
   _step--; 
    count++;
 } 
 if(_step>7){ 
   _step=0; 
 } 
 if(_step<0){ 
   _step=7; 
 } 
 delay(velocitat); 
 
 
}// final while
 
}// funcio

Si utilitzem passos complets:

fmotpcomp.ino
// Recorda configurar els pins 8,9,10,11 com OUTPUT al SETUP
int Pin0=4;
int Pin1=5;
int Pin2=6;
int Pin3=7;
////////////////////
 
void motor(int pasos, boolean dir, int velocitat){
int step=0;
int count=0;
 
while(pasos>count){
 
   if (step==0){ 
         digitalWrite(Pin0, LOW);  
         digitalWrite(Pin1, LOW); 
         digitalWrite(Pin2, HIGH); 
         digitalWrite(Pin3, HIGH); 
   } 
  if (step==1){ 
         digitalWrite(Pin0, HIGH);  
         digitalWrite(Pin1, LOW); 
         digitalWrite(Pin2, LOW); 
         digitalWrite(Pin3, HIGH); 
 }  
  if (step==2){ 
         digitalWrite(Pin0, HIGH);  
         digitalWrite(Pin1, HIGH); 
         digitalWrite(Pin2, LOW); 
         digitalWrite(Pin3, LOW);
      } 
  if (step==3){ 
         digitalWrite(Pin0, LOW);  
         digitalWrite(Pin1, HIGH); 
         digitalWrite(Pin2, HIGH); 
         digitalWrite(Pin3, LOW); 
      }
  if(dir){ 
       step=step+1;
       count=count+1;
     }else{ 
       step=step-1;
       count=count+1;
     } 
     if(step>4){ 
       step=0; 
     } 
     if(step<0){ 
       step=4; 
     } 
     delay(velocitat); 
    }
}
tutorials/arduino/05_arduino_motors/20_func_stepper.txt · Darrera modificació: 2019/04/08 21:23 per crevert