Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 2971|回復: 1
打印 上一主題 下一主題

PCA9685 16路12-bit PWM控制板 的程式問題

[複製鏈接]
跳轉到指定樓層
1#
發表於 2016-9-18 11:09:23 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最後由 Charlotte 於 2016-9-20 15:24 編輯

這是我的控制板,裡面是用Adafruit_PWMServoDriver.h 資料庫

範例:
#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);

// 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)

// our servo # counter
uint8_t servonum = 0;

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

#ifdef ESP8266
  Wire.pins(2, 14);   // ESP8266 can use any two pins, such as SDA to #2 and SCL to #14
#endif

  pwm.begin();

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

  yield();
}

// you can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
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 *= 1000;
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
}

void loop() {
  // Drive each servo one at a time
  Serial.println(servonum);
  for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
    pwm.setPWM(servonum, 0, pulselen);
  }

  delay(500);
  for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
    pwm.setPWM(servonum, 0, pulselen);
  }

  delay(500);

  servonum ++;
  if (servonum > 7) servonum = 0;
}


Q:
我想要使用上面的控制板達到下面程式的效果,請問程式要如何打?

#include <SoftwareSerial.h>
#include <Servo.h>

SoftwareSerial BT(11,10); //分別接藍芽模組的TX, RX
Servo servoX;


void setup() {
Serial.begin(9600); //Arduino起始鮑率:9600
BT.begin(115200);

servoX.attach(8);
servoX.write(90);

}

void loop() {
  int angleOffset,ServoXAngle;
ServoXAngle = servoX.read();    ←這一行程式不知道怎麼在控制板上面呈現
// 檢查是否有資料可供讀取
   if (BT.available() > 0) {
     // 讀取進來的 byte
     int inByte =BT.read();
     Serial.println(inByte);
     switch(inByte){



  case 'U':

    if(inByte >0) {
        angleOffset = 2;

      ServoXAngle+= angleOffset; //計算新的馬達角度

      // 如果新的馬達角度在最小角度和最大角度之間,就改變馬達的角度
      if(ServoXAngle > 29 && ServoXAngle < 151) {
       servoX.write(ServoXAngle);
        }
        }
    break;

}
}
}
2#
發表於 2016-9-19 17:14:13 | 只看該作者
其實程式改一下, 就可以省掉
ServoXAngle = servoX.read();  這一行

如下
int ServoXAngle=90;
void setup() {
................
}
void loop() {
  int angleOffset;
// 檢查是否有資料可供讀取
   if (BT.available() > 0) {
// 讀取進來的 byte
     int inByte =BT.read();
.......................
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-4-29 21:09 , Processed in 0.266847 second(s), 7 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表