Adafruit 16 channel pwm servo shield control servo with Arduino

Thread Starter

theo99

Joined Sep 19, 2018
2
I've followed this guide to use Adafruit 16 channel pwm servo shield with Arduino Uno controller.

I've uploaded servo example code from Adafruit PWM Servo Driver Library. I'm not quite understand code yet, but it successfully performs example movement, I guess, it is single movement from one side to another for each servo sequentially in time interval loop. It means that everything soldered and connected correctly I guess.

I have 2 motors attached to `0`, `1` of Adafruit 16 channel pwm servo shield, and I'm trying to figure out, how to get value from C# and get it as condition in uploaded code for each separate motor with sending of degree position.

Here is my attempt with servos attached to 0 and 1, edit of `void loop()` of example code. So, what I have tried here in `pwm.setPWM(0, 0, Serial.parseInt());`, first zero is a servo number defined by condition from C# in `val` variable value, and `Serial.parseInt()` degree position which must be also received from C#. Seems like it is wrong way, servo does not moves:

C# trackBar1 for servo on 0:

Code:
 if (myport.IsOpen)
  {
  myport.WriteLine("0");
  myport.WriteLine(trackBar1.Value.ToString());
  }
and uploaded code:

Code:
  #include <Wire.h>
  #include <Adafruit_PWMServoDriver.h>
  Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
  #define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
  #define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)
  int val;
  void setup() {
  Serial.begin(9600);
  Serial.println("8 channel Servo test!");
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
  delay(10);
  }
   
  void setServoPulse(uint8_t n, double pulse) {
  double pulselength;
   
  pulselength = 1000000;  // 1,000,000 us per second
  pulselength /= 60;  // 60 Hz
  Serial.print(pulselength); Serial.println(" us per period");
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit");
  pulse *= 1000000;  // convert to us
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
  }
   
  void loop() {
  val = Serial.parseInt();
  if(val == 0){
  pwm.setPWM(0, 0, Serial.parseInt());
  }
  else if(val == 1){
  pwm.setPWM(1, 0, Serial.parseInt());
  }  
  }
Also I've tried with `val = Serial.println();` and `pwm.setPWM(0, 0, Serial.println());`, serial port becomes busy for uploaded code or control application with debug of one or another, but anyway without checking of serial monitor `myport.WriteLine("0"); myport.WriteLine(trackBar1.Value.ToString());` it does not moves motor, also without result.

What I'm trying to do in code above, is to get equivalent code to my usual control from breadboard as below, value from `C#` control application as condition and position value at the same time for each separate motor:

Code:
if (myport.IsOpen)
  {
  myport.WriteLine("9");  // Pin number as name for servo condition
  myport.WriteLine("20");  // Angle degree position
  myport.WriteLine("11");  // Pin number as name for servo condition
  myport.WriteLine("160"); // Angle degree position
  myport.WriteLine("13");  // Pin number as name for servo condition
  myport.WriteLine("60");  // Angle degree position
  }
Or any other way, separately with trackbar for example:

Code:
 myport.WriteLine("11");
  myport.WriteLine(trackBar2.Value.ToString());
My load in C# , in case of Uno it is COM3, with Due COM4:

Code:
  myport.PortName = comPort;
  myport.BaudRate = 9600;
  myport.Open();
`val` variable takes value from `C#` in uploaded code:

Code:
#include <Servo.h>
  Servo servo1;
  Servo servo2;
  Servo servo3;
  int val;
  void setup() {
  Serial.begin(9600);
  servo1.attach(9);
  servo2.attach(11);
  servo3.attach(13);
  }
  void loop() {
  val = Serial.parseInt();
  if(val == 9){
  servo1.write(Serial.parseInt());
  }
  else if(val == 11){
  servo2.write(Serial.parseInt());
  }
  else if(val == 13){
  servo3.write(Serial.parseInt());
  }
  }
So I'm sending different degree angle position from C# control application at the same time to each separate digital or regular servo. Example code moves both, and I need to use both same way. I have tried change `pwm.setPWMFreq(60);` to `pwm.setPWMFreq(333);` Hz, to control, but this way example code also stops to work

Same scenario as it works with this breadboard code above, I want control servos, attached to Adafruit 16 channel pwm servo shield.

I'm not sure how to use it, need your support

Any advice, guide or example would be helpful
 
Top