Cours:ShieldLcd

De troyesGEII
Révision datée du 18 novembre 2016 à 17:27 par SergeMoutou (discussion | contributions) (Travail à faire)
Aller à : navigation, rechercher

Il s’agit d’une page protégée.

Manette Nunchuk

Pour garder la connectique de la Nunchuk, il vous faut un adaptateur fourni.

Si vous désirez garder le connecteur, voici sa documentation :

Documentation de la Wii-Nunchuk

Voici quelques brochages utiles qu’il est facile de retrouver sur Internet.

I2C SCL SDA
UNO PC5 (Arduino:A5) PC4 (Arduino:A4)
LEONARDO PD0 (Arduino:SCL) PD1 (Arduino:SDA)
Pro Micro (Sparkfun) PD0 (Arduino:3) PD1 (Arduino:2)

Wii Nunchuk help donne aussi des informations importantes sur la manette Nunchuk. En particulier, il vous faut garder à l'esprit que cette manette est faite pour fonctionner sous 3.3 V alors que le monde Arduino fonctionne en général plutôt à 5 V. L'adaptation en tension est réalisée avec le shield qui vous est fourni.

Le point essentiel est que la manette Nunchuk retourne 6 octets dont la signification est la suivante :

Bit
Byte 7 6 5 4 3 2 1 0
0 SX<7:0>
1 SY<7:0>
2 AX<9:2>
3 AY<9:2>
4 AZ<9:2>
5 AZ<1:0> AY<1:0> AX<1:0> BC BZ

(tableau tiré de wiibrew)

SX,SY sont les positions du Joystick analogique en X et Y, tandis que AX, AY, et AZ sont les donnée de l'accéléromètre sur 10 bits suivant les trois axes.

Voici un exemple de lecture des accéléromètres en langage Arduino :

#include <Wire.h>;
 // adresse I2C du nunchuck
#define WII_NUNCHUK_I2C_ADDRESS 0x52
 
// définition d'une variable counter
uint8_t counter;
 
// définition d'un tableau de données
uint8_t data[6];

void setup() {
  Serial.begin(9600);
  // initialisation du nunchuck
  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() { 
  // on demande 6 octets au nunchuck
  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
     int16_t accelX = ((data[2] << 2) + ((data[5] >> 2) & 0x03));
     int16_t accelY = ((data[3] << 2) + ((data[5] >> 4) & 0x03));
     int16_t accelZ = ((data[4] << 2) + ((data[5] >> 6) & 0x03));
     Serial.print("ax = ");
     Serial.print(accelX);
     Serial.print(" : ay = ");
     Serial.print(accelY);
     Serial.print(" : az = ");
     Serial.println(accelZ);
   }
   // un petit delai  pour pas saturer la liaison série.
   delay(1000);
}

Travail à faire

Si l'accéléromètre donne une valeur quand ses positions changent c’est tout simplement parce qu’il est sensible à l'accélération de pesanteur.

1°) On vous demande les valeurs envoyées par l’accéléromètre pour chacun des axes en positif et négatif. Profitez de cet exercice pour bien repérer les trois axes x, y et z de l'accélération. Pour cela chercher à rendre maximum l'accélération sur un des axes : la verticale vous donnera la direction de l'axe correspondant.

2°) On vous demande de repérer le point 0 d'accélération (entre les deux extrêmes, puis de convertir les valeurs données par l'accéléromètre en unité standard Échec d'analyse (L’exécutable <code>texvc</code> est introuvable. Lisez math/README pour le configurer.): ms^{-2} sachant que l'accélération de pesanteur vaut 9,81 S.I. (Échec d'analyse (L’exécutable <code>texvc</code> est introuvable. Lisez math/README pour le configurer.): ms^{-2} )

3°) Choisissez un axe et réalisez la plus grande accélération possible. Combien de ms⁻2 faites-vous (sans vous démonter l'épaule) ?

4°) Faire un programme qui calcule sans arrêt les minima et maxima des accélérations tant que vous n'appuyez pas sur le bouton Z et affiche les résultats si vous appuyez dessus.

