📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Attention all domestic customers! Please place UPS Ground and USPS First Class & Priority orders by 11am ET Monday December 9, 2019 📬❄️📦 Attention all international customers! Please place all UPS WORLDWIDE EXPRESS; SAVER, and DHL EXPRESS WORLDWIDE orders by 11am ET Monday December 9, 2019 📬❄️📦
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.

Updating some old code for Arduino (predator canon setup mad

by knoxvilles_Joker on Sun Oct 08, 2017 10:56 pm

OK, I have purchased a12-bit PWM + servo shield for the Arduino uno. I have three servos and a wii nunchuck I want to use for PTZ functionality

I have purchased the servos from servocity.com but they do work with the pwmtest.

I have verified the nunchuck is communicating with the wiichuck test library and I am getting updated streams with it.

https://www.adafruit.com/product/345
https://www.adafruit.com/product/342
https://www.adafruit.com/product/1411

The codebase I am trying to play with:
https://www.instructables.com/id/Arduin ... ostumes-m/
(I have reached out to the author and have not yet gotten a response for some collaboration.)

The problem I am running into is I am not seeing any documentation using the two that is current with the WiiChuck libraries that works.

The WiiChuck library addition seems recent and several of the sites I have tried mostly use direct serial communication or a custom and earlier iteration of the wiichuck library. As far as I can find most of the write-ups are at least a year or three or more old.

Are there any sites or documentation examples out there I can use to get a Pan and tilt setup working with two servos and a third power on/switching servo that raises the cannon arm. For now I will settle with getting two servos working and worry about the rest later.


I want to get this ready soon in preparation for the predator movie coming out next year as I think we will have a predator movie cast reunion in town and I want to showcase the build at the show. I will also be using the build for a fascimile tracking smart-gun turret for my groups booth using this same or similar setup.

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

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

by knoxvilles_Joker on Thu Oct 12, 2017 7:13 pm

Without the shield I can get the servos to work.

I ran an I2c code scanner
and with the nunchuck I get:
I2C device found at address 0x40 !
I2C device found at address 0x52 !
I2C device found at address 0x70 !
done

Is 40 the shield, and what is 70?
52 is the nunchuck.

I found that this code worked somewhat without the shield

https://blog.underc0de.org/arduino-wii- ... o-motores/

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

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

by knoxvilles_Joker on Thu Oct 12, 2017 7:18 pm

And in the code what is the syntax to drive a single set servo pin set?

void loop() {
// Drive each PWM in a 'wave'
for (uint16_t i=0; i<4096; i += 8) {
#ifdef ESP8266
yield();
#endif
for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
pwm.setPWM(pwmnum, 0, (i + (4096/16)*pwmnum) % 4096 );
}
}

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

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

by knoxvilles_Joker on Wed Oct 25, 2017 6:47 am

ok, I had some toasted servos (accident on my part). They work, just have to work on some line noise through some power filtering.

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

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

by knoxvilles_Joker on Wed Oct 25, 2017 6:58 am

This is the code I managed to get working. My hats off to under code for making a killer code piece that writes a very useful stream of text to the serial monitor.

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


Code: Select all | TOGGLE FULL SIZE
// Arduino Center//
// 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
 
//Include the needed libraries.
#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 servo3;
Servo servo5;
Servo servo9;
 
//Initializes the variables
int xjoystick;
int yjoystick;
int xtilt;
int ytilt;
//int controlPin1 = 6;
//int pos9 = 85;

//const int leftpin6 = 6;
//int leftpinpressed = 0;
// sets push button input
//int buttonPin = 4;

// from honus 2007 setup
int ledPin1 = 11;  // control pin for LED
int ledPin2 = 12;  // control pin for laser sight
// for push button
//int val; // variable for reading the pin status
//int buttonState; // variable to hold the last button state
 
void setup() {
  //Initializes the serial servo monitor
  Serial.begin(9600);
  ss.begin(9600);

  delay(800); 
  //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");
  Serial.print("Initialized serial connections\n");
 
  //Initializes nunchuck ans servos
  nunchuk.init();
  servo3.attach(3);
  servo5.attach(5);
  servo9.attach(9);
//  pinMode(controlPin1, INPUT);
//  pinMode(buttonPin, INPUT);  // set the button pin as input
// from honus setup sets pins 11, 12 as led output pins   
  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);
// for push button 
//  buttonState = digitalRead(buttonPin);

}
 
