| 
 | 
 
 本帖最後由 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; 
 
} 
} 
} |   
 
 
 
 |