[Aide] Ajout d'un nunchuck dans le code d' Unojoy ( Arduino en mode HID)

Bonjour tout le monde et bravo pour le boulot !

Étant assez nouveau dans le monde Arduino, je suis en recherche de conseils...

En suivant ce tuto ( Google Code Archive - Long-term storage for Google Code Project Hosting.) je passe une Arduino Uno en mode DFU
et j'ai créé un contrôleur musical en mode HID avec 3 capteurs distance ( sharp), jusque la la c'est OK
à présent je voudrais intégrer une manette Nunchuck, mais je suis vraiment une bille en code et je ne sais pas comment "mixer" les 2 codes
donc si une bonne âme a un peu de temps à me consacrer... :wink:

voila le code basique qui marche avec les sharp:

#include "UnoJoy.h"

void setup(){
  setupPins();
  setupUnoJoy();
}

void loop(){
  // Always be getting fresh data
  dataForController_t controllerData = getControllerData();
  setControllerData(controllerData);
}

void setupPins(void){
  // Set all the digital pins as inputs
  // with the pull-up enabled, except for the 
  // two serial line pins
  for (int i = 2; i <= 12; i++){
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
  }
  pinMode(A4, INPUT);
  digitalWrite(A4, HIGH);
  pinMode(A5, INPUT);
  digitalWrite(A5, HIGH);
}

dataForController_t getControllerData(void){
  
  // Set up a place for our controller data
  //  Use the getBlankDataForController() function, since
  //  just declaring a fresh dataForController_t tends
  //  to get you one filled with junk from other, random
  //  values that were in those memory locations before
  dataForController_t controllerData = getBlankDataForController();
  // Since our buttons are all held high and
  //  pulled low when pressed, we use the "!"
  //  operator to invert the readings from the pins
  controllerData.triangleOn = !digitalRead(2);
  controllerData.circleOn = !digitalRead(3);
  controllerData.squareOn = !digitalRead(4);
  controllerData.crossOn = !digitalRead(5);
  controllerData.dpadUpOn = !digitalRead(6);
  controllerData.dpadDownOn = !digitalRead(7);
  controllerData.dpadLeftOn = !digitalRead(8);
  controllerData.dpadRightOn = !digitalRead(9);
  controllerData.l1On = !digitalRead(10);
  controllerData.r1On = !digitalRead(11);
  controllerData.selectOn = !digitalRead(12);
  controllerData.startOn = !digitalRead(A4);
  controllerData.homeOn = !digitalRead(A5);
  
  // Set the analog sticks
  //  Since analogRead(pin) returns a 10 bit value,
  //  we need to perform a bit shift operation to
  //  lose the 2 least significant bits and get an
  //  8 bit number that we can use  
  controllerData.leftStickX = analogRead(A0) >> 2;
  controllerData.leftStickY = analogRead(A1) >> 2;
  controllerData.rightStickX = analogRead(A2) >> 2;
  controllerData.rightStickY = analogRead(A3) >> 2;
  // And return the data!
  return controllerData;
}

ensuite j'ai installé cette librairie et je tente d'introduire son code..
http://www.gabrielbianconi.com/arduinonunchuk/

et je branche la nunchuck avec ce schéma ( partie nunchuck uniquement):

voila , j’espère que vous avez tous les éléments

Bonjour,

Voila de quoi comprendre le fonctionnement d'un nunchunk de wii :

Il te suffit de modifier la fonction getControllerData() pour y intégrer tes données du nunchunk en lieu et place des capteurs d'origine (voir la partie "Set the analog sticks").

Hello,
merci beaucoup de t’intéresser à mon cas
bon, je tatonne... j'ai fait ce code qui est validé
j'ai enlevé les sharp pour l'instant et mis le nunchuck sur les slots 4 et 5
mais rien ne semble passer..

j'ai rajouté ces 2 lignes apres le getControllerData()

  byte buffer[6]; // Buffer contenant les 6 précieux octets qui nous intéresse
byte cnt = 0; // index courant de buffer

et les axes dans Set the analog sticks
ça donne ça:

#include "UnoJoy.h"

void setup(){
  setupPins();
  setupUnoJoy();
}

void loop(){
  // Always be getting fresh data
  dataForController_t controllerData = getControllerData();
  setControllerData(controllerData);
}

void setupPins(void){
  // Set all the digital pins as inputs
  // with the pull-up enabled, except for the 
  // two serial line pins
  for (int i = 2; i <= 12; i++){
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
  }
  pinMode(A4, INPUT);
  digitalWrite(A4, HIGH);
  pinMode(A5, INPUT);
  digitalWrite(A5, HIGH);
}

