Bonjour à tous,
je sollicite votre aide car j'ai un projet d'électrifier un chariot de golf. j'ai récupéré d'une ancienne trottinette la roue motrice et la batterie. j'ai acheté un contrôleur 36V. 250W. je souhaite pouvoir contrôler la vitesse du moteur via une télécommande IR.
le problème est que je n'arrive pas à faire varier la vitesse du moteur avec le code ci-dessous. le moteur fonctionne car lorsque je joue Digitalwrite(HIGH) le moteur se lance à fond.
j'ai essayé la pin 8 digitalwrite et la 9 avec analogwrite, même effet le moteur fait du bruit mais ne tourne pas.
Ci dessous le code.
merci à vous
//****************librairies***************
#include <Arduino.h>
#include <Servo.h>
#include "PinDefinitionsAndMore.h"
#include <IRremote.hpp> // include the library
// ***************Constantes******************
const int motor =12;
int comptD =90;
int comptV =0;
Servo esc;
Servo myservo;
//******************SETUP**************
void setup() {
Serial.begin(9600);
myservo.attach(7);
esc.attach(8);
//if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
//printActiveIRProtocols(&Serial);
//pinMode(motorPin,OUTPUT);
}
void loop() {
if (IrReceiver.decode()) {
/*
* Print a summary of received data
*/
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));
// Nous avons ici un protocole inconnu, veuillez imprimer des informations supplémentaires
IrReceiver.printIRResultRawFormatted(&Serial, true);
IrReceiver.resume(); // Faites-le ici, afin de préserver les données brutes pour l'impression avec printIRResultRawFormatted()
} else {
IrReceiver.resume(); // Réception anticipée de la trame IR suivante
IrReceiver.printIRResultShort(&Serial);
IrReceiver.printIRSendUsage(&Serial);
}
Serial.println();
/*
* Finally, check the received data and perform actions according to the received command
*/
if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) {
Serial.println(F("Répétition reçue. Ici, vous pouvez répéter la même action qu'auparavant."));
} else {
//********************Comande de direction ****************
if (IrReceiver.decodedIRData.command == 0x6) {//16
Serial.println(comptD);
comptD=comptD-10;
Serial.println(comptD);
if (comptD>=0 && comptD<=180)
{
myservo.write (comptD);
delay (15);
}
else
{
comptD=0;
}
} else if (IrReceiver.decodedIRData.command == 0x7) {//15
comptD=comptD+10;
if (comptD>=0 && comptD<=180)
{
myservo.write (comptD);
delay (15);
}
else
{
comptD=180;
}
}
//***********************COMMANDE DE VITESSE***********************
else if (IrReceiver.decodedIRData.command == 0x40) {//14
Serial.println(F("Received command vitesse+."));
Serial.println(comptV);
comptV=comptV+100;
if (comptV>=0 && comptV<=1000)
{
esc.write (comptV);
delay (15);
Serial.println(comptV);
}
else
{
comptV=1000;
}
}else if (IrReceiver.decodedIRData.command == 0x28) {//13
Serial.println(F("Received command vitesse-."));
Serial.println(comptV);
comptV=comptV-10;
Serial.println(comptV);
if (comptV>=0 && comptV<=256)
{
esc.write (comptV);
delay (15);
}
else
{
comptV=0;
}
//**************** Remise à Zéro des commandes ***************
}else if (IrReceiver.decodedIRData.command == 0x44) {//35
myservo.write(90); // remet le servo à 90 dg
comptD=90; // réinitialise le compteur de direction
esc.write (0);
comptV=0;
}
}
}
}