void loop() {
  // This turns on the head laser while device is on.
  digitalWrite(ledPin2, HIGH); // sets the LED pin HIGH (turns it on)
  // this takes the arm servo and raises it
  //servo9.write(10); //resets cannon arm
//  servo9.write(5);
//  delay(50);
//  servo9.write(170); //raises cannon arm
//  val = digitalRead(buttonPin);
 
  //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");
// this takes the joystick and moves servos without button presses 
//   this method is too draining on the battery
//    servo3.write(xjoystick);
//    servo5.write(yjoystick);
//    delay(15);

// does not work well
//  // read input value and store it in val
//  if (val != buttonState) {  // the button state has changed!
//    if (val == LOW) {  // check if the button is pressed
//      Serial.println("button pressed");   
//      Serial.print ("button pressed"); 
//        if (servo9.read() == 170) {
//          servo9.write(10);
//          }           
//          }
//          }
// These are the while loops. 
// They are what send commands to lights and

 
  //This moves the servos while button z is pressed
  //With the joystick
  //once let go it remembers the position until next press
  while (nunchuk.zButton == 1) {
//    servo3.write(xtilt);
//    servo5.write(ytilt);
    digitalWrite(ledPin1, HIGH);
    Serial.print("#2\n");
    delay(500);
    digitalWrite(ledPin1, LOW);   
// prints sound stored on pin2 named T02.ogg or T02.wav
    break;
  }
  while (nunchuk.cButton == 1 && nunchuk.zButton == 1) {
    digitalWrite(ledPin1, HIGH);
    Serial.print("#1\n");
    delay(500);
    digitalWrite(ledPin1, LOW);   
// prints sound stored on pin3 named T03.ogg or T03.wav
// splat sound
  break;
  }
 
  //This moves the servos while button c is pressed
  //With the joystick
  //once let go it remembers the position until next press
  while (nunchuk.cButton == 1) {
//    servo9.write(170);
    servo3.write(xjoystick);
    servo5.write(yjoystick);
//    digitalWrite(ledPin1, HIGH);
//    Serial.print("#3\n");
//    delay(500);
//    digitalWrite(ledPin1, LOW); 

      break;
  }

//  this method was too draining on battery
//  while (nunchuk.cButton == 0 && nunchuk.zButton == 0) {
//    servo3.write(xjoystick);
//    servo5.write(yjoystick);
//    break;
//  }

 
  delay(15);
}

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

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

by knoxvilles_Joker on Sun Oct 29, 2017 2:22 pm

OK, I managed to get things working. I just need to buy another fx sound board and amp and speaker to verify all the code pieces are 100% operational. I will update the code in more detail more fully later.

I will add another post for the code pieces I used to test the servos.

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


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

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

by knoxvilles_Joker on Sun Oct 29, 2017 2:27 pm

To scan your I2C bus load this sketch. This will check to make sure you have the wii nunchuck wired properly

Code: Select all | TOGGLE FULL SIZE
 // --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }   
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}



This was off of arduino.cc. This tests the travel of your servo. The pauses are required for there to be time for movement to finish



Code: Select all | TOGGLE FULL SIZE
#include <Servo.h>  //import the servo library

Servo coreservo; //Name the Servo

void setup() {
  coreservo.attach(10);
  /*Attach the named servo object to Digital IO 13, use following syntax:
  servoname.attach(Pin#, minimum Pulse width (ms), maximum pulse width (ms));
  if you want to define the pulse widths for your motor*/

}

void loop() {
  coreservo.write(0);
  delay(900);
  coreservo.write(90);
  delay(900);
  coreservo.write(180);
  delay(900);
  /* If you wanted to read the angle of your servo at any given time, use servoname.read();
   * If you wanted to write a pulse of a certain width use servoname.writemicroseconds(value in microseconds);
   */
}


This is an actual PWM servo test. This will allow you to calibrate your code if you are using a servo that does not adhere to industry standards. Common problem with Chinese knock offs and cheap imitations. One of the reasons why I would rather buy off of adafruit rather than save a few dollars.

Code: Select all | TOGGLE FULL SIZE
//https://www.arduino.cc/en/Reference/ServoWriteMicroseconds
// courtesy of above range is 700 to 2300.  1000 is zero,
// 1500 is 90 and 2000 is 180 degrees typically.
// The standard is not defacto or dejure as of yet so
// your results will vary.
// La Saluta Non Si Paga as my late step-father would say.
// You get what you pay for.

#include <Servo.h>

Servo myservo;

void setup()
{
  myservo.attach(9);
//  myservo.writeMicroseconds(2000); // set servo to mid-point
  delay (150);
//  myservo.writeMicroseconds(700);

}


