0

Updating some old code for Arduino (predator canon setup mad
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: Updating some old code for Arduino (predator canon setup

by knoxvilles_Joker on Sat Apr 20, 2019 3:41 am

knoxvilles_Joker wrote:Now for some reason this will work if I have the FTDI header plugged in, but will not work if that piece is not plugged in.

I am also looking at wiring in some opto isolators to help with some noise issues I am getting with the setup.


OK, I wired in some capacitors with no change. I switched over to the servo shield for the Arduino Uno and it seems to be much more stable now after a complete code rewrite:

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  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(); } }

knoxvilles_Joker
 
Posts: 95
Joined: Wed Mar 01, 2017 9:15 pm

Re: Updating some old code for Arduino (predator canon setup

by knoxvilles_Joker on Sat Apr 20, 2019 3:42 am

And this was the test code that I used for the recode:

Code: Select all | TOGGLE FULL SIZE
#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);

}

knoxvilles_Joker
 
Posts: 95
Joined: Wed Mar 01, 2017 9:15 pm

Re: Updating some old code for Arduino (predator canon setup

by knoxvilles_Joker on Sun Apr 21, 2019 12:48 am

I did some substantial code clean up. I have made it easier to go to each section and hash out what is going on and gave brief descriptions.

Due to everything running I am not able to do the fx soundboard in uart mode. I am going to try a stacked wave shield instead and that should help simplify the mass of wires I am dealing with. I am starting to see why stacked headers are so very, very popular now. The lack of wires that they allow you to not have really simplifies setups and troubleshooting.

I am trying to get things debugged but this is usable right now. I want to come back and add-in all the relevant comments at the top to ensure all proper credit is duely given and the setup is properly noted on what is doing what.

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  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 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(); } }

knoxvilles_Joker
 
Posts: 95
Joined: Wed Mar 01, 2017 9:15 pm

Re: Updating some old code for Arduino (predator canon setup

by knoxvilles_Joker on Mon Apr 22, 2019 11:55 am

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:
https://www.adafruit.com/product/2912 in the cannon
https://www.adafruit.com/product/2913 in the backpack housing
https://www.adafruit.com/product/827 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.

knoxvilles_Joker
 
Posts: 95
Joined: Wed Mar 01, 2017 9:15 pm

Re: Updating some old code for Arduino (predator canon setup

by knoxvilles_Joker on Sat May 04, 2019 2:02 am

OK, I got it to work with the mp3 music maker shield. I can get it to play a sound at startup, but it will freeze if I try to initialize a sound play when triggered from one of the wii buttons.

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  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
 
 * 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 "ArduinoNunchuk.h"
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
//end servo shield declarations

// 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.begin(9600);
 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.print("loading sound card init\n"); delay(1000); Serial.print("card initialized"); nunchuk.init(); //Initializes nunchuck and servos
  Serial.println("Adafruit VS1053 Simple Test"); if (! musicPlayer.begin()) { Serial.println(F("Couldn't find VS1053, do you have the right pins defined?")); while (1); }
  Serial.println(F("VS1053 found")); if (!SD.begin(CARDCS)) { Serial.println(F("SD failed, or not present")); while (1); }
  musicPlayer.setVolume(20,20); musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ smf vol int
  Serial.println(F("Playing POWER ON")); musicPlayer.playFullFile("/T00NEXT0.WAV"); delay(10); // Play one file, don't return until complete
}

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);  delay(300); }
    if (nunchuk.cButton == 1)  { pwm.setPWM(ledPin2, 4096, 0); delay(300);  } // musicPlayer.playFullFile("/T03NEXT1.WAV")
    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(); } }

knoxvilles_Joker
 
Posts: 95
Joined: Wed Mar 01, 2017 9:15 pm

Re: Updating some old code for Arduino (predator canon setup

by knoxvilles_Joker on Sat May 04, 2019 9:14 pm

More substantial code cleanup done. Finally got sound initialization to work with minimally added code. Had to add delays to allow the full file to play. Switched to using the interrupt music maker example sketch as the base. separated out button presses as separate void functions and added in loop firing trigger sound with in the run loop.

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  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
 * 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/
 * 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
 * Based on the following resources:
 *   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 "ArduinoNunchuk.h"
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
//end servo shield declarations

// 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 = 2;       // Control pin for LED 1
const byte ledPin2 = 12;       // Control pin for LED 2
const int servoPin4 = 5;      // 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.begin(9600);
 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; nunchuk.init (); delay(1000);     
// This initializes the Serial interface functions 
  Serial.println("Adafruit VS1053 Library Test"); musicPlayer.begin();
  SD.begin(CARDCS); printDirectory(SD.open("/"), 0); musicPlayer.setVolume(20,20);
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);
  delay(500); musicPlayer.startPlayingFile("/t00next0.wav"); delay(1500); //PLAYS INIT SOUND
}

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) { musicPlayer.startPlayingFile("/T03NEXT2.WAV"); delay(1900); }
    checkzbutton(); checkcbutton();   
// 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");
    }
