Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


materiel:uln2003:accueil

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
materiel:uln2003:accueil [2018/03/15 15:35]
resonance
materiel:uln2003:accueil [2019/01/30 10:23] (Version actuelle)
resonance [simple code avec lib stepper]
Ligne 9: Ligne 9:
  
 {{youtube>Sl2mzXfTwCs?large}} {{youtube>Sl2mzXfTwCs?large}}
 +
 +===== Exemple =====
 +<code cpp>
 +/*
 +   BYJ48 Stepper motor code
 +   Connect :
 +   IN1 >> D8
 +   IN2 >> D9
 +   IN3 >> D10
 +   IN4 >> D11
 +   VCC ... 5V Prefer to use external 5V Source
 +   Gnd
 +   written By :Mohannad Rawashdeh
 +  https://www.instructables.com/member/Mohannad+Rawashdeh/
 +     28/9/2013
 +  */
 +
 +#define IN1  8
 +#define IN2  9
 +#define IN3  10
 +#define IN4  11
 +
 +int Steps = 0;
 +boolean Direction = true;// gre
 +unsigned long last_time;
 +unsigned long currentMillis ;
 +int steps_left=4095;
 +long time;
 +void setup()
 +{
 +Serial.begin(115200);
 +pinMode(IN1, OUTPUT); 
 +pinMode(IN2, OUTPUT); 
 +pinMode(IN3, OUTPUT); 
 +pinMode(IN4, OUTPUT); 
 +// delay(1000);
 +
 +}
 +void loop()
 +{
 +  while(steps_left>0){
 +  currentMillis = micros();
 +  if(currentMillis-last_time>=1000){
 +  stepper(1); 
 +  time=time+micros()-last_time;
 +  last_time=micros();
 +  steps_left--;
 +  }
 +  }
 +   Serial.println(time);
 +  Serial.println("Wait...!");
 +  delay(2000);
 +  Direction=!Direction;
 +  steps_left=4095;
 +}
 +
 +void stepper(int xw){
 +  for (int x=0;x<xw;x++){
 +switch(Steps){
 +   case 0:
 +     digitalWrite(IN1, LOW); 
 +     digitalWrite(IN2, LOW);
 +     digitalWrite(IN3, LOW);
 +     digitalWrite(IN4, HIGH);
 +   break; 
 +   case 1:
 +     digitalWrite(IN1, LOW); 
 +     digitalWrite(IN2, LOW);
 +     digitalWrite(IN3, HIGH);
 +     digitalWrite(IN4, HIGH);
 +   break; 
 +   case 2:
 +     digitalWrite(IN1, LOW); 
 +     digitalWrite(IN2, LOW);
 +     digitalWrite(IN3, HIGH);
 +     digitalWrite(IN4, LOW);
 +   break; 
 +   case 3:
 +     digitalWrite(IN1, LOW); 
 +     digitalWrite(IN2, HIGH);
 +     digitalWrite(IN3, HIGH);
 +     digitalWrite(IN4, LOW);
 +   break; 
 +   case 4:
 +     digitalWrite(IN1, LOW); 
 +     digitalWrite(IN2, HIGH);
 +     digitalWrite(IN3, LOW);
 +     digitalWrite(IN4, LOW);
 +   break; 
 +   case 5:
 +     digitalWrite(IN1, HIGH); 
 +     digitalWrite(IN2, HIGH);
 +     digitalWrite(IN3, LOW);
 +     digitalWrite(IN4, LOW);
 +   break; 
 +     case 6:
 +     digitalWrite(IN1, HIGH); 
 +     digitalWrite(IN2, LOW);
 +     digitalWrite(IN3, LOW);
 +     digitalWrite(IN4, LOW);
 +   break; 
 +   case 7:
 +     digitalWrite(IN1, HIGH); 
 +     digitalWrite(IN2, LOW);
 +     digitalWrite(IN3, LOW);
 +     digitalWrite(IN4, HIGH);
 +   break; 
 +   default:
 +     digitalWrite(IN1, LOW); 
 +     digitalWrite(IN2, LOW);
 +     digitalWrite(IN3, LOW);
 +     digitalWrite(IN4, LOW);
 +   break; 
 +}
 +SetDirection();
 +}
 +
 +void SetDirection(){
 +if(Direction==1){ Steps++;}
 +if(Direction==0){ Steps--; }
 +if(Steps>7){Steps=0;}
 +if(Steps<0){Steps=7; }
 +}
 +</code>
 +
 +
 +=====simple code avec lib stepper =====
 +<code c+>
 +#include <Stepper.h>
 +const int stepsPerRevolution = 32*64;  
 +
 +// (step,1N2,1N4,1N3,1N1)
 +Stepper myStepper(stepsPerRevolution, 9, 11, 10, 8);
 + 
 +void setup() {    }
 + 
 +void loop() {
 +    myStepper.setSpeed(10);  //vitesse entre 0 et 15
 +
 +    // 100 step = 2048 
 +    myStepper.step(2048);
 +    delay(1500);
 +    myStepper.step(-2048);
 +    delay(1500);
 +}
 +
 +
 +</code>
 +===== StepperMotor LIB =====
 +  * Creer un dossier StepperMotor dans /User/..../Arduino/libraries
 +  * copier StepperMotor.h et StepperMotor.cpp dedans
 +
 +<code cpp>
 +// StepperMotor.h
 +#ifndef Stepper_h
 +#define Stepper_h
 +
 +class StepperMotor {
 +public:
 +    StepperMotor(int In1, int In2, int In3, int In4);    // Constructor that will set the inputs
 +    void setStepDuration(int duration);    // Function used to set the step duration in ms
 +    void step(int noOfSteps);    // Step a certain number of steps. + for one way and - for the other
 +
 +    int duration;    // Step duration in ms
 +    int inputPins[4];    // The input pin numbers
 +};
 +
 +#endif
 +
 +</code>
 +
 +<code cpp>
 +// StepperMotor.cpp
 +#include <Arduino.h>
 +#include <StepperMotor.h>
 +
 +StepperMotor::StepperMotor(int In1, int In2, int In3, int In4){
 +    // Record pin numbers in the inputPins array
 +    this->inputPins[0] = In1;
 +    this->inputPins[1] = In2;
 +    this->inputPins[2] = In3;
 +    this->inputPins[3] = In4;
 +
 +    // Iterate through the inputPins array, setting each one to output mode
 +    for(int inputCount = 0; inputCount < 4; inputCount++){
 +        pinMode(this->inputPins[inputCount], OUTPUT);
 +    }
 +    duration = 50;
 +}
 +
 +void StepperMotor::setStepDuration(int duration){
 +    this->duration = duration;
 +}
 +
 +void StepperMotor::step(int noOfSteps){
 +    /*
 +        The following 2D array represents the sequence that must be
 +        used to acheive rotation. The rows correspond to each step, and
 +        the columns correspond to each input. L
 +    */
 +    bool sequence[][4] = {{LOW, LOW, LOW, HIGH },
 +                          {LOW, LOW, HIGH, HIGH},
 +                          {LOW, LOW, HIGH, LOW },
 +                          {LOW, HIGH, HIGH, LOW},
 +                          {LOW, HIGH, LOW, LOW },
 +                          {HIGH, HIGH, LOW, LOW},
 +                          {HIGH, LOW, LOW, LOW },
 +                          {HIGH, LOW, LOW, HIGH}};
 +                      
 +    int factor = abs(noOfSteps) / noOfSteps;    // If noOfSteps is +, factor = 1. If noOfSteps is -, factor = -1 
 +    noOfSteps = abs(noOfSteps);    // If noOfSteps was in fact negative, make positive for future operations
 +
 +    /* 
 +        The following algorithm runs through the sequence the specified number 
 +        of times
 +    */
 +    for(int sequenceNum = 0;  sequenceNum <= noOfSteps/8; sequenceNum++){
 +        for(int position = 0; ( position < 8 ) && ( position < ( noOfSteps - sequenceNum*8 )); position++){
 +            delay(duration);
 +            for(int inputCount = 0; inputCount < 4; inputCount++){
 +                digitalWrite(this->inputPins[inputCount], sequence[(int)(3.5 - (3.5*factor) + (factor*position))][inputCount]);
 +            }
 +        } 
 +    }
 +}
 +</code>
 +
 +
 +<code cpp>
 +#include <StepperMotor.h>
 +
 +StepperMotor motor1(8,9,10,11);
 +StepperMotor motor2(4,5,6,7);
 +
 +void setup(){
 +  Serial.begin(9600);
 +  motor1.setStepDuration(1);
 +  motor2.setStepDuration(1);
 +}
 +
 +void loop(){
 +  motor1.step(1000);
 +  motor2.step(1000);
 +  delay(2000);
 +  motor1.step(-1000);
 +  motor2.step(-1000);
 +  delay(2000);
 +</code>
/home/resonancg/www/wiki/data/attic/materiel/uln2003/accueil.1521124504.txt.gz · Dernière modification: 2018/03/15 15:35 de resonance