dataForController_t getControllerData(void){
  byte buffer[6]; // Buffer contenant les 6 précieux octets qui nous intéresse
byte cnt = 0; // index courant de buffer
  // Set up a place for our controller data
  //  Use the getBlankDataForController() function, since
  //  just declaring a fresh dataForController_t tends
  //  to get you one filled with junk from other, random
  //  values that were in those memory locations before
  dataForController_t controllerData = getBlankDataForController();
  // Since our buttons are all held high and
  //  pulled low when pressed, we use the "!"
  //  operator to invert the readings from the pins
  controllerData.triangleOn = !digitalRead(2);
  controllerData.circleOn = !digitalRead(3);
  controllerData.squareOn = !digitalRead(4);
  controllerData.crossOn = !digitalRead(5);
  controllerData.dpadUpOn = !digitalRead(6);
  controllerData.dpadDownOn = !digitalRead(7);
  controllerData.dpadLeftOn = !digitalRead(8);
  controllerData.dpadRightOn = !digitalRead(9);
  controllerData.l1On = !digitalRead(10);
  controllerData.r1On = !digitalRead(11);
  controllerData.selectOn = !digitalRead(12);
  controllerData.startOn = !digitalRead(A4);
  controllerData.homeOn = !digitalRead(A5);
  
  // Set the analog sticks
  //  Since analogRead(pin) returns a 10 bit value,
  //  we need to perform a bit shift operation to
  //  lose the 2 least significant bits and get an
  //  8 bit number that we can use  
 byte joy_x_axis = buffer[0]; // joystick axe x (0-255)
  byte joy_y_axis = buffer[1]; // joystick axe y (0-255)
  int accel_x_axis = buffer[2] * 4; // accéléromètre axe x
  int accel_y_axis = buffer[3] * 4; // accéléromètre axe y
  int accel_z_axis = buffer[4] * 4; // accéléromètre axe z
  // And return the data!
  return controllerData;
}

j'ai aussi tenté en ajoutant

#include <Wire.h>
#include <ArduinoNunchuk.h>

que j’avais oublié au début, mais non..
je recois un array immobile..
Une idée..? il manque surement quelque chose..?

si ça peux t'aiguiller, voila l' array obtenue:
bytes out[19]0 0 8 128 128 128 128 0 0 0 0 0 0 0 0 0 0 0

Une idée..? il manque surement quelque chose..?

Juste l'intégralité du code :wink:

Le nunchunck communique en I2C, les données tombent pas comme par magie dans buffer[].
Si tu remplis par le buffer ça ne peut que pas marcher, relie mon article.

Et, oui..désolé, quand je disais que j’étais une bille en code dans mon premier post, c'est pas pour rien... XD
bon, j'ai intégré tout ton code, au passage j'ai du remplacé les send par write et les receive par read

Au final j'ai ce code qui m'envoie bien des données, mais en continu ( même sans bouger le nunchuck) et inexploitables hélas...

#include "UnoJoy.h"
#include <Wire.h>
#include <ArduinoNunchuk.h>

void setup(){
  setupPins();
  setupUnoJoy();
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission (0x52); // Séquence d'initialisation
  Wire.write (0x40);
  Wire.write(0x00);
  Wire.endTransmission ();
}
void handshake() // Handshack, merci captain obvious !
{
  Wire.beginTransmission (0x52);   
  Wire.write (0x00);
  Wire.endTransmission ();
}
  byte buffer[6]; // Buffer contenant les 6 précieux octets qui nous intéresse
byte cnt = 0; // index courant de buffer