void loop() {
  myservo.writeMicroseconds(2300); // set servo to mid-point
  delay (1000);
  myservo.writeMicroseconds(700);
  delay (1000);
  myservo.writeMicroseconds(1500);
  delay (1000);
  }

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

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

by knoxvilles_Joker on Mon Oct 30, 2017 7:11 am

If anyone has a good site for a 4 servo pan tilt assembly on a single arm I am all ears. It would pan at bottom and top and tilt at the bottom and top.

All the original makers are unavailable and I can not source the 3-d models used to make the arm by Carl J Toti.

At this point I am looking at having to use a robot claw pan-tilt module on both ends to accomplish the same but the complaint will be the arm will not look the same...

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

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

by knoxvilles_Joker on Tue Nov 07, 2017 6:43 am

This updated code works. I just need to wire in some transistors to help with some of the servos causing excessive draw on my power supply.

I would advise that there is not a built-in or available wii nunchuck library available as it stands. This will change in a few years I suspect as in 2007 everyone just incorporated the communication and control functions in to the main script. In the linux world you want to separate main functions into separate libraries so that it is easier to update and maintain the code base when you run into issues or hardware updates.

I will work to add some PWM dimming functions for the LEDs. All the pins are more or less in use. Sound playback was fun. I only managed to get a couple of sound pins to work. I think it has to do with the fact I went with the mini versus the full board (less sound pins.) Great board regardless. The more compact design actually halves the PCB footprint by about 50% versus the original design when used with a trinket pro 5v.

Code: Select all | TOGGLE FULL SIZE
/*
 * 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
 *
 */
// 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 = 5;      // Control pin for servo motor
 int servoPin4 = 6;      // Control pin for servo motor
// sound pins for firing sounds
const int firePin1 = 3;
const int firePin2 = 4;
const int firePin3 = 7;
const int startSoundPin = 11;

 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, INPUT);
  pinMode(firePin1, INPUT);
  pinMode(firePin2, INPUT);
  pinMode(firePin3, INPUT);
  // set up the audio trigger pins to give a path to GND when set to OUTPUT
  digitalWrite(startSoundPin, LOW);
  digitalWrite(firePin1, LOW);
  digitalWrite(firePin2, LOW);
  digitalWrite(firePin3, LOW);
 


// servoPin1.attach(9);
// servoPin2.attach(8);
// servoPin3.attach(5);
// 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);

Serial.begin(9600);
  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("#00\n");
  delay(200);
    //Initializes nunchuck ans servos
  nunchuk.init();

}

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("#3\n");
    //  3 = 1
    delay(100);
   }
  else {
    digitalWrite(ledPin1,LOW);

  }
   
     if(nunchuk.cButton == 1) {     // light the LED if c button is pressed
    digitalWrite(ledPin2, HIGH);
    Serial.print("#4\n");
    // 4 = 4
    delay(100);
   
     }
  else {
    digitalWrite(ledPin2,LOW);
  }

   if(nunchuk.cButton == 1 && nunchuk.zButton == 1) {
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin1, HIGH);
      Serial.print("#7\n");
//      serial interfacing was making too much noise for a dual output deal
      delay(300);
       }
  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
  }
}


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

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

by knoxvilles_Joker on Tue Nov 07, 2017 6:54 am

I will work on the servo shield next as I think it is a better platform for PWM control as the transistors and controls are built-in. Couple that with a wave shield and you have a nice stack.

I had some ideas on how to test the servo control with it and will do that once I get the transistor board built and heat issues addressed. I will start a separate thread on that.

I hope that this thread helps the community and others as a whole for others trying to do similar things.

I would also advise that this setup would be a perfect build for an animatronic parrot on a pirate costume. saying things like ::sqwak:: walk the plank. and other fun phrases.

Again I love the site and Adafruit products.

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

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

by knoxvilles_Joker on Tue Nov 07, 2017 8:27 pm

OK as it stands with the bare bones setup I have, the moment I invoke UART serial control I get all sorts of noise from the servos and the all move slightly nonstop.

Anyone have suggestions on how to deal with the noise situation.

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

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

by knoxvilles_Joker on Thu Nov 09, 2017 8:10 pm

Doing things in GPIO mode fixes the noise issues. Apparently serial UART with PWM servos conflict with each other due to noise...

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

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

by knoxvilles_Joker on Sat Nov 11, 2017 11:27 am

Final code that I am calling done for now:

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


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

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

by knoxvilles_Joker on Sat Nov 11, 2017 3:48 pm


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

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

by knoxvilles_Joker on Wed Apr 10, 2019 11:50 am

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.

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

Please be positive and constructive with your questions and comments.