0

16 Channel PWM/Servo Shield P_S_3 Controller
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

16 Channel PWM/Servo Shield P_S_3 Controller

by hiabcrane on Sun Sep 01, 2019 5:06 pm

Hi!
I need your help to combine the 16 Channel Servoshield with the USB Hostshield with Bluetooth Dongle on my Nano. I already picked parts from different codes, but I dont know anything about writing sketches. I just want you to help me get one servo started with the right hat of a P_S_3 controller.
This is the best I could find so far:

Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <P_S_3BT.h>                                                    //Include the necessary libraries.

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

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// you can also call it with a different address and I2C interface
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40, &Wire);

// 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 SERVOMIN1  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX1  600 // this is the 'maximum' pulse length count (out of 4096)

// servo and pot constants
const int servo1 = 0;     //servo1 position

// variables
int val;
int pulseLength;

USB Usb;
BTD Btd(&Usb);
P_S_3BT P_S_3(&Btd);

void setup() {
  Serial.begin(115200);

  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nP_S_3 Bluetooth Library Started"));

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

  yield();
}

void loop()
{
  Usb.Task();

  if (P_S_3.P_S_3Connected || P_S_3.P_S_3NavigationConnected) {
 
    Serial.println(map(P_S_3.getAnalogHat(RightHatX), 0, 255, 0, 600));
  }
  if (P_S_3.getButtonClick(UP)) {
    digitalWrite(LED1, HIGH);

    Serial.print(F("\r\nUp"));

     if (P_S_3.P_S_3Connected) {
      P_S_3.setLedOff();
      P_S_3.setLedOn(LED1);

    }
  }


 {
 
 
  val = analogRead(RightHatX);           
  pulseLength = map(val, 0, 255, SERVOMIN1, SERVOMAX1);     
  pwm.setPWM(servo1, 0, pulseLength);
  delay(500);
   
  }
 }   


Thank you

Couldnt write P_S_3 together, so just ignore the underscore also in the code

hiabcrane
 
Posts: 2
Joined: Sun Sep 01, 2019 4:46 pm

Re: 16 Channel PWM/Servo Shield P_S_3 Controller

by hiabcrane on Sun Sep 15, 2019 7:19 am

Finally I got it. Even though its easy for the most of you to change such code, I will post this code for beginners like me.

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

#include <SPI.h>
#include <P_S_3BT.h>
#include <Servo.h>

USB Usb;
BTD Btd(&Usb);
P_S_3BT P_S_3(&Btd);

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// 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 SERVOMIN1  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX1  600 // this is the 'maximum' pulse length count (out of 4096)

// servo and pot constants
const int servo1 = 0;     //servo1 position

// variables
int val;
int pulseLength;

void setup() {
  Serial.begin(115200);
  Serial.println("16 channel Servo test!");


if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nP_S_3 Bluetooth Library Started"));


  pwm.begin();

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

  yield();
}



void loop()

{
  Usb.Task();

  if (P_S_3.P_S_3Connected || P_S_3.P_S_3NavigationConnected)
{
  val = (P_S_3.getAnalogHat(RightHatX));           
  pulseLength = map(val, 0, 255, SERVOMIN1, SERVOMAX1);   
  pwm.setPWM(servo1, 0, pulseLength);
  delay(5);


if (P_S_3.getButtonClick(UP)) {
    digitalWrite(LED1, HIGH);

    Serial.print(F("\r\nUp"));

     if (P_S_3.P_S_3Connected) {
      P_S_3.setLedOff();
      P_S_3.setLedOn(LED1);
}
}
}
}

hiabcrane
 
Posts: 2
Joined: Sun Sep 01, 2019 4:46 pm

Please be positive and constructive with your questions and comments.