5°) Modifier le programme pour être capable d'utiliser les données du joystick analogique en vous aidant de la documentation. Trouver aussi les extrémas et la position de repos pour les deux axes.

Ecran

Librairie

https://code.google.com/p/u8glib/

Exemple

/*

  GraphicsTest.pde
  
  >>> Before compiling: Please remove comment from the constructor of the 
  >>> connected graphics display (see below).
  
  Universal 8bit Graphics Library, http://code.google.com/p/u8glib/
  
  Copyright (c) 2012, olikraus@gmail.com
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification, 
  are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this list 
    of conditions and the following disclaimer.
    
  * Redistributions in binary form must reproduce the above copyright notice, this 
    list of conditions and the following disclaimer in the documentation and/or other 
    materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  


*/


#include "U8glib.h"

// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The following list is incomplete. The complete list of supported 
// devices with all constructor calls is here: http://code.google.com/p/u8glib/wiki/device
U8GLIB_KS0108_128 u8g(4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 2, 13, 0); 		// 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, cs1=14, cs2=15,di=17,rw=16

void u8g_prepare(void) {
  u8g.setFont(u8g_font_6x10);
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setFontPosTop();
}

void u8g_box_frame(uint8_t a) {
  u8g.drawStr( 0, 0, "drawBox");
  u8g.drawBox(5,10,20,10);
  u8g.drawBox(10+a,15,30,7);
  u8g.drawStr( 0, 30, "drawFrame");
  u8g.drawFrame(5,10+30,20,10);
  u8g.drawFrame(10+a,15+30,30,7);
}

void u8g_disc_circle(uint8_t a) {
  u8g.drawStr( 0, 0, "drawDisc");
  u8g.drawDisc(10,18,9);
  u8g.drawDisc(24+a,16,7);
  u8g.drawStr( 0, 30, "drawCircle");
  u8g.drawCircle(10,18+30,9);
  u8g.drawCircle(24+a,16+30,7);
}

void u8g_r_frame(uint8_t a) {
  u8g.drawStr( 0, 0, "drawRFrame/Box");
  u8g.drawRFrame(5, 10,40,30, a+1);
  u8g.drawRBox(50, 10,25,40, a+1);
}

void u8g_string(uint8_t a) {
  u8g.drawStr(30+a,31, " 0");
  u8g.drawStr90(30,31+a, " 90");
  u8g.drawStr180(30-a,31, " 180");
  u8g.drawStr270(30,31-a, " 270");
}

void u8g_line(uint8_t a) {
  u8g.drawStr( 0, 0, "drawLine");
  u8g.drawLine(7+a, 10, 40, 55);
  u8g.drawLine(7+a*2, 10, 60, 55);
  u8g.drawLine(7+a*3, 10, 80, 55);
  u8g.drawLine(7+a*4, 10, 100, 55);
}

void u8g_triangle(uint8_t a) {
  uint16_t offset = a;
  u8g.drawStr( 0, 0, "drawTriangle");
  u8g.drawTriangle(14,7, 45,30, 10,40);
  u8g.drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset);
  u8g.drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53);
  u8g.drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset);
}

void u8g_ascii_1() {
  char s[2] = " ";
  uint8_t x, y;
  u8g.drawStr( 0, 0, "ASCII page 1");
  for( y = 0; y < 6; y++ ) {
    for( x = 0; x < 16; x++ ) {
      s[0] = y*16 + x + 32;
      u8g.drawStr(x*7, y*10+10, s);
    }
  }
}

void u8g_ascii_2() {
  char s[2] = " ";
  uint8_t x, y;
  u8g.drawStr( 0, 0, "ASCII page 2");
  for( y = 0; y < 6; y++ ) {
    for( x = 0; x < 16; x++ ) {
      s[0] = y*16 + x + 160;
      u8g.drawStr(x*7, y*10+10, s);
    }
  }
}