// button
void checkzbutton() { if(nunchuk.zButton == 1)  { pwm.setPWM(ledPin1, 4096, 0); delay(300); pwm.setPWM(ledPin1, 0, 4096); delay(300);  delay(300); }}
void checkcbutton() { if(nunchuk.cButton == 1)  { pwm.setPWM(ledPin2, 4096, 0); delay(3000);}  else {pwm.setPWM(ledPin2, 0, 4096); delay(300);  }}
// File listing helper
void printDirectory(File dir, int numTabs) { while(true) { File entry =  dir.openNextFile(); if (! entry) { break; } for (uint8_t i=0; i<numTabs; i++) { Serial.print('\t'); } Serial.print(entry.name()); if (entry.isDirectory()) { Serial.println("/"); printDirectory(entry, numTabs+1); } else { Serial.print("\t\t"); Serial.println(entry.size(), DEC); } entry.close(); } }
// 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(); } }

knoxvilles_Joker
 
Posts: 95
Joined: Wed Mar 01, 2017 9:15 pm

Re: Updating some old code for Arduino (predator canon setup

by knoxvilles_Joker on Sun Jun 23, 2019 4:03 am

More code updates done. I dialed back PWM for the hs-785hb servo (it rotates about 400 some degrees with a full signal). I shortened the millis delay down to 10 which appears to be the max the Arduino uno can handle with everything going on. I have a metro m4 on order to see if I can lower that delay down even more.

Stan Winston school has some classes on making the predator costume so I might take a dive and spend 400 for a yearly membership if things keep progressing the way that they are. (https://www.stanwinstonschool.com/blog/ ... ator-suits)

Code: Select all | TOGGLE FULL SIZE
C-like:
/***************************************************
  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
 * 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/
 * 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
 * Based on the following resources:
 *   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 "ArduinoNunchuk.h"
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
//end servo shield declarations

// 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  530 // 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 = 4;      // Control pin for servo motor
const int servoPin3 = 10;      // Control pin for servo motor
const int ledPin1 = 2;       // Control pin for LED 1
const byte ledPin2 = 12;       // Control pin for LED 2
const int servoPin4 = 5;      // 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 = 3;  // the time in millisecs needed in between pulses

void setup() {
  Serial.begin(9600);
 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; nunchuk.init (); delay(1000);     
// This initializes the Serial interface functions
  Serial.println("Adafruit VS1053 Library Test"); musicPlayer.begin();
  SD.begin(CARDCS);
//  printDirectory(SD.open("/"), 0);
  musicPlayer.setVolume(20,20);
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);
  delay(500); musicPlayer.startPlayingFile("/t00next0.wav"); delay(1500); //PLAYS INIT SOUND
}

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) { musicPlayer.startPlayingFile("/T03NEXT2.WAV"); delay(1900); }
    checkzbutton(); checkcbutton();   
// 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");
    }
// button
void checkzbutton() { if(nunchuk.zButton == 1)  { pwm.setPWM(ledPin1, 4096, 0); delay(300); pwm.setPWM(ledPin1, 0, 4096); delay(300);  delay(300); }}
void checkcbutton() { if(nunchuk.cButton == 1)  { pwm.setPWM(ledPin2, 4096, 0); delay(3000);}  else {pwm.setPWM(ledPin2, 0, 4096); delay(300);  }}
// File listing helper
//void printDirectory(File dir, int numTabs) { while(true) { File entry =  dir.openNextFile(); if (! entry) { break; } for (uint8_t i=0; i<numTabs; i++) { Serial.print('\t'); } Serial.print(entry.name()); if (entry.isDirectory()) { Serial.println("/"); printDirectory(entry, numTabs+1); } else { Serial.print("\t\t"); Serial.println(entry.size(), DEC); } entry.close(); } }
// These are the functions to check and set the PWM settings for the servos   
void checkNunchuck1() { if( loop_cnt > 10 ) { float tilt = xjoystick; pulseWidth1 = map(xjoystick, 0, 180, SERVOMIN, 265); loop_cnt = 0; } loop_cnt++; }
void updateServo1() { if (millis() - lastPulse1 >= refreshTime) { pwm.setPWM(servoPin1, 0, pulseWidth1); lastPulse1 = millis(); }
//if (pulseWidth1 <= 170) {pwm.setPWM(14, 0, 130);}
if (pulseWidth1 >= 200) {pwm.setPWM(14, 0, 530);} else {pwm.setPWM(14, 0, 230);}
}

void checkNunchuck2() { if( loop_cnt > 10 ) { 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 > 10 ) { 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 > 10 ) { 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(); } }

knoxvilles_Joker
 
Posts: 95
Joined: Wed Mar 01, 2017 9:15 pm

Re: Updating some old code for Arduino (predator canon setup

by knoxvilles_Joker on Fri Jul 26, 2019 7:27 pm

OK, here is a physical overview and detailing on the build:
https://youtu.be/42oK4IWrk5g
-note I will have to redo as the AI on youtube flagged something.

knoxvilles_Joker
 
Posts: 95
Joined: Wed Mar 01, 2017 9:15 pm

Please be positive and constructive with your questions and comments.