Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 2890|回復: 1

Arduino 手臂 藍芽控制 伺服馬達 干擾問題

[複製鏈接]
發表於 2018-5-12 18:33:43 | 顯示全部樓層 |閱讀模式
我用的是 3顆 MG995 3顆MG90S 組成的機械手臂

但每次控制其他顆馬達的時候

就有一顆MG90S會劇烈的轉動

不知道是甚麼問題

下面是我的程式 請高手幫我解決此問題!!感謝


#include <SoftwareSerial.h> // TX RX software library for bluetooth

#include <Servo.h> // servo library
Servo myservo1, myservo2, myservo3, myservo4, myservo5, myservo6; // servo name

int bluetoothTx = 4; // bluetooth tx to 4 pin
int bluetoothRx = 7; // bluetooth rx to 7 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  myservo1.attach(3); // attach servo signal wire to pin 9
  myservo2.attach(5);
  myservo3.attach(6);
  myservo4.attach(A0);
  myservo5.attach(10);
  myservo6.attach(11);
  //Setup usb serial connection to computer
  myservo1.write(90);
  myservo2.write(22.5);
  myservo3.write(180);
  myservo4.write(90);
  myservo5.write(90);
  myservo6.write(45);
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()>= 2 )
  {
    unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();
    unsigned int realservo = (servopos1 *256) + servopos;
    Serial.println(realservo);

  if (realservo >= 1000 && realservo <1180) {
      int servo1 = realservo;
      servo1 = map(servo1, 1000, 1180, 0, 180);
      myservo1.write(servo1);
      Serial.println("Servo 1 ON");
      delay(10);
    }
  if (realservo >= 2000 && realservo <2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      Serial.println("Servo 2 ON");
      delay(10);
    }
  if (realservo >= 3000 && realservo <3180) {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 90, 180);
      myservo3.write(servo3);
      Serial.println("Servo 3 ON");
      delay(10);
    }
  if (realservo >= 4000 && realservo <4180) {
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180, 45, 180);
      myservo4.write(servo4);
      Serial.println("Servo 4 ON");
      delay(10);
    }
  if (realservo >= 5000 && realservo <5180) {
      int servo5 = realservo;
      servo5 = map(servo5, 5000, 5180, 45, 135);
      myservo5.write(servo5);
      Serial.println("Servo 5 ON");
      delay(10);
    }
  if (realservo >= 6000 && realservo <6180) {
      int servo6 = realservo;
      servo6 = map(servo6, 6000, 6180, 45, 0 );
      myservo6.write(servo6);
      Serial.println("Servo 6 ON");
      delay(10);
    }
  }
}
發表於 2018-5-12 20:52:01 | 顯示全部樓層
程式看起來似乎沒問題
可能是 softwareserial 去干擾到
可以先把藍牙改接到 0 和1
然後程式改用硬體 serial 來控制
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

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

GMT+8, 2024-3-29 14:31 , Processed in 0.201983 second(s), 6 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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