wii nunchuck et servo diy un conseil ?

Bonjour,

Mon but est de faire bouger une plateforme sur deux axes, ceci a l' aide de moteurs d' essuie glace, un par axe.
J' utilise arduino Uno avec une carte de puissance sabertooth de Dimension Engineering et un potentiometre pour controler mon moteur en position
J' ai adapté des morceaux de codes existant pour exploiter les données d' une wii nunchuck sur l' axe X, et cela fonctionne.
En revanche je rencontre un problème pour écrire le code pour deux moteurs, quelqu un pourrait il me donner un tuyau de structure de code, c est à dire comment écrire mon code pour que chaque opération ait lieu pour chaque moteur?Merci d' avance

Voici ici mon code pour un seul moteur

  // first draft from " bayesian adventure "
  // lundi 31 octobre
   // réécriture a partir de decompositiondiyservosaber fonctionne sur moteur 2;

#include <Wire.h>  
#include <SabertoothSimplified.h>
#include <SoftwareSerial.h>
#define ZEROX 530  
#define ZEROY 530
#define ZEROZ 530
#define WII_NUNCHUK_I2C_ADDRESS 0x52
#define POT_VALUE_MAX 700 // potentiometer reading when motor shaft is at 180 degree position. //You will need to fill this value according to your setup.See below....
#define POT_VALUE_MIN 200//potentiometer reading when when motor shaft is at 0 degree position. 
#define PERM_ERROR 3 
#define MAX_ANGLE 180

//we will allow our motor to turn by a maximum angle of 180 degrees 
SoftwareSerial SWSerial(NOT_A_PIN, 8); //declare the Pin 8 to be used to communicate with Sabertooth
SabertoothSimplified ST(SWSerial); //open a serial COM on previously identified pin


int motpotPin =A0;
int counter;
uint8_t data[6];
int M_Speed;
int sens;
int turnDirection;
enum turnDirection { right,left};
int valueX;
int val;

//********************************** 
void SetTurnSpeed (int s )
{
  M_Speed = s;}

  
void SetTurnDirection(int dir)//Setting turn directions on L298 IC 
{ 
turnDirection = dir;

 switch(turnDirection) 
{ 
case right: //turning Right //motor moves CW 
sens = 1; 
break; 

case left: //turning Left //motor moves CCW 
sens = -1;
break; 
} 
}
 
void Turn ()
{ST.motor ( 1,sens*M_Speed);
  }

void Stop ()
{ST.motor ( 1, 0);
  }

void GoToAngle ( int target,int howFast)
{  
 // find current angle
 int currentAngle = (( float)analogRead(motpotPin) - POT_VALUE_MIN)/(POT_VALUE_MAX - POT_VALUE_MIN) * MAX_ANGLE;
  
// decide if we need to go clockwise or counterclockwise
if (currentAngle < target) 
{ 
SetTurnDirection ( right );}
 
else if (currentAngle > target) 
{ 
SetTurnDirection ( left);}

SetTurnSpeed ( howFast);
 while(abs(currentAngle - target) > PERM_ERROR  )   //if the shaft is not at the required value, 
{ 
Turn();       //Keep on turning the shaft 
delay ( 5);  // wait a moment
// update the angle
 currentAngle = (( float)analogRead(motpotPin) - POT_VALUE_MIN)/(POT_VALUE_MAX - POT_VALUE_MIN) * MAX_ANGLE; 
}

Stop(); //Stop the shaft after the error is acceptable 
} 
 

void setup ()
{
 Serial.begin ( 9600);
 SWSerial.begin (38400); // switches 2,4,5 OFF
 Wire.begin();

  Wire.beginTransmission(WII_NUNCHUK_I2C_ADDRESS);
  Wire.write(0xF0);
  Wire.write(0x55);
  Wire.endTransmission();

  Wire.beginTransmission(WII_NUNCHUK_I2C_ADDRESS);
  Wire.write(0xFB);
  Wire.write(0x00);
  Wire.endTransmission();
} 
  
void loop ()
{
  int turnSpeed = 70;
  Wire.requestFrom(WII_NUNCHUK_I2C_ADDRESS, 6);


    counter = 0;
    // tant qu'il y a des données
    while(Wire.available())
    {
      // on récupère les données
      data[counter++] = Wire.read();
    }

    // on réinitialise le nunchuck pour la prochaine demande
    Wire.beginTransmission(WII_NUNCHUK_I2C_ADDRESS);
    Wire.write(0x00);
    Wire.endTransmission();

    if(counter >= 5)
    {
      // on extrait les données
      // dans mon exemple j'utilise uniquement les données d'accélération sur l'axe Y
      double accelX = ((data[2] << 2) + ((data[5] >> 2) & 0x03) - ZEROX);
      double accelY = ((data[3] << 2) + ((data[5] >> 4) & 0x03) - ZEROY);
      double accelZ = ((data[4] << 2) + ((data[5] >> 6) & 0x03) - ZEROZ);

    
       int valueX = constrain(accelX, -180, 180);
       valueX = map(valueX, -180, 180, 0, 180);

     Serial.print ( " valueX "); // to control wii nunchuck data
     Serial.println( valueX  );
     
   GoToAngle(valueX,turnSpeed);
    }
}

Question résolue :wink: