Wii nunchuck z button

Hi all

im trying to build a remote control water cannon using a Wii nunchuck, nema 17, tb6600 and either an uno or mega. (currently trying on the uno). i have control of the motor but am looking to add a relay shield for the pump. very new to coding and cant work out how to get the z and c buttons working. any pointer much appreciated.


#include <AccelStepper.h>
#include <Wire.h>
#include <nunchuck_funcs.h>

#define Pump 4

// Define a stepper and the pins it will use
AccelStepper stepper(1, 8, 9);


byte joyx; // The joystick
int topspd = 300; // Speed limit
int stick; // The mapped value from the joystick
int spd; // Extra variable so that the serial read out doesn't add 100 decimal places
int z_button = 0;


long receivedMMdistance = 0; //distance in mm from the computer
long receivedDelay = 0; //delay between two steps, received from the computer
long receivedAcceleration = 0; //acceleration value from computer
char receivedCommand; //character for commands
/* s = Start (CCW) // needs steps and speed values
 * o = open (CCW) // needs steps and speed values
 * c = close (CW) //needs steps and speed values
 * a = set acceleration // needs acceleration value
 * n = stop right now! // just the 'n' is needed
 */
 
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag

void setup()
{  
  Serial.begin(115200);
  nunchuck_setpowerpins();
  nunchuck_init(); // send the initilization handshake

  stepper.setMaxSpeed(500); // Sets the maximum speed to the user defined limit
  stepper.setSpeed(0); // Gives the stepper an initial speed of 0
if ((nunchuck_buf[5] >> 0) & 1) 
        z_button = 1;

}

void loop()
{
  nunchuck_get_data();
        joyx  = nunchuck_joyx(); // Define the joystick value as joyx
  stick = joyx;

if( nunchuck_zbutton() )      // light the LED if z button is pressed
    digitalWrite(Pump, HIGH);
  else 
    digitalWrite(Pump,LOW);
 
  // Map joyx extremes to the positive and negative top speed
  // and reduce the resolution to 10 intervals per direction
  stick = map(stick, 35, 223, -topspd/10, topspd/10);
  
  stepper.setSpeed(stick*10); // Changes the stepper speed in intervals of 10
  stepper.runSpeed();
  spd = stepper.speed(); // Reduced the number of decimal places that print
  
        Serial.print("joyx: "); Serial.print((byte)joyx,DEC);
        Serial.print("\tstick: "); Serial.print(stick,DEC);
        Serial.print("\tspd: "); Serial.println(spd,DEC);
        Serial.print("butZ: "); Serial.println(z_button, DEC);
}

????

Schematics would be helpful....

hi railroader,

im not sure how i would be able to add the wii nunchuck into the schematic as its an i2c device.
the z and c buttons are already on the nunchuck

Okey. I2C is familiar. Then the question is what/how that device wants buttons to be connected. Debounce or not, pullup or pulldown etc......
Datasheets for that device if You don't feel You know "how to".

this is as close as i can find to a data sheet https://bootlin.com/labs/doc/nunchuk.pdf

Not sure where you found the library you are using. The ones found by the Library Manager (NintendoExtensionCtrl and WiiChuck) have functions to return the button state as a boolean.

That's okey. The question is if there's a lib handling startup (.begin) and reading or if You need to code it on Your own. Shouldn't be too difficult.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.