• Bonsoir, je me rends juste compte que les notifications des nouveaux messages étaient limitées aux membres actifs dans les 30 derniers jours. Donc, j'ai supprimé cette option. En espérant que vous aurez bien les notifications attendues. Merci pour votre patience. Yves

Programmation Arduino pour Nunchuk .

Voici le code , j' espère que ce ne sera pas trop grand :rolleyes:

#include <Wire.h>
#include <string.h>

#undef int
#include <stdio.h>

uint8_t outbuf[6]; // array to store arduino output
int cnt = 0;
int ledPin = 13;
int ledPin1 = 11; // Led broche digital PWM 11 pour visualisation du signal


void
setup ()
{
beginSerial (19200);
Serial.print ("Finished setup\n");
Wire.begin (); // join i2c bus with address 0x52
nunchuck_init (); // send the initilization handshake
pinMode(ledPin1, OUTPUT); // configure broche 11 en sortie

}



void
nunchuck_init ()
{
Wire.beginTransmission (0x52); // transmit to device 0x52
Wire.send (0x40); // sends memory address
Wire.send (0x00); // sends sent a zero.
Wire.endTransmission (); // stop transmitting
}

void
send_zero ()
{
Wire.beginTransmission (0x52); // transmit to device 0x52
Wire.send (0x00); // sends one byte
Wire.endTransmission (); // stop transmitting
}

void
loop ()
{
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ())
{
outbuf[cnt] = nunchuk_decode_byte (Wire.receive ()); // receive byte as an integer
digitalWrite (ledPin, HIGH); // sets the LED on
cnt++;
}

// If we recieved the 6 bytes, then go print them
if (cnt >= 5)
{
print ();
}
{
analogWrite(ledPin1,outbuf[3] -50 ) ; //commande en PWM la led1 , j'ai mis -50 à outbuf[3] au hazard .
}

cnt = 0;
send_zero (); // send the request for next bytes
delay (100);
}

// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits. That is why I
// multiply them by 2 * 2
void
print ()
{
int joy_x_axis = outbuf[0];
int joy_y_axis = outbuf[1];
int accel_x_axis = outbuf[2] * 2 * 2;
int accel_y_axis = outbuf[3] * 2 * 2;
int accel_z_axis = outbuf[4] * 2 * 2;


int z_button = 0;
int c_button = 0;

// byte outbuf[5] contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf[5]
if ((outbuf[5] >> 0) & 1)
{
z_button = 1;
}
if ((outbuf[5] >> 1) & 1)
{
c_button = 1;
}

if ((outbuf[5] >> 2) & 1)
{
accel_x_axis += 2;
}
if ((outbuf[5] >> 3) & 1)
{
accel_x_axis += 1;
}

if ((outbuf[5] >> 4) & 1)
{
accel_y_axis += 2;
}
if ((outbuf[5] >> 5) & 1)
{
accel_y_axis += 1;
}

if ((outbuf[5] >> 6) & 1)
{
accel_z_axis += 2;
}
if ((outbuf[5] >> 7) & 1)
{
accel_z_axis += 1;
}

Serial.print (joy_x_axis, DEC);
Serial.print ("\t");

Serial.print (joy_y_axis, DEC);
Serial.print ("\t");

Serial.print (accel_x_axis, DEC);
Serial.print ("\t");

Serial.print (accel_y_axis, DEC);
Serial.print ("\t");

Serial.print (accel_z_axis, DEC);
Serial.print ("\t");

Serial.print (z_button, DEC);
Serial.print ("\t");

Serial.print (c_button, DEC);
Serial.print ("\t");

Serial.print ("\r\n");
}

// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char
nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
 
Alors avec ce code en sortie j'ai environ 0.400 Volt à 2.270 Volts avec 1.278 Volts à l'horizontale .

(Petite précision , depuis le début je parle de signal analogique mais en faite le signal que fournit l'Arduino est en PWM .)

Je ne connais pas la fonction "int out" , et d'où viens "22/9" et "2304/13" je suis très très débutant en Programmation ça doit faire 4 semaines que j'ai l'Arduino . Un vrai boulet en fait . :(
 
Essaye ça pour voir si la valeur renvoyée est correcte. J'ai fait rapidement et j'ai pas de nunchuck pour tester, mais j'ai pas d'erreur de compilation c'est déjà çà.
La fonction de conversion faut pas trop regarder sa tête, les matheux rigolerons...

J'ai acheté ma carte arduino y'a 1 mois et demi à peu près comme toi, vraiment c'est sympathique quand on voit le nombre d'applications possibles, particulièrement en modélisme. Et le prix est encourageant ;)

++

MadProf

Code:
#include <Wire.h>
#include <string.h>

#undef int
#include <stdio.h>

uint8_t outbuf[6];    // array to store arduino output
int cnt = 0;
int ledPin = 13;
int ledPin1 = 11;     // Led broche digital PWM 11 pour visualisation du signal
int nouvelleValeur = 128;
int valeurRetour = 128;