void u8g_extra_page(uint8_t a)
{
  if ( u8g.getMode() == U8G_MODE_HICOLOR || u8g.getMode() == U8G_MODE_R3G3B2) {
    /* draw background (area is 128x128) */
    u8g_uint_t r, g, b;
    b = a << 5;
    for( g = 0; g < 64; g++ )
    {
      for( r = 0; r < 64; r++ )
      {
	u8g.setRGB(r<<2, g<<2, b );
	u8g.drawPixel(g, r);
      }
    }
    u8g.setRGB(255,255,255);
    u8g.drawStr( 66, 0, "Color Page");
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
  {
    u8g.drawStr( 66, 0, "Gray Level");
    u8g.setColorIndex(1);
    u8g.drawBox(0, 4, 64, 32);    
    u8g.drawBox(70, 20, 4, 12);
    u8g.setColorIndex(2);
    u8g.drawBox(0+1*a, 4+1*a, 64-2*a, 32-2*a);
    u8g.drawBox(74, 20, 4, 12);
    u8g.setColorIndex(3);
    u8g.drawBox(0+2*a, 4+2*a, 64-4*a, 32-4*a);
    u8g.drawBox(78, 20, 4, 12);
  }
  else
  {
    u8g.drawStr( 0, 12, "setScale2x2");
    u8g.setScale2x2();
    u8g.drawStr( 0, 6+a, "setScale2x2");
    u8g.undoScale();
  }
}


uint8_t draw_state = 0;

void draw(void) {
  u8g_prepare();
  switch(draw_state >> 3) {
    case 0: u8g_box_frame(draw_state&7); break;
    case 1: u8g_disc_circle(draw_state&7); break;
    case 2: u8g_r_frame(draw_state&7); break;
    case 3: u8g_string(draw_state&7); break;
    case 4: u8g_line(draw_state&7); break;
    case 5: u8g_triangle(draw_state&7); break;
    case 6: u8g_ascii_1(); break;
    case 7: u8g_ascii_2(); break;
    case 8: u8g_extra_page(draw_state&7); break;
  }
}

void setup(void) {

  // flip screen, if required
  //u8g.setRot180();

  
  pinMode(13, OUTPUT);           
  digitalWrite(13, HIGH);  
}

void loop(void) {
  
  // picture loop  
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  
  // increase the state
  draw_state++;
  if ( draw_state >= 9*8 )
    draw_state = 0;
  
  // rebuild the picture after some delay
  delay(150);

}

Touchpad

#define haut A0
#define bas A1
#define droite A2
#define gauche A3

unsigned int posTouchY()
{
  unsigned int pos;
  pinMode(droite,OUTPUT);
  pinMode(gauche,OUTPUT);
  digitalWrite(droite,0);
  digitalWrite(gauche,1);
  delay(1);
  pos=analogRead(haut);
  pinMode(droite,INPUT_PULLUP);
  pinMode(gauche,INPUT_PULLUP);
  return pos;
}

unsigned int posTouchX()
{
  unsigned int pos;
  pinMode(haut,OUTPUT);
  pinMode(bas,OUTPUT);
  digitalWrite(haut,0);
  digitalWrite(bas,1);
  delay(1);
  pos=analogRead(droite);
  pinMode(haut,INPUT_PULLUP);
  pinMode(bas,INPUT_PULLUP);
  return pos;
}


void setup()
{
  Serial.begin(9600);

}

void loop()
{
  Serial.print(posTouchX(),DEC);
  Serial.print(" ");
  Serial.println(posTouchY(),DEC);
  delay(200);
}


Nunchuck

#include <Servo.h>;
#include <Wire.h>;

// Doit être ajusté en fonction de chaque nunchuck
#define ZEROX 530  
#define ZEROY 530
#define ZEROZ 530

// adresse I2C du nunchuck
#define WII_NUNCHUK_I2C_ADDRESS 0x52

// définition d'une variable Servo
Servo servomoteur;

// définition d'une variable counter
int counter;

// définition d'un tableau de données
uint8_t data[6];

void setup() 
{ 
  // on attache le servomoteur à la pin 11 (PWM)
  //Servomoteur.attach(11);
  Serial.begin(9600);

  // initialisation du nunchuck
  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() 
{ 
    // on demande 6 octets au nunchuck
    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);

      // on limite la valeur entre -180 et 180
      int value = constrain(accelY, -180, 180);
      // on mappe cette valeur pour le servomoteur soit entre 0 et 180
      value = map(value, -180, 180, 0, 180);
      // on écrit sur le servomoteur la valeur
      Serial.println(value,DEC);
      //servomoteur.write(value);

      // un petit delai pour pas saturer le servomoteur
      delay(100);
    }
}

