1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| #include <TMCStepper.h>
#include <SoftwareSerial.h>
// Définitions des broches
#define SERIAL_TX_PIN 4
#define SERIAL_RX_PIN 3
#define EN_PIN 8
#define DIR_PIN 5
#define STEP_PIN 2
#define BUTTON_PIN 7 // Broche pour le bouton-poussoir 1
#define BUTTON2_PIN 6 // Broche pour le bouton-poussoir 2
#define BUZZER_PIN 9 // Broche pour le buzzer passif
// Initialisation de la communication série pour le driver
SoftwareSerial TMCSerial(SERIAL_RX_PIN, SERIAL_TX_PIN);
// Paramètres du driver
#define R_SENSE 0.11f
#define DRIVER_ADDRESS 0b00
TMC2209Stepper driver(&TMCSerial, R_SENSE, DRIVER_ADDRESS);
// Configuration du hachage
const bool USE_STEALTHCHOP = true;
// Variables pour la gestion des boutons et de l'état du moteur
bool moteur_en_marche = false; // Le moteur est éteint au démarrage
bool bouton1_actif = false;
bool bouton2_actif = false;
// Variables pour le sens et les micropas
bool dir_forward = true;
int microsteps = 0;
// Variable pour le délai entre les pas du moteur
unsigned int step_delay_micros = 1000;
// Définition des notes de musique pour le buzzer
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_G4 392
#define NOTE_A4 440
// Mélodie pour le bouton 1 (vitesse lente)
int melodie1[] = {NOTE_E4, NOTE_G4, NOTE_A4};
int durees1[] = {100, 100, 100};
// Mélodie pour le bouton 2 (vitesse rapide)
int melodie2[] = {NOTE_A4, NOTE_G4, NOTE_E4};
int durees2[] = {100, 100, 100};
// Fréquence et durée pour le bip d'arrêt
const int bip_frequence = 800;
const int bip_duree = 100;
// Fonction pour jouer une mélodie
void jouerMelodie(int notes[], int durees[], int taille) {
for (int i = 0; i < taille; i++) {
tone(BUZZER_PIN, notes[i], durees[i]);
delay(durees[i] + 30); // Petit délai entre les notes
}
noTone(BUZZER_PIN);
}
// Fonction pour jouer un simple bip
void jouerBip() {
tone(BUZZER_PIN, bip_frequence, bip_duree);
delay(bip_duree);
noTone(BUZZER_PIN);
}
void setup() {
Serial.begin(115200);
Serial.println("Initialisation du driver TMC2209...");
TMCSerial.begin(115200);
// Configuration des broches
pinMode(EN_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Active la résistance de pull-up interne
pinMode(BUTTON2_PIN, INPUT_PULLUP); // Active la résistance de pull-up interne pour le deuxième bouton
// Démarrage du driver
digitalWrite(EN_PIN, LOW);
driver.begin();
driver.toff(5);
driver.rms_current(1100); // Réglage du courant
driver.microsteps(microsteps);
// Configuration du mode de hachage (StealthChop pour un fonctionnement silencieux)
if (USE_STEALTHCHOP) {
Serial.println("Activation du mode StealthChop");
driver.en_spreadCycle(false);
driver.pwm_autoscale(true);
} else {
Serial.println("Activation du mode SpreadCycle");
driver.en_spreadCycle(true);
driver.pwm_autoscale(false);
}
// Test de connexion
if (driver.test_connection()) {
Serial.println("Connexion reussie!");
Serial.print("Courant RMS: ");
Serial.print(driver.rms_current() / 1000.0);
Serial.println(" A");
Serial.print("Micro-pas: ");
Serial.println(driver.microsteps());
} else {
Serial.println("Erreur de connexion!");
}
}
void loop() {
// Lecture des états des boutons
int etat_bouton1 = digitalRead(BUTTON_PIN);
int etat_bouton2 = digitalRead(BUTTON2_PIN);
// Gère le premier bouton (sens par défaut, 0 micropas)
if (etat_bouton1 == LOW && !bouton1_actif) {
bouton1_actif = true;
if (moteur_en_marche) {
moteur_en_marche = false;
Serial.println("Moteur arrete.");
jouerBip();
} else {
moteur_en_marche = true;
dir_forward = true;
microsteps = 0;
driver.microsteps(microsteps);
Serial.print("Moteur en marche (sens direct, 0 micropas, delai ");
Serial.print(step_delay_micros);
Serial.println(" us).");
jouerMelodie(melodie1, durees1, sizeof(melodie1) / sizeof(melodie1[0]));
}
}
if (etat_bouton1 == HIGH) {
bouton1_actif = false;
}
// Gère le deuxième bouton (sens inverse, 4 micropas)
if (etat_bouton2 == LOW && !bouton2_actif) {
bouton2_actif = true;
if (moteur_en_marche) {
moteur_en_marche = false;
Serial.println("Moteur arrete.");
jouerBip();
} else {
moteur_en_marche = true;
dir_forward = false;
microsteps = 4;
driver.microsteps(microsteps);
Serial.print("Moteur en marche (sens inverse, 4 micropas, delai ");
Serial.print(step_delay_micros);
Serial.println(" us).");
jouerMelodie(melodie2, durees2, sizeof(melodie2) / sizeof(melodie2[0]));
}
}
if (etat_bouton2 == HIGH) {
bouton2_actif = false;
}
// Contrôle du moteur
if (moteur_en_marche) {
digitalWrite(EN_PIN, LOW);
if (dir_forward) {
digitalWrite(DIR_PIN, HIGH);
} else {
digitalWrite(DIR_PIN, LOW);
}
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(step_delay_micros);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(step_delay_micros);
} else {
digitalWrite(EN_PIN, HIGH);
}
} |