OK I did my first round of edits. You will have to include the two bottom libraries to your sketch.
If I did things right I set the unit to respond to analog stick input only ( you could change to tilt and disassemble your controller into your helmet)
c
z
c+z
button presses make different sounds and light up the cannon
The laser initializes as soon as you boot the device.
I added a push button sequence to raise the arm to 170 degrees (straight) and if already there leave it
Code:
/*
* Example 6
* Nunchuck control for four servos and two button inputs
* Honus 2007
* This allows the use of a Wii nunchuck as an input device and is modified/extended from the original code
* by Tod E. Kurt and Windmeadow Labs
*2007 Tod E. Kurt, http://todbot.com/blog/
*The Wii Nunchuck reading code is taken from Windmeadow Labs, http://www.windmeadow.com/node/42
*/
// www.facebook.com/ArduinoCenter
// https://blog.underc0de.org/arduino-wii-nunchuck-servo-motores/
// Original Code base credited to Undercode
// Code adapted from Sean Maio Crybabyfx setup
//https://github.com/outcry27/crybabyFX
// Updated by knoxvilles_joker 2017
// http://facebook.com/knoxvillesjoker
// more instructions documented at
// http://alienslegacy.com
#include "Wire.h"
#include "ArduinoNunchuk.h"
#include <Servo.h>
// This is imported from the smartgun setup
#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
// pins used for Serial communication with the audio board
// FX board must be grounded on the uart pin.
// I believe these have to be crossed tx on one end and rx
// on the other end.
//
#define SFX_TX 1
#define SFX_RX 0
// initializes the sound board serial connection
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, NULL);
//Creates the objects to control the servos
ArduinoNunchuk nunchuk = ArduinoNunchuk();
// Servo servoPin1;
// Servo servoPin2;
// Servo servoPin3;
// Servo servoPin4;
int ledPin1 = 13; // Control pin for LED 1
int ledPin2 = 12; // Control pin for LED 2
int servoPin1 = 9; // Control pin for servo motor
int servoPin2 = 8; // Control pin for servo motor
int servoPin3 = 7; // Control pin for servo motor
int servoPin4 = 6; // Control pin for servo motor
int pulseWidth1 = 0; // Amount to pulse the servo 1
int pulseWidth2 = 0; // Amount to pulse the servo 2
int pulseWidth3 = 0; // Amount to pulse the servo 3
int pulseWidth4 = 0; // Amount to pulse the servo 4
int refreshTime = 20; // the time in millisecs needed in between pulses
//Initializes the variables
int xjoystick;
int yjoystick;
int xtilt;
int ytilt;
long lastPulse1;
long lastPulse2;
long lastPulse3;
long lastPulse4;
int minPulse = 700; // minimum pulse width
int loop_cnt=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(19200);
ss.begin(9600);
Serial.print("loading sound card init\n");
delay(800);
Serial.print("card initialized");
//give the audio board time to power up.
// Otherwise bootup sound will be called before audio
// board is ready.
// this plays an initialization sound.
Serial.print("#0\n");
//Initializes nunchuck ans servos
nunchuk.init();
// servoPin1.attach(9);
// servoPin2.attach(8);
// servoPin3.attach(7);
// servoPin4.attach(6);
pinMode(servoPin1, OUTPUT); // Set servo pin as an output pin
pinMode(servoPin2, OUTPUT); // Set servo pin as an output pin
pinMode(servoPin3, OUTPUT); // Set servo pin as an output pin
pinMode(servoPin4, OUTPUT); // Set servo pin as an output pin
pulseWidth1 = minPulse; // Set the motor position to the minimum
pulseWidth2 = minPulse; // Set the motor position to the minimum
pulseWidth3 = minPulse; // Set the motor position to the minimum
pulseWidth4 = minPulse; // Set the motor position to the minimum
pinMode(ledPin1, OUTPUT); // sets the LED pin as output
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin1, LOW); // sets the LED pin LOW (turns it off)
digitalWrite(ledPin2, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
checkNunchuck1();
updateServo1(); // update servo 1 position
checkNunchuck2();
updateServo2(); // update servo 2 position
checkNunchuck3();
updateServo3(); // update servo 3 position
checkNunchuck4();
updateServo4(); // update servo 4 position
if(nunchuk.zButton == 1) { // light the LED if z button is pressed
digitalWrite(ledPin1, HIGH);
Serial.print("#1\n");
}
else {
digitalWrite(ledPin1,LOW);
}
if(nunchuk.cButton == 1) { // light the LED if c button is pressed
digitalWrite(ledPin2, HIGH);
Serial.print("#2\n");
}
else {
digitalWrite(ledPin2,LOW);
}
if(nunchuk.cButton == 1 && nunchuk.zButton == 1) {
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, HIGH);
Serial.print("#3\n");
}
else {
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
}
delay(1); // this is here to give a known time per loop
//Guardamos los valores que nos manda el Nunchuk en las variables
xjoystick = nunchuk.analogX;
xjoystick = constrain(xjoystick, 26, 226);
xjoystick = map(xjoystick, 26, 226, 0, 180);
yjoystick = nunchuk.analogY;
yjoystick = constrain(yjoystick, 26, 226);
yjoystick = map(yjoystick, 26, 226, 180, 0);
xtilt = nunchuk.accelX;
xtilt = constrain(xtilt, 320, 720);
xtilt = map(xtilt, 320, 720, 180, 0);
ytilt = nunchuk.accelY;
ytilt = constrain(ytilt, 320, 720);
ytilt = map(ytilt, 320, 720, 0, 180);
// This prints the serial status of the nunchuck.
Serial.print ("Joystick X: ");
Serial.print (xjoystick, DEC);
Serial.print ("\t");
Serial.print ("Joystick Y: ");
Serial.print (yjoystick, DEC);
Serial.print ("\t");
Serial.print ("X: ");
Serial.print (xtilt, DEC);
Serial.print ("\t");
Serial.print ("Y: ");
Serial.print (ytilt, DEC);
Serial.print ("\t");
nunchuk.update();
if (nunchuk.cButton == 1) {
Serial.print("--C-- ");
}
if (nunchuk.zButton == 1) {
Serial.print("--Z-- ");
}
if (nunchuk.cButton == 1 && nunchuk.zButton == 1) {
Serial.print("--Z-C--");
}
Serial.print ("\r\n");
}
void checkNunchuck1()
{
if( loop_cnt > 100 ) { // loop()s is every 1msec, this is every 100msec
float tilt = xjoystick; // x-axis, in this case ranges from ~70 - ~185
tilt = (tilt - 70) * 1.5; // convert to angle in degrees, roughly
pulseWidth1 = (tilt * 9) + minPulse; // convert angle to microseconds
// servoPin1.write(xjoystick);
loop_cnt = 0; // reset for
}
loop_cnt++;
}
// called every loop().
// uses global variables servoPin, pulsewidth, lastPulse, & refreshTime
void updateServo1()
{
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse1 >= refreshTime) {
digitalWrite(servoPin1, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth1); // Length of the pulse sets the motor position
digitalWrite(servoPin1, LOW); // Turn the motor off
lastPulse1 = millis(); // save the time of the last pulse
}
}
void checkNunchuck2()
{
if( loop_cnt > 100 ) { // loop()s is every 1msec, this is every 100msec
// nunchuck_get_data();
// nunchuck_print_data();
float tilt = yjoystick; // y-axis, in this case ranges from ~70 - ~185
tilt = (tilt - 70) * 1.5; // convert to angle in degrees, roughly
pulseWidth2 = (tilt * 9) + minPulse; // convert angle to microseconds
loop_cnt = 0; // reset for
}
loop_cnt++;
}
// called every loop().
// uses global variables servoPin, pulsewidth, lastPulse, & refreshTime
void updateServo2()
{
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse2 >= refreshTime) {
digitalWrite(servoPin2, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth2); // Length of the pulse sets the motor position
digitalWrite(servoPin2, LOW); // Turn the motor off
lastPulse2 = millis(); // save the time of the last pulse
}
}
void checkNunchuck3()
{
if( loop_cnt > 100 ) { // loop()s is every 1msec, this is every 100msec
// nunchuck_get_data();
// nunchuck_print_data();
float tilt = xtilt; // x-axis, in this case ranges from ~70 - ~185
tilt = (tilt - 70) * 1.5; // convert to angle in degrees, roughly
pulseWidth3 = (tilt * 9) + minPulse; // convert angle to microseconds
loop_cnt = 0; // reset for
}
loop_cnt++;
}
// called every loop().
// uses global variables servoPin, pulsewidth, lastPulse, & refreshTime
void updateServo3()
{
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse3 >= refreshTime) {
digitalWrite(servoPin3, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth3); // Length of the pulse sets the motor position
digitalWrite(servoPin3, LOW); // Turn the motor off
lastPulse3 = millis(); // save the time of the last pulse
}
}
void checkNunchuck4()
{
if( loop_cnt > 100 ) { // loop()s is every 1msec, this is every 100msec
// nunchuck_get_data();
// nunchuck_print_data();
float tilt = ytilt; // y-axis, in this case ranges from ~70 - ~185
tilt = (tilt - 70) * 1.5; // convert to angle in degrees, roughly
pulseWidth4 = (tilt * 9) + minPulse; // convert angle to microseconds
loop_cnt = 0; // reset for
}
loop_cnt++;
}
// called every loop().
// uses global variables servoPin, pulsewidth, lastPulse, & refreshTime
void updateServo4()
{
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse4 >= refreshTime) {
digitalWrite(servoPin4, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth4); // Length of the pulse sets the motor position
digitalWrite(servoPin4, LOW); // Turn the motor off
lastPulse4 = millis(); // save the time of the last pulse
}
}
Code:
/*
* ArduinoNunchuk.h - Improved Wii Nunchuk library for Arduino
*
* Copyright 2011-2013 Gabriel Bianconi, http://www.gabrielbianconi.com/
*
* Project URL: http://www.gabrielbianconi.com/projects/arduinonunchuk/
*
* Based on the following resources:
* http://www.windmeadow.com/node/42
* http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
* http://wiibrew.org/wiki/Wiimote/Extension_Controllers
*
*/
#ifndef ArduinoNunchuk_H
#define ArduinoNunchuk_H
#include <Arduino.h>
class ArduinoNunchuk
{
public:
int analogX;
int analogY;
int accelX;
int accelY;
int accelZ;
int zButton;
int cButton;
void init();
void update();
private:
void _sendByte(byte data, byte location);
};
#endif
Code:
/*
* ArduinoNunchuk.cpp - Improved Wii Nunchuk library for Arduino
*
* Copyright 2011-2013 Gabriel Bianconi, http://www.gabrielbianconi.com/
*
* Project URL: http://www.gabrielbianconi.com/projects/arduinonunchuk/
*
* Based on the following resources:
* http://www.windmeadow.com/node/42
* http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
* http://wiibrew.org/wiki/Wiimote/Extension_Controllers
*
*/
#include <Arduino.h>
#include <Wire.h>
#include "ArduinoNunchuk.h"
#define ADDRESS 0x52
void ArduinoNunchuk::init()
{
Wire.begin();
ArduinoNunchuk::_sendByte(0x55, 0xF0);
ArduinoNunchuk::_sendByte(0x00, 0xFB);
ArduinoNunchuk::update();
}
void ArduinoNunchuk::update()
{
int count = 0;
int values[6];
Wire.requestFrom(ADDRESS, 6);
while(Wire.available())
{
values[count] = Wire.read();
count++;
}
ArduinoNunchuk::analogX = values[0];
ArduinoNunchuk::analogY = values[1];
ArduinoNunchuk::accelX = (values[2] << 2) | ((values[5] >> 2) & 3);
ArduinoNunchuk::accelY = (values[3] << 2) | ((values[5] >> 4) & 3);
ArduinoNunchuk::accelZ = (values[4] << 2) | ((values[5] >> 6) & 3);
ArduinoNunchuk::zButton = !((values[5] >> 0) & 1);
ArduinoNunchuk::cButton = !((values[5] >> 1) & 1);
ArduinoNunchuk::_sendByte(0x00, 0x00);
}
void ArduinoNunchuk::_sendByte(byte data, byte location)
{
Wire.beginTransmission(ADDRESS);
Wire.write(location);
Wire.write(data);
Wire.endTransmission();
delay(10);
}