preparation tp

#include "Arduino.h"
#include "U8glib.h"

#include "Servo.h";
#include "Wire.h";

// Doit être ajusté en fonction de chaque nunchuck
#define ZEROX 530
#define ZEROY 530
#define ZEROZ 530

// adresse I2C du nunchuck
#define WII_NUNCHUK_I2C_ADDRESS 0x52
uint8_t data[6];
//The setup function is called once at startup of the sketch
U8GLIB_KS0108_128 u8g(4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 2, 13, 0); 		// 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, cs1=14, cs2=15,di=17,rw=16

uint8_t tab[32][16];

int8_t posx=2,posy=8;
int8_t vitx=1,vity=0;

void setup()
{
// Add your initialization code here
	u8g.begin();
	Serial.begin(9600);
	  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();

}

// The loop function is called in an endless loop
void loop()
{
	uint8_t np=0;
	u8g.firstPage();
	  do {
		  for (uint8_t i=0;i<32;i++)
			  for (uint8_t j=0;j<16;j++)
				  if (tab[i][j]!=0) u8g.drawBox(i*4,j*4,4,4);
	  } while( u8g.nextPage() );

	    // on demande 6 octets au nunchuck
	    Wire.requestFrom(WII_NUNCHUK_I2C_ADDRESS, 6);

	    char 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
	      int16_t accelX = ((data[2] << 2) + ((data[5] >> 2) & 0x03) - ZEROX);
	      int16_t accelY = ((data[3] << 2) + ((data[5] >> 4) & 0x03) - ZEROY);
	      int16_t accelZ = ((data[4] << 2) + ((data[5] >> 6) & 0x03) - ZEROZ);
	      Serial.print("X= ");
	      Serial.print(accelX);
	      Serial.print(" Y= ");
	      Serial.println(accelY);
	      // on limite la valeur entre -180 et 180
	      if (accelX<-60) {vitx=-1;vity=0;}
	      if (accelX>60) {vitx=1;vity=0;}
	      if (accelY<-60) {vity=1;vitx=0;}
	      if (accelY>60) {vity=-1;vitx=0;}
	    }


	  posx+=vitx;
	  if (posx>=32) posx =0;
	  if (posx<0) posx = 31;
	  if (posy>=16) posy =0;
	  if (posy<0) posy = 15;
	  posy+=vity;
      Serial.print(" posx= ");
      Serial.println(posx);
      Serial.print(" posy= ");
      Serial.println(posy);
	  /*if (posx>=32)
	  {
		  while(1)
		  {
				u8g.firstPage();
				  do {
					  u8g.setFont(u8g_font_6x10);
					  u8g.setFontRefHeightExtendedText();
					  u8g.setDefaultForegroundColor();
					  u8g.setFontPosTop();
		  u8g.drawStr(20,20,"perdu !");
				  } while( u8g.nextPage() );

		  }
	  }*/
	  tab[posx][posy]=1;
	  delay(500);
//Add your repeated code here
}