Ci-dessous, les différences entre deux révisions de la page.
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 16:00] resonance |
materiel:uln2003:accueil [2019/01/30 10:23] (Version actuelle) resonance [simple code avec lib stepper] |
||
---|---|---|---|
Ligne 10: | Ligne 10: | ||
{{youtube> | {{youtube> | ||
+ | ===== Exemple ===== | ||
+ | <code cpp> | ||
+ | /* | ||
+ | BYJ48 Stepper motor code | ||
+ | | ||
+ | IN1 >> D8 | ||
+ | IN2 >> D9 | ||
+ | IN3 >> D10 | ||
+ | IN4 >> D11 | ||
+ | VCC ... 5V Prefer to use external 5V Source | ||
+ | Gnd | ||
+ | | ||
+ | https:// | ||
+ | | ||
+ | */ | ||
+ | #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, | ||
+ | pinMode(IN2, | ||
+ | pinMode(IN3, | ||
+ | pinMode(IN4, | ||
+ | // delay(1000); | ||
+ | |||
+ | } | ||
+ | void loop() | ||
+ | { | ||
+ | while(steps_left> | ||
+ | currentMillis = micros(); | ||
+ | if(currentMillis-last_time> | ||
+ | stepper(1); | ||
+ | time=time+micros()-last_time; | ||
+ | last_time=micros(); | ||
+ | steps_left--; | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | Serial.println(" | ||
+ | delay(2000); | ||
+ | Direction=!Direction; | ||
+ | steps_left=4095; | ||
+ | } | ||
+ | |||
+ | void stepper(int xw){ | ||
+ | for (int x=0; | ||
+ | switch(Steps){ | ||
+ | case 0: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | case 1: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | case 2: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | case 3: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | case 4: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | case 5: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | case 6: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | case 7: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | SetDirection(); | ||
+ | } | ||
+ | } | ||
+ | void SetDirection(){ | ||
+ | if(Direction==1){ Steps++;} | ||
+ | if(Direction==0){ Steps--; } | ||
+ | if(Steps> | ||
+ | if(Steps< | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | =====simple code avec lib stepper ===== | ||
+ | <code c+> | ||
+ | #include < | ||
+ | const int stepsPerRevolution = 32*64; | ||
+ | |||
+ | // (step, | ||
+ | Stepper myStepper(stepsPerRevolution, | ||
+ | |||
+ | void setup() { } | ||
+ | |||
+ | void loop() { | ||
+ | myStepper.setSpeed(10); | ||
+ | |||
+ | // 100 step = 2048 | ||
+ | myStepper.step(2048); | ||
+ | delay(1500); | ||
+ | myStepper.step(-2048); | ||
+ | delay(1500); | ||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
===== StepperMotor LIB ===== | ===== StepperMotor LIB ===== | ||
+ | * Creer un dossier StepperMotor dans / | ||
+ | * 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); | ||
+ | void step(int noOfSteps); | ||
+ | |||
+ | int duration; | ||
+ | int inputPins[4]; | ||
+ | }; | ||
+ | |||
+ | #endif | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code cpp> | ||
+ | // StepperMotor.cpp | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | StepperMotor:: | ||
+ | // Record pin numbers in the inputPins array | ||
+ | this-> | ||
+ | this-> | ||
+ | this-> | ||
+ | this-> | ||
+ | |||
+ | // Iterate through the inputPins array, setting each one to output mode | ||
+ | for(int inputCount = 0; inputCount < 4; inputCount++){ | ||
+ | pinMode(this-> | ||
+ | } | ||
+ | duration = 50; | ||
+ | } | ||
+ | |||
+ | void StepperMotor:: | ||
+ | this-> | ||
+ | } | ||
+ | |||
+ | void StepperMotor:: | ||
+ | /* | ||
+ | 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; | ||
+ | noOfSteps = abs(noOfSteps); | ||
+ | |||
+ | /* | ||
+ | The following algorithm runs through the sequence the specified number | ||
+ | of times | ||
+ | */ | ||
+ | for(int sequenceNum = 0; sequenceNum <= noOfSteps/ | ||
+ | for(int position = 0; ( position < 8 ) && ( position < ( noOfSteps - sequenceNum*8 )); position++){ | ||
+ | delay(duration); | ||
+ | for(int inputCount = 0; inputCount < 4; inputCount++){ | ||
+ | digitalWrite(this-> | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
<code cpp> | <code cpp> |