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);
}