hello english is not my first language sorry
Hello everyone, I'm having a problem controlling a bipolar stepper motor with an Arduino board. Let me briefly explain my situation:
My stepper motor has an axis around which a wire is wound. I have a dipole supplying a variable voltage. This voltage takes a finite number of values (16 in all). To limit the vagaries of precision, I've chosen to define voltage intervals instead. This voltage will be read by my Arduino board. Then, for each voltage value, my motor must have unwound a certain length of wire (relative to the starting position, i.e. when 0mm of wire is unwound). So I've created a list that gives (in the right order) the unwound lengths associated with each voltage interval read. The program will then read the tension, check whether it lies within one of the intervals I've defined, and if so, calculate the number of steps required (relative to the initial state) to unwind the associated length. Next, the program makes the difference between the current position in steps and the position to be reached, and the motor moves as many steps as the difference (in the right direction, thanks to a condition on the sign of the difference). As the voltage will vary during use, the operation is in loop.
However, when I run my code, the wire unwinds once, then nothing, even though I'm still varying the voltage.
My Arduino is a UNO R3
My motor is a bipolar stepper.
My electronic chopper is an L298N
My power supply is 15V
I can give you my connections if you need them, even though I think they're good, since the operation is performed once at each launch. I think the problem lies in the code, but I can't identify it.
Please do not mind the french comments sorry
#include <AccelStepper.h>
// Définitions des pins de contrôle
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
#define EN1 6
#define EN2 5
#define POT_PIN A0
// Initialisation des étapes du moteur
int stepsPerRevolution = 200; // Ajuster selon les spécifications du moteur
float mmPerStep = (8.0 * 3.14159) / 200.0; // Diamètre de l'axe (8 mm) converti en mm par pas
// Listes des intervalles de tension et des longueurs de fil correspondantes
float debutIntervalles[] = {0,1,2,3,4}; // Début des intervalles de tension
float finIntervalles[] = {1,2,3,4,5}; // Fin des intervalles de tension
float longueurs[] = {3,5,6,9,10}; // Longueurs de fil déroulées correspondantes en mm
// Variables pour la position
long targetPosition = 0;
long currentPosition = 0;
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
digitalWrite(EN1, HIGH);
digitalWrite(EN2, HIGH);
Serial.begin(9600);
}
// Fonction pour faire un pas du moteur
void stepMotor(int step) {
switch (step) {
case 0:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 1:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 2:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case 3:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
}
}
// Fonction pour faire tourner le moteur jusqu'à la position cible
void moveToPosition(long target) {
long stepsToMove = target - currentPosition;
int direction = stepsToMove > 0 ? 1 : -1;
stepsToMove = abs(stepsToMove);
for (long i = 0; i < stepsToMove; i++) {
stepMotor((currentPosition + i * direction) % 4);
delay(10); // Ajuster pour contrôler la vitesse
}
currentPosition = target;
}
void loop() {
int potValue = analogRead(POT_PIN);
float voltage = potValue * (5.0 / 1023.0);
// Map la tension lue aux intervalles de longueur de corde
for (int i = 0; i < sizeof(debutIntervalles) / sizeof(debutIntervalles[0]); i++) {
if (voltage >= debutIntervalles[i] && voltage < finIntervalles[i]) {
targetPosition = longueurs[i] / mmPerStep;
break;
}
}
moveToPosition(targetPosition);
delay(50);
}
