Le code de mes moteurs :
/
/ This Arduino example demonstrates bidirectional operation of a // 28BYJ-48, using a ULN2003 interface board to drive the stepper. // The 28BYJ-48 motor is a 4-phase, 8-beat motor, geared down by // a factor of 68. One bipolar winding is on motor pins 1 & 3 and // the other on motor pins 2 & 4. The step angle is 5.625/64 and the // operating Frequency is 100pps. Current draw is 92mA. //////////////////////////////////////////////// //declare variables for the motor pins int motorPin1 = 8; // Blue - 28BYJ48 pin 1 int motorPin2 = 9; // Pink - 28BYJ48 pin 2 int motorPin3 = 10; // Yellow - 28BYJ48 pin 3 int motorPin4 = 11; // Orange - 28BYJ48 pin 4 // Red - 28BYJ48 pin 5 (VCC) int motorSpeed = 1200; //variable to set stepper speed int count = 0; // count of steps made int countsperrev = 512; // number of steps per full revolution int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001}; ////////////////////////////////////////////////////////////////////////////// void setup() { //declare the motor pins as outputs pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(motorPin3, OUTPUT); pinMode(motorPin4, OUTPUT); Serial.begin(9600); } ////////////////////////////////////////////////////////////////////////////// void loop(){ if(count < countsperrev ) clockwise(); else if (count == countsperrev * 2) count = 0; else anticlockwise(); count++; } ////////////////////////////////////////////////////////////////////////////// //set pins to ULN2003 high in sequence from 1 to 4 //delay "motorSpeed" between each pin setting (to determine speed) void anticlockwise() { for(int i = 0; i < 8; i++) { setOutput(i); delayMicroseconds(motorSpeed); } } void clockwise() { for(int i = 7; i >= 0; i--) { setOutput(i); delayMicroseconds(motorSpeed); } } void setOutput(int out) { digitalWrite(motorPin1, bitRead(lookup[out], 0)); digitalWrite(motorPin2, bitRead(lookup[out], 1)); digitalWrite(motorPin3, bitRead(lookup[out], 2)); digitalWrite(motorPin4, bitRead(lookup[out], 3)); } celui de mes capteurs : #define trigPin 12 #define echoPin 13 void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { int duration, distance,pos=0,i; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; Serial.print(distance); Serial.println(" cm"); } et voila enfin le code que j'ai essayer de faire avec mais je crains qu'il soit tout faux, je vous laisse en juger : #include <Stepper.h> #define ECHOPINcapteurdroit 13 // Pin to receive echo pulse #define TRIGPINcapteurdroit 12 #define ECHOPINcapteurgauche 4 // Pin to receive echo pulse #define TRIGPINcapteurgauche 5 //declare variables for the motor pins int motorPindroit1 = 6; int motorPindroit2 = 7; int motorPindroit3 = 8; int motorPindroit4 = 9; int motorPingauche1 = 10; int motorPingauche2 = 11; int motorPingauche3 = 12; int motorPingauche4 = 13; int motorSpeed = 1200; //variable to set stepper speed int lookupdroit[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001}; void setup() { Serial.begin (9600); pinMode(ECHOPINcapteurdroit, INPUT); pinMode(TRIGPINcapteurdroit, OUTPUT); pinMode(ECHOPINcapteurgauche, INPUT); pinMode(TRIGPINcapteurgauche, OUTPUT); pinMode(motorPindroit1, OUTPUT); pinMode(motorPindroit2, OUTPUT); pinMode(motorPindroit3, OUTPUT); pinMode(motorPindroit4, OUTPUT); pinMode(motorPingauche1, OUTPUT); pinMode(motorPingauche2, OUTPUT); pinMode(motorPingauche3, OUTPUT); pinMode(motorPingauche4, OUTPUT); } void loop() { int durationdroit ; int distancedroit ; digitalWrite(TRIGPINcapteurdroit, LOW); delayMicroseconds(2); digitalWrite(TRIGPINcapteurdroit, HIGH); delayMicroseconds(10); digitalWrite(TRIGPINcapteurdroit, LOW); durationdroit = pulseIn(ECHOPINcapteurdroit, HIGH); distancedroit = (durationdroit/2) / 29.1; Serial.print(distancedroit); Serial.println(" cm"); int distancegauche ; digitalWrite(TRIGPINcapteurgauche, LOW); delayMicroseconds(2); digitalWrite(TRIGPINcapteurgauche, HIGH); delayMicroseconds(10); digitalWrite(TRIGPINcapteurgauche, LOW); int durationgauche = pulseIn(ECHOPINcapteurgauche, HIGH); distancegauche = (durationgauche/2) / 29.1; Serial.print(distancegauche); Serial.println(" cm"); if(distancedroit < 10) { moteurgauche = Anticlockwise } else { moteurgauche = Clockwise } if(distancegauche < 10) { moteurdroit = Anticlockwise } else { moteurdroit = Clockwise } void anticlockwise() { for(int i = 0; i < 8; i++) { setOutput(i); delayMicroseconds(motorSpeed); } } void clockwise() { for(int i = 7; i >= 0; i--) { setOutput(i); delayMicroseconds(motorSpeed); } } void setOutput(int out) { digitalWrite(motorPin1, bitRead(lookup[out], 0)); digitalWrite(motorPin2, bitRead(lookup[out], 1)); digitalWrite(motorPin3, bitRead(lookup[out], 2)); digitalWrite(motorPin4, bitRead(lookup[out], 3)); }
Merci, je sais que c'est un peut long a lire ^^