void
setup ()
{
  beginSerial (19200);
  Serial.print ("Finished setup\n");
  Wire.begin ();        // join i2c bus with address 0x52
  nunchuck_init ();                   // send the initilization handshake
  pinMode(ledPin1, OUTPUT);      // configure broche 11 en sortie 

}

 

void
nunchuck_init ()
{
  Wire.beginTransmission (0x52);    // transmit to device 0x52
  Wire.send (0x40);        // sends memory address
  Wire.send (0x00);        // sends sent a zero. 
  Wire.endTransmission ();    // stop transmitting
}

void
send_zero ()
{
  Wire.beginTransmission (0x52);    // transmit to device 0x52
  Wire.send (0x00);        // sends one byte
  Wire.endTransmission ();    // stop transmitting
}

void
loop ()
{
  Wire.requestFrom (0x52, 6);    // request data from nunchuck
  while (Wire.available ())
    {
      outbuf[cnt] = nunchuk_decode_byte (Wire.receive ()); // receive byte as an integer
      digitalWrite (ledPin, HIGH);    // sets the LED on
      cnt++;
    }

  // If we recieved the 6 bytes, then go print them
  if (cnt >= 5)
    {
      print ();
    }
    {
      valeurRetour = nunchuk_decode_byte(outbuf[3]); //je décompose ici volontairement : tu récupère ta donnée sous la forme d'un entier
      valeurRetour = conversion(valeurRetour); //ensuite on converti avec la fonction "conversion" en dessous.
     analogWrite(ledPin1, valeurRetour) ;   //commande en PWM la led1 , j'ai mis -50 à outbuf[3] au hazard .
    }

  cnt = 0;
  send_zero (); // send the request for next bytes
  delay (100);
}

//la fonction de conversion (qui doit etre acceptable mais pas parfaite, je peux pas tester j'ai pas de nunchuck :( )
//elle prend un entier compris entre 72 et 176 en parametre, et renvoi un entier compris entre 0 et 255
int conversion (int x){
  nouvelleValeur=(int)(2805*x/1144-(2295/13));
  return nouvelleValeur;
  }

// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits.  That is why I
// multiply them by 2 * 2
void
print ()
{
  int joy_x_axis = outbuf[0];
  int joy_y_axis = outbuf[1];
  int accel_x_axis = outbuf[2] * 2 * 2;
  int accel_y_axis = outbuf[3] * 2 * 2;
  int accel_z_axis = outbuf[4] * 2 * 2;


  int z_button = 0;
  int c_button = 0;

// byte outbuf[5] contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf[5]
  if ((outbuf[5] >> 0) & 1)
    {
      z_button = 1;
    }
  if ((outbuf[5] >> 1) & 1)
    {
      c_button = 1;
    }

  if ((outbuf[5] >> 2) & 1)
    {
      accel_x_axis += 2;
    }
  if ((outbuf[5] >> 3) & 1)
    {
      accel_x_axis += 1;
    }

  if ((outbuf[5] >> 4) & 1)
    {
      accel_y_axis += 2;
    }
  if ((outbuf[5] >> 5) & 1)
    {
      accel_y_axis += 1;
    }

  if ((outbuf[5] >> 6) & 1)
    {
      accel_z_axis += 2;
    }
  if ((outbuf[5] >> 7) & 1)
    {
      accel_z_axis += 1;
    }

  Serial.print (joy_x_axis, DEC);
  Serial.print ("\t");

  Serial.print (joy_y_axis, DEC);
  Serial.print ("\t");

  Serial.print (accel_x_axis, DEC);
  Serial.print ("\t");

  Serial.print (accel_y_axis, DEC);
  Serial.print ("\t");

  Serial.print (accel_z_axis, DEC);
  Serial.print ("\t");

  Serial.print (z_button, DEC);
  Serial.print ("\t");

  Serial.print (c_button, DEC);
  Serial.print ("\t");

  Serial.print ("\r\n");
}

// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char
nunchuk_decode_byte (char x)
{
  x = (x ^ 0x17) + 0x17;
  return x;
}
 
Je viens d'essayer ton code , le signal est bizarre la tension ne cesse de varier (1.8 V à 1.5 V avec 1.4 V à l 'horizontale ) il n'est pas proportionnel .

En fait pendant le mouvement du nunchuk la diode clignote "aléatoirement " .
(variation de luminosité , elle ne s'éteint pas)



C'est clair que l'Arduino est sympathique , sur le net il y a plein de projet à base d' Arduino , du jeu de lumière , au pilote automatique d' Avion RC en passant par des Robots en tous genre ...

Et en modélisme j'ai déjà pensé à moult systèmes , notamment pour le Vol en immersion . D'ailleur ce montage me servira à faire bouger la caméra de mon Easystar .

PS: Je te remerci encore pour ton aide , surtout quand je vois l'heure à laquelle tu a posté le code !
 
Oui en y repensant ça ne doit pas fonctionner comme je le pensais. Je jette un œil tout à l'heure. J'ai vu que tu as posté sur le forum Arduino également, il me semble qu'une des réponse donnait une bonne piste.

T'inquiète si je poste tard c'est normal, je me couche jamais avant 2h30 :D

++

MadProf
 
Haut