Bonjour à tous
Le programme fonctionne bien, il est stable, tout fonctionne, mais par contre je n'arrive pas a accélérer la réponse de l'aiguille, elle est beaucoup trop lente pour simuler un compte tour.
J'ai bien essayer de faire sauter des pas pour aller plus vite, mais là l'aiguille fait n'importe quoi...
Auriez vous une idée ?
#include <Arduino.h>
// --- constantes ---
const int AIN1 = 8;
const int AIN2 = 9;
const int BIN1 = 10;
const int BIN2 = 11;
const int SIGNAL_PIN = A0;
const int POT_PIN = A1;
const int STEP_DELAY = 2; // délai standard pour fluidité
const int STEPS_INIT = 700;
const int LEFT_OFFSET = 50;
const int SIGNAL_THRESHOLD = 5;
const int DEADZONE = 3;
const int MAX_SIGNAL = 400;
const int GAIN = 9;
const int INPUT_OFFSET = 61;
// --- moyenne ---
#define NUM_SAMPLES 1
int samples[NUM_SAMPLES];
int sampleIndex = 0;
// séquence pas à pas
int steps[4][4] = {
{1, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 1},
{1, 0, 0, 1}
};
int stepIndex = 0;
int currentPos = LEFT_OFFSET;
int cycleMax = STEPS_INIT;
bool initDone = false;
// --- fonctions ---
void stepMotor(int step) {
digitalWrite(AIN1, steps[step][0]);
digitalWrite(AIN2, steps[step][1]);
digitalWrite(BIN1, steps[step][2]);
digitalWrite(BIN2, steps[step][3]);
}
void setup() {
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
Serial.begin(115200);
// init buffer
for (int i = 0; i < NUM_SAMPLES; i++) samples[i] = 0;
// --- phase init aiguille (aller-retour complet) ---
int potVal = analogRead(POT_PIN);
cycleMax = map(potVal, 0, 1023, LEFT_OFFSET + 50, STEPS_INIT);
// aller à droite
for (int i = LEFT_OFFSET; i < cycleMax; i++) {
stepIndex = (stepIndex + 1) % 4;
stepMotor(stepIndex);
delay(STEP_DELAY);
}
// retour gauche
for (int i = cycleMax; i > LEFT_OFFSET; i--) {
stepIndex = (stepIndex + 3) % 4;
stepMotor(stepIndex);
delay(STEP_DELAY);
}
currentPos = LEFT_OFFSET;
initDone = true;
// couper bobines
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
}
void loop() {
if (!initDone) return;
// lire et stocker l’échantillon
samples[sampleIndex] = analogRead(SIGNAL_PIN);
sampleIndex = (sampleIndex + 1) % NUM_SAMPLES;
// calcul moyenne
long sum = 0;
for (int i = 0; i < NUM_SAMPLES; i++) sum += samples[i];
int avgSigVal = sum / NUM_SAMPLES;
int correctedVal = avgSigVal - INPUT_OFFSET;
if (correctedVal < 0) correctedVal = 0;
// recalcul cycleMax si signal très faible
if (correctedVal < SIGNAL_THRESHOLD) {
int potVal = analogRead(POT_PIN);
cycleMax = map(potVal, 0, 1023, LEFT_OFFSET + 50, STEPS_INIT);
}
// calcul target
int target;
if (correctedVal < SIGNAL_THRESHOLD) {
target = LEFT_OFFSET;
} else {
long calc = (long)correctedVal * (long)GAIN * (long)(cycleMax - LEFT_OFFSET);
target = LEFT_OFFSET + (calc / MAX_SIGNAL);
target = constrain(target, LEFT_OFFSET, cycleMax);
}
// déplacement vers target
if (abs(currentPos - target) > DEADZONE) {
if (currentPos < target) {
stepIndex = (stepIndex + 1) % 4;
stepMotor(stepIndex);
currentPos++;
} else {
stepIndex = (stepIndex + 3) % 4;
stepMotor(stepIndex);
currentPos--;
}
}
// debug série
static unsigned long lastPrint = 0;
if (millis() - lastPrint > 200) {
lastPrint = millis();
Serial.print("avg="); Serial.print(avgSigVal);
Serial.print(" Target="); Serial.print(target);
Serial.print(" Pos="); Serial.println(currentPos);
}
delay(10);
}