wii nunchuck 4 servo control

ksj

Blooded
Does anyone have any good code to control 4 servos with a wii nunchuck?

I can find code all day long to control 2 servos with just an arduino and a wii nunchuck.

Honus code base seems to only work with his specific setup ( finally have all the parts in to build it with his board layouts. I am hoping to have better luck with his code with that setup.
 

ksj

Blooded
OK, I figured out how Honus did it 2 servos controlled with the joystick and 2 more with the yaw/pitch functionality. Can't believe I missed that little tidbit after looking through his code numerous times.
 

ksj

Blooded
Will test to see if I can control 2 servos on the same channel PWM pin to see if I can run dual plasma casters.
 

Shogun

Hunter
Just OMG ksj ... I really cannot help you mate :confused: these animatronics programming stuff gets me confused soooooo much
 

ksj

Blooded
Programming is like any language, there are rules, some have to be followed absolutely others, not so much.

One guy figured out that language can be equated to math and when you follow the formula you can write legal documents that are well written and very much legally binding.

Programming is not too terribly different. Instead of conveying a message you are conveying a command that has to be received in a certain manner.

As with all things you learn and get better the more you practice and with the more mis steps that you have. A mistake is only a failure if you learn nothing from the experience.
 

ksj

Blooded
Take it back I have some bugs I am working through. I think I found a fix though...
 

ksj

Blooded
OK, I have it working and I am calling things done at this point. I couldn't get fading to work properly, but I will live with the results at this point:

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
 * 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
 *   http://www.gammon.com.au/blink
 *
 */
// 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>

//Creates the objects to control the servos
ArduinoNunchuk nunchuk = ArduinoNunchuk();

const int ledPin1 = 13;       // Control pin for LED 1
const byte ledPin2 = 12;       // Control pin for LED 2
const unsigned long ledPin2interval = 500;
unsigned long ledPin2timer;
const int servoPin1 = 9;      // Control pin for servo motor
const int servoPin2 = 11;      // Control pin for servo motor
const int servoPin3 = 10;      // Control pin for servo motor
const int servoPin4 = 6;      // Control pin for servo motor
// sound pins for firing sounds
const int firePin1 = 4;
const int firePin2 = 5;
const int firePin3 = 16;
const int startSoundPin = 17;
 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:
// sound pins are setup before initialization of serial interfaces.
  // initialize the audio pins
  pinMode(startSoundPin, OUTPUT);
  pinMode(firePin1, OUTPUT);
  pinMode(firePin2, OUTPUT);
  pinMode(firePin3, OUTPUT);
  // set up the audio trigger pins to give a path to GND when set to OUTPUT
  analogWrite(startSoundPin, LOW);
  analogWrite(firePin1, LOW);
  analogWrite(firePin2, LOW);
  analogWrite(firePin3, LOW);

  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);
  ledPin2timer = millis ();
  digitalWrite(ledPin1, LOW); // sets the LED pin LOW (turns it off)
  digitalWrite(ledPin2, LOW);

Serial.begin(19200);
 Serial.print("loading sound card init\n");
  delay(1000); 
  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.
  digitalWrite(startSoundPin, HIGH);
  //Serial.print("#00\n");
  delay(300);
  digitalWrite(startSoundPin, LOW);
  delay(300);
    //Initializes nunchuck and servos
  nunchuk.init();

}

