Servos resetting each time button pressed on bluetooth controller

Hello,

I am attempting to build one of nerf turret designs seen on Youtube and am having a problem with my servos resetting each time I press the “trigger” button on my Dabble joystick controller. Both the trigger and the joystick/servos work just fine when used independently, but if I am using the joystick, then pressing the “X” button on the controller resets the servo resets and returns it to the starting position (even as I still holding the position constant on the joystick). The opposite is also true: If I am holding down the button for continuous darts, moving the joystick will stop the nerf gun from firing.

If it makes any difference, I do have a drivers for stepper motor (trigger) and the 2 servos. Servos are run from an independent power supply (5V, 2.4A). Below is my code, please feel free to also provide feedback on my post itself as well (this is my 2nd post).

#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#include <Wire.h>


// Depending on your servo make, the pulse width min and max may vary, you 
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN  150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // This is the 'maximum' pulse length count (out of 4096)
#define SERVO_FREQ 60 // Analog servos run at ~50 Hz updates



// define stepper pins
#define stepPin 4
#define dirPin 5

// define pin for nerf feeder motor
#define nerfMotorPin 7

// define value for stepper motor steps per revolution (1/16=3200, 1/8=1600, 1/4=800)
#define stepsPerRevolution 3200

// define variables for measuring joytsick position

int joyVarX;
int joyVarY;


/*
   Gamepad module provides three different mode namely Digital, JoyStick and Accerleometer.

   You can reduce the size of library compiled by enabling only those modules that you want to
   use. For this first define CUSTOM_SETTINGS followed by defining INCLUDE_modulename.

   Explore more on: https://thestempedia.com/docs/dabble/game-pad-module/
*/
#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_MODULE
#include <Dabble.h>



void setup() {
  // put your setup code here, to run once:
  Serial.begin(250000);      // make sure your Serial Monitor is also set at this baud rate.
  Dabble.begin(9600);      //Enter baudrate of your bluetooth.Connect bluetooth on Bluetooth port present on evive.
  
  
  // set mode for stepper motor pins
  pinMode (stepPin, OUTPUT);
  pinMode (dirPin, OUTPUT);

  // set mode for Nerf pin 
  pinMode (nerfMotorPin, OUTPUT); 

// start pwm and set frequency
  pwm.begin ();
  pwm.setPWMFreq(SERVO_FREQ);  // Analog servos run at ~50 Hz updates

   yield (); // stops until everything in setup is run, then resumes

}



void barrage () {
    
     digitalWrite (nerfMotorPin, HIGH); // turn on nerf feeder motor
     delay (1300);

     for (int i = 0; i < stepsPerRevolution; i++){  // 1 full revolution before checking if button is pressed for "homimg"
      digitalWrite (stepPin, HIGH);
      delayMicroseconds (200);
      digitalWrite (stepPin, LOW);
      delayMicroseconds (200);
     }
     digitalWrite (nerfMotorPin, LOW); // Nerf motor off 
}


void loop() {

    Dabble.processInput();   //this function is used to refresh data obtained from smartphone.Hence calling this function is mandatory in order to get data properly from your mobile.

 // Moving Servo 1

       joyVarX = GamePad.getXaxisData();
       
       joyVarX = map (joyVarX, -7, 7, 550, 100);  // mapping joytstick position values to servo position values
      
       pwm.setPWM (0, 0, joyVarX);
       delay (20);


      // Moving Servo 2
       joyVarY = GamePad.getYaxisData();

       
       joyVarY = map (joyVarY, -7, 7, 320, 180);  // mapping joytstick position values to servo position values
   
       pwm.setPWM (4, 0, joyVarY);
       delay (20); 

    
  // Buttons
  if (GamePad.isSquarePressed())
    {
      barrage ();
    }
  
  if (GamePad.isCirclePressed())
    {
      barrage ();
    }
  
  if (GamePad.isCrossPressed())
    {
      barrage ();
    }
  
  if (GamePad.isTrianglePressed())
    {
      barrage ();
    }
  
  if (GamePad.isStartPressed())
  {
    Serial.print("Start");
  }

  if (GamePad.isSelectPressed())
  {
    Serial.print("Select");
  }
  Serial.print('\t');

}

put some Serial.print() in the setup() to check if your arduino is rebooting

Thanks for the feedback. I added a serial.print to the setup, and it does not appear that my Arduino is restarting when the servos restart (the only output for the serial.print was at startup and when I pressed the reset button, with no output when using joystick and servos).

what's your arduino ? how is your Adafruit 16-Channel 12-bit PWM/Servo wired ?

can you provide a full description of your setup?

Okay, I'll give it a shot...

PCA9685 16 channel 12 bit servo driver

  • independent power supply (5V, 2.4A) - connected to V+ and GND screw connectors
  • servo 0 and 4 connected directly to 2 MG996Rs
  • VCC and GND to Arduino 5V and GND
  • SCL and SCA to Arduino A4 and A5

TB6560 3A stepper motor driver

  • independent power supply(12V) - connected directly to + and GND on driver connector
  • As and Bs connected to stepper motor
  • CLK+ and CW+ to digital pins on Arduino 4 and 5, CLK- and CW- to Arduino ground

Bluetooth

  • Power connected to Arduino 3.3V and GND
  • transmit and receive to Arduino pins 2 and 3

What’s on pin 7?

 // define pin for nerf feeder motor
#define nerfMotorPin 7

What’s your BT adaptor?

I assume arduino is powered through USB?

Pin 7 goes to a basic 3v relay that kicks on the motor for the nerf gun (powered with 9v battery pack), while the stepper with a diy piston pulls the trigger.

Bluetooth is a HM-10

Arduino is powered through usb to computer while programming, and with a simple 9v when not. I’ve run my code with both and same results.

So one more relay.. frustrating to extract information to get the full picture...

Can you add up the current draw on your Arduino when it’s all activated ?

a simple 9v

if it's a simple rectangle shape 9V battery, it's not meant to deliver high current or answer peak demand.

switching on/off coils can create voltage-spikes. Why do you use a 3V relais to switch on/off the Power of the steppermotor?
The TB6560 Steppermotordriver has an enable/disable-input which can do that.

best regards Stefan
any newbee can apply the most professional habit from the first line of code they write on their own:
add only ONE thing at a time. Test/debug that ONE thing until that ONE thing works reliable - repeat.
The sad thing is: only the REAL professionals write code this way.

That is helpful feedback, I’ll try be more thorough with details in future posts.

Using a multimeter (between battery and arduino) the current draw is 54 mA with Arduino only, 64 with Bluetooth and Arduino, and 144 mA total when the relay is switched on. Use of the other components, servos or stepper motor, results in no change in current.
So...
Arduino - 54mA
Bluetooth - 10 mA
Relay - 80mA

The current draw for each one of my components (listed in data sheet) is:

  • servos — 2.5A x 2 = 5A
  • Stepper motor — 1.7A

StephanL38

Thanks for the great tip! I'll change my setup accordingly, removing the unnecessary relay.

Software Serial is not doing too well when there is lots of data coming.

Can you print the values you get from the Phone through BT ?

Why do you need the delay (20); in the code ?

It would help clarify if there are any request to move the servos to the wrong position

Another test would be to print the values and actions but disconnect the motors

If you never get a wrong value then tracking down a power or driver issue is probably the right path