Bonjour,
Je tente d'utiliser un récepteur 4voies (fonctionnent très bien avec des servo) pour faire fonctionner un moteur DC.
Mon code permet d'avoir un point mort au milieu du throttle.
Je rencontre plusieurs problèmes :
- quand je pousse les gaz à fond d'un sens ou d'un autre, le moteur s'emballe et reste à fond pendant quelques dizaines de secondes.
- quand je vais dans la partie haute des gaz, le moteur donne des acoups alors que dans l'autre sens, je n'ai aucun problème
voici mon montage et mon code :
#define THROTTLE_SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt
#define THROTTLE_SIGNAL_IN_PIN 2 // INTERRUPT 0 = DIGITAL PIN 2 - use the PIN number in digitalRead
#define NEUTRAL_THROTTLE 1500 // this is the duration in microseconds of neutral throttle on an electric RC Car
#define ENABLE 5
#define DIRA 3
#define DIRB 4
volatile int nThrottleIn = NEUTRAL_THROTTLE;
volatile unsigned long ulStartPeriod = 0;
volatile boolean bNewThrottleSignal = false;
volatile int vitesse = 0;
void setup()
{
// tell the Arduino we want the function calcInput to be called whenever INT0 (digital pin 2) changes from HIGH to LOW or LOW to HIGH
// catching these changes will allow us to calculate how long the input pulse is
attachInterrupt(THROTTLE_SIGNAL_IN, calcInput, CHANGE);
pinMode(ENABLE, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// if a new throttle signal has been measured
if (bNewThrottleSignal)
{
if (nThrottleIn > 1000 && nThrottleIn < 2000) { // une vaine tentative de supprimer les acoups mais qui provoque l'affolement du moteur...
vitesse = min((((nThrottleIn > NEUTRAL_THROTTLE ? nThrottleIn - NEUTRAL_THROTTLE : NEUTRAL_THROTTLE - nThrottleIn) / 5) * 255 / 100), 255);
vitesse = vitesse > 25 ? vitesse : 0; //défini une zone morte
analogWrite(ENABLE, vitesse);
Serial.println(nThrottleIn);
if (nThrottleIn > NEUTRAL_THROTTLE) {
digitalWrite(DIRA, HIGH); //one way
digitalWrite(DIRB, LOW);
} else {
digitalWrite(DIRA, LOW); //one way
digitalWrite(DIRB, HIGH);
}
}
bNewThrottleSignal = false;
}
delay(1);// je me suis demandé si une loop vide risquait de crasher l'arduino
}
void calcInput()
{
if ((bNewThrottleSignal == false) && digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH)
{
ulStartPeriod = micros();
}
else
{
if (ulStartPeriod && (bNewThrottleSignal == false))
{
nThrottleIn = (int)(micros() - ulStartPeriod);
ulStartPeriod = 0;
// tell loop we have a new signal on the throttle channel
// we will not update nThrottleIn until loop sets
// bNewThrottleSignal back to false
bNewThrottleSignal = true;
}
}
}
Auriez-vous une idée ?
Pour info, le petit truc noir qui traine est la schématisation de mon récepteur alimenté en 5v
le circuit n'est pas alimenté par une pile 9v mais par une lipo 3S (11,5v) 450mAh 75C, j'ai tenté avec ou sans l'alimentation usb conservée, ça ne change rien.
Merci pour votre aide.
Cordialement,