void ledPin2toggle ()
  {
   if (digitalRead (ledPin2) == LOW)
      digitalWrite (ledPin2, HIGH);
   else
      digitalWrite (ledPin2, LOW);

  // remember when we toggled it
  ledPin2timer = millis (); 
  }  // end of toggleGreenLED

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);
    digitalWrite(firePin1, HIGH);
  //  Serial.print("#3\n");
    //  3 = 1
    delay(300);
    digitalWrite(ledPin1,LOW);
    digitalWrite(firePin1, LOW);
    delay(300);
   }

    if (nunchuk.cButton == 1)  {
      if ( (millis () - ledPin2timer) >= ledPin2interval) {
     ledPin2toggle ();
      }
    digitalWrite(firePin2, HIGH);
//  Serial.print("#4\n");
//  4 = 4
//    delay(300);
    
    digitalWrite(firePin2, LOW);
//    delay(300);
   }



    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
    analogWrite(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
    analogWrite(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
    analogWrite(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
    analogWrite(servoPin4, LOW);    // Turn the motor off
    lastPulse4 = millis();            // save the time of the last pulse
  }
}
 

ksj

Blooded
I will add more thorough documentation on the code itself soon. I have done what I could to ensure I included all sites where I pulled information from.

This works with off the shelf components and is something anyone can build if they can use a soldering iron. estimated cost in parts is under a 100.

I will focus on building the canon backpack. I would also note that this would be easy to do dual canons with. I just have not figured out a cheap and easy way to split canon rotation on both ends of the canon arm. I am not happy that I can not get the arm any smaller than I will have it now.

My builds are more for functionality and easy field maintenance with low investment cost. I will do my best to get something darn close to movie looks.

I was almost thinking lego had some sort of setup I could use.

Any servo gear sets I see are very expensive. I would also note that this setup would work well for a robotic arm manipulator as well.

If anyone has Karl's 3-d arm files please let me know. that arm would allow me to condense the space to something approximating movie scale.
 

ksj

Blooded
I think I will do a wooden mock up until I can get the backpack hashed out. I will need that at least for a table prototype...

I am trying to figure out how I can do a two DOF servo setup. That setup would allow hiding two servos in the bottom at the base.
 

ksj

Blooded
OK, I have a primitive mockup done for the con this weekend. I only had materials and time to do a basic pan and light function via momentary push button switches.

IMG_1163.JPG


IMG_1164.JPG
 

ksj

Blooded
I think one of my bundles of joy I hosted may have reversed some wires on me frying the low voltage PWM controls on my current setup. I just need to have a sit down session and swap out the trinket pro and see if that fixes the servo control issue I am seeing.
 

ksj

Blooded
OK, sorted some issues out. Apparently the core issue is that the FTDI header Has to be plugge din for it to work on the trinket pro. I switched back over to working with the servo shield and the arduino uno and I managed to rehash the code to make it work much better and more smoothly.

Code:
/***************************************************
  This is an example for our Adafruit 16-channel PWM & Servo driver
  Servo test - this will drive 8 servos, one after the other on the
  first 8 pins of the PCA9685

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/815
 
  These drivers use I2C to communicate, 2 pins are required to 
  interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries. 
  BSD license, all text above must be included in any redistribution
 ****************************************************/
#include "ArduinoNunchuk.h"
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
//Creates the objects to control the servos
ArduinoNunchuk nunchuk = ArduinoNunchuk();

#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  550 // this is the 'maximum' pulse length count (out of 4096)
 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
long lastPulse1;
long lastPulse2;
long lastPulse3;
int xjoystick;
int yjoystick;
int xtilt;
const int servoPin1 = 9;      // Control pin for servo motor
const int servoPin2 = 11;      // Control pin for servo motor
const int servoPin3 = 10;      // Control pin for servo motor
const int ledPin1 = 13;       // Control pin for LED 1
const byte ledPin2 = 12;       // Control pin for LED 2
const unsigned long ledPin2interval = 500;
unsigned long ledPin2timer;
 
const int servoPin4 = 6;      // Control pin for servo motor
 int pulseWidth4 = 0;    // Amount to pulse the servo 4
 long lastPulse4;
int minPulse = 700;   // minimum pulse width
int loop_cnt=0;
int ytilt;
 int refreshTime = 20;  // the time in millisecs needed in between pulses

void setup() {
//    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
   ledPin2timer = millis ();
  Serial.begin(9600);
  Serial.println("8 channel Servo test!");
   Serial.print("loading sound card init\n");
  delay(1000); 
  Serial.print("card initialized");
    //Initializes nunchuck and servos
  nunchuk.init();
  delay(10);
}

void loop() {
 
    checkNunchuck1();
  updateServo1();   
    checkNunchuck2();
  updateServo2();   
    checkNunchuck3();
  updateServo3();
    checkNunchuck4();
  updateServo4();   
  delay(10);

     if(nunchuk.zButton == 1)  {    // light the LED if z button is pressed
    pwm.setPWM(ledPin1, 4096, 0);
//    digitalWrite(firePin1, HIGH);
    delay(300);
    pwm.setPWM(ledPin1, 0, 4096);
//    digitalWrite(firePin1, LOW);
    delay(300);
   }

    if (nunchuk.cButton == 1)  {
    pwm.setPWM(ledPin2, 4096, 0);
    delay(300);   

   }
    else {
      pwm.setPWM(ledPin2, 0, 4096);
    }
 
  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 ) { float tilt = xjoystick; pulseWidth1 = map(xjoystick, 0, 180, SERVOMIN, SERVOMAX); loop_cnt = 0; } loop_cnt++; }
void updateServo1() { if (millis() - lastPulse1 >= refreshTime) { pwm.setPWM(servoPin1, 0, pulseWidth1); lastPulse1 = millis(); } }
void checkNunchuck2() { if( loop_cnt > 100 ) { float tilt = yjoystick; pulseWidth2 = map(yjoystick, 0, 180, SERVOMIN, SERVOMAX); loop_cnt = 0; } loop_cnt++; }
void updateServo2() { if (millis() - lastPulse2 >= refreshTime) { pwm.setPWM(servoPin2, 0, pulseWidth2); lastPulse2 = millis(); } }   
void checkNunchuck3() { if( loop_cnt > 100 ) { float tilt = xtilt; pulseWidth3 = map(xtilt, 0, 180, SERVOMIN, SERVOMAX); loop_cnt = 0; } loop_cnt++; }
void updateServo3() { if (millis() - lastPulse3 >= refreshTime) { pwm.setPWM(servoPin3, 0, pulseWidth3); lastPulse3 = millis(); } }
void checkNunchuck4() { if( loop_cnt > 100 ) { float tilt = ytilt; pulseWidth4 = map(ytilt, 0, 180, SERVOMIN, SERVOMAX); loop_cnt = 0; } loop_cnt++; }
void updateServo4() { if (millis() - lastPulse4 >= refreshTime) { pwm.setPWM(servoPin4, 0, pulseWidth4); lastPulse4 = millis(); } }
 

ksj

Blooded
And the test code I played with that will allow for simple function testing:

Code:
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  550 // this is the 'maximum' pulse length count (out of 4096)

unsigned long ledPin2timer;
const int servoPin4 = 6;
const int ledPin1 = 13;       // Control pin for LED 1
const byte ledPin2 = 12;       // Control pin for LED 2

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  Serial.println("8 channel Servo test!");

  pwm.begin();
 
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  delay(10);
  Serial.println("starting");
  ledPin2timer = millis ();
 
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println("start");
pwm.setPWM(servoPin4, 0, SERVOMIN);
delay(1000);
Serial.println("extend");
pwm.setPWM(servoPin4, 0, SERVOMAX);
delay(1000);
Serial.println("end");
pwm.setPWM(ledPin1, 4096, 0);
delay(500);
pwm.setPWM(ledPin1, 0, 4096);
delay(500);
pwm.setPWM(ledPin2, 4096, 0);
delay(500);
pwm.setPWM(ledPin2, 0, 4096);
delay(500);

}
 

ksj

Blooded
I simplified and condensed the code to make it a little more readable. I will add in more comments once I get the audio piece working with a wave shield. Two add-on stacked shields will really simplify wire management and looks of the setup.