void parse()
{
  byte joy_x_axis = buffer[0]; // joystick axe x (0-255)
  byte joy_y_axis = buffer[1]; // joystick axe y (0-255)
  int accel_x_axis = buffer[2] * 4; // accéléromètre axe x
  int accel_y_axis = buffer[3] * 4; // accéléromètre axe y
  int accel_z_axis = buffer[4] * 4; // accéléromètre axe z
 
  byte z_button = 0; // bouton Z
  byte c_button = 0; // bouton c
 
  if ((buffer[5] >> 0) & 1)
    z_button = 1;
 
  if ((buffer[5] >> 1) & 1)
    c_button = 1;
 
  if ((buffer[5] >> 2) & 1)
    accel_x_axis += 2;
 
  if ((buffer[5] >> 3) & 1)
    accel_x_axis += 1;
 
  if ((buffer[5] >> 4) & 1)
    accel_y_axis += 2;
 
  if ((buffer[5] >> 5) & 1)
    accel_y_axis += 1;
 
  if ((buffer[5] >> 6) & 1)
    accel_z_axis += 2;
 
  if ((buffer[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.println();
}


void loop(){
  // Always be getting fresh data
  dataForController_t controllerData = getControllerData();
  setControllerData(controllerData);
  Wire.requestFrom (0x52, 6);
  while (Wire.available ())
  {
    buffer[cnt] = Wire.read();
    cnt++;
  }
 
  if (cnt >= 5)
    parse();
 
  cnt = 0;
  handshake();
  delay (100);
}


void setupPins(void){
  // Set all the digital pins as inputs
  // with the pull-up enabled, except for the 
  // two serial line pins
  for (int i = 2; i <= 12; i++){
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
  }
  pinMode(A4, INPUT);
  digitalWrite(A4, HIGH);
  pinMode(A5, INPUT);
  digitalWrite(A5, HIGH);
}

dataForController_t getControllerData(void){

  // Set up a place for our controller data
  //  Use the getBlankDataForController() function, since
  //  just declaring a fresh dataForController_t tends
  //  to get you one filled with junk from other, random
  //  values that were in those memory locations before
  dataForController_t controllerData = getBlankDataForController();
  // Since our buttons are all held high and
  //  pulled low when pressed, we use the "!"
  //  operator to invert the readings from the pins
  controllerData.triangleOn = !digitalRead(2);
  controllerData.circleOn = !digitalRead(3);
  controllerData.squareOn = !digitalRead(4);
  controllerData.crossOn = !digitalRead(5);
  controllerData.dpadUpOn = !digitalRead(6);
  controllerData.dpadDownOn = !digitalRead(7);
  controllerData.dpadLeftOn = !digitalRead(8);
  controllerData.dpadRightOn = !digitalRead(9);
  controllerData.l1On = !digitalRead(10);
  controllerData.r1On = !digitalRead(11);
  controllerData.selectOn = !digitalRead(12);
  controllerData.startOn = !digitalRead(A4);
  controllerData.homeOn = !digitalRead(A5);
  
  // Set the analog sticks
  //  Since analogRead(pin) returns a 10 bit value,
  //  we need to perform a bit shift operation to
  //  lose the 2 least significant bits and get an
  //  8 bit number that we can use  
 byte joy_x_axis = buffer[0]; // joystick axe x (0-255)
  byte joy_y_axis = buffer[1]; // joystick axe y (0-255)
  int accel_x_axis = buffer[2] * 4; // accéléromètre axe x
  int accel_y_axis = buffer[3] * 4; // accéléromètre axe y
  int accel_z_axis = buffer[4] * 4; // accéléromètre axe z
  // And return the data!
  return controllerData;
}

Copier coller ne fait pas tout !

parse() d'origine fait un affichage sur le port série, tu doit prendre la partie communication de parse() et l'intégrer dans getControllerData().
Si tu ne vois pas comment faire il va falloir reprend les bases du C et du C++ de zéro :wink:

Ok, ok...
désolé mais je ne pensais pas qu'il fallait des bases de C ou C++ pour faire de l'arduino
Il me semble que beaucoup de gens sur les forums utilisent des codes existant en l'adaptant un peu et sans comprendre forcement toutes les subtilités ?
après je peux comprendre que ça énerve un connaisseur... :frowning:

tikitpok:
désolé mais je ne pensais pas qu'il fallait des bases de C ou C++ pour faire de l'arduino

Il faut un minimum pour tout, tu ne peut pas coder quelque chose sans comprendre les bases du langage ...

tikitpok:
Il me semble que beaucoup de gens sur les forums utilisent des codes existant en l'adaptant un peu et sans comprendre forcement toutes les subtilités ?
après je peux comprendre que ça énerve un connaisseur... :frowning:

Ça m'énerve pas :wink:
C'est juste pour toi, vu comment c'est parti tu va mettre plusieurs jours avant d'arriver à quelque chose de fonctionnel.

Ps: les personnes qui réutilisent bêtement du code sans comprendre n'ont jamais réussi à faire quelque chose qui marche :wink:

Oui, bien sur, je comprends ton point de vue
Après le rapport temps libre/connaissance à ingurgiter est pas toujours évident... XD
mais je vais chercher ça ...