Updated code:
/***************************************************
This is an example for our Adafruit 16-channel PWM & Servo driver
Servo test - this will drive 8 servos, one after the other on the
first 8 pins of the PCA9685

Pick one up today in the adafruit shop!
------> Adafruit 16-Channel 12-bit PWM/Servo Driver - I2C interface

These drivers use I2C to communicate, 2 pins are required to
interface.

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "ArduinoNunchuk.h"
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
//Creates the objects to control the servos
ArduinoNunchuk nunchuk = ArduinoNunchuk();

#define SERVOMIN 150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 550 // this is the 'maximum' pulse length count (out of 4096)
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
long lastPulse1;
long lastPulse2;
long lastPulse3;
int xjoystick;
int yjoystick;
int xtilt;
const int SoundPin1 = 14;
const int SoundPin2 = 150;
const int servoPin1 = 9; // Control pin for servo motor
const int servoPin2 = 11; // Control pin for servo motor
const int servoPin3 = 10; // Control pin for servo motor
const int ledPin1 = 13; // Control pin for LED 1
const byte ledPin2 = 12; // Control pin for LED 2
const int servoPin4 = 6; // Control pin for servo motor
int pulseWidth4 = 0; // Amount to pulse the servo 4
long lastPulse4;
int minPulse = 150; // minimum pulse width
int loop_cnt=0;
int ytilt;
int refreshTime = 20; // the time in millisecs needed in between pulses

void setup() {
Serial.println("PWM Begin"); pwm.begin(); pwm.setPWMFreq(60);
// This moves all servos to minimum positions at start. Good if you do not want overloaded servos
pulseWidth1 = minPulse; pulseWidth2 = minPulse; pulseWidth3 = minPulse; pulseWidth4 = minPulse;
// This initializes the Serial interface functions
Serial.begin(9600); Serial.print("loading sound card init\n"); delay(1000); Serial.print("card initialized");
//Initializes nunchuck and servos
nunchuk.init();
delay(10);
}

void loop() {
// This initializes the servo read and write functions
checkNunchuck1(); updateServo1(); checkNunchuck2(); updateServo2(); checkNunchuck3(); updateServo3(); checkNunchuck4(); updateServo4();
// This checks if buttons are pressed and then turns on two separate LED elements
if(nunchuk.zButton == 1) { pwm.setPWM(ledPin1, 4096, 0); delay(300); pwm.setPWM(ledPin1, 0, 4096); delay(300); }
if(nunchuk.zButton == 1) { pwm.setPWM(SoundPin2, 4096, 0); delay(300); pwm.setPWM(SoundPin2, 0, 4096); delay(300); }
if (nunchuk.cButton == 1) { pwm.setPWM(ledPin2, 4096, 0); delay(300); }
else { pwm.setPWM(ledPin2, 0, 4096); }
// This sets and reads the output from the nunchuck and stores them as floating 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");
}
// These are the functions to check and set the PWM settings for the servos
void checkNunchuck1() { if( loop_cnt > 100 ) { float tilt = xjoystick; pulseWidth1 = map(xjoystick, 0, 180, SERVOMIN, SERVOMAX); loop_cnt = 0; } loop_cnt++; }
void updateServo1() { if (millis() - lastPulse1 >= refreshTime) { pwm.setPWM(servoPin1, 0, pulseWidth1); lastPulse1 = millis(); } }
void checkNunchuck2() { if( loop_cnt > 100 ) { float tilt = yjoystick; pulseWidth2 = map(yjoystick, 0, 180, SERVOMIN, SERVOMAX); loop_cnt = 0; } loop_cnt++; }
void updateServo2() { if (millis() - lastPulse2 >= refreshTime) { pwm.setPWM(servoPin2, 0, pulseWidth2); lastPulse2 = millis(); } }
void checkNunchuck3() { if( loop_cnt > 100 ) { float tilt = xtilt; pulseWidth3 = map(xtilt, 0, 180, SERVOMIN, SERVOMAX); loop_cnt = 0; } loop_cnt++; }
void updateServo3() { if (millis() - lastPulse3 >= refreshTime) { pwm.setPWM(servoPin3, 0, pulseWidth3); lastPulse3 = millis(); } }
void checkNunchuck4() { if( loop_cnt > 100 ) { float tilt = ytilt; pulseWidth4 = map(ytilt, 0, 180, SERVOMIN, SERVOMAX); loop_cnt = 0; } loop_cnt++; }
void updateServo4() { if (millis() - lastPulse4 >= refreshTime) { pwm.setPWM(servoPin4, 0, pulseWidth4); lastPulse4 = millis(); } }
Code:
 

ksj

Blooded
Well, thank you adafruit for always expanding your product offerings.

I redid the wiring on the cannons so I could use an ethernet patch cables on the cannon assemblies.

These products are what I got:
Ethernet RJ45 Female Socket Push-Terminal Block in the cannon
Ethernet RJ45 Male Plug Terminal Block in the backpack housing
Cable Gland - Waterproof RJ-45 / Ethernet connector through coupler for quick disconnect

in cannon with clip down:
pins
1 servo negative
2 servo positive
3 servo signal
7 led positive
8 led negative

In bay backpack housing:
3 led positive
4 led negative
6 servo signal
7 servo positive
8 servo negative

This substantially lessons the wire mess and makes things a lot cleaner and more easily repaired/concealed.

I used a regular ethernet patch cable. If you use a straight through or cross over cable or coupler the pinouts will be different.

For the cable covers I use ribbed cable covers you can buy at auto parts stores.
 
Top