f660229 發表於 2018-2-14 18:16:29

藍芽控制 自走車+2顆伺服馬達

我使用藍芽+inventor 2 APP(手機控制)+arduino控制自走車+伺服馬達   我希望能夠按住1個按鈕=>2顆伺服馬達的角度慢慢上升or下降 放開後就停止現在自走車可以正常動作但伺服馬達指會固定在一開始設定的角度然後就不動了

下面是inventor 2的積木圖 & arduino程式






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

Servo servo1;
Servo servo2;
SoftwareSerial BT(11,12);
const int leftMotorIn1 = 2;
const int leftMotorIn2 = 3;
const int rightMotorIn3 = 4;      
const int rightMotorIn4 = 5;
char command;
int ang;

void setup()
{
pinMode(leftMotorIn1, OUTPUT);
pinMode(leftMotorIn2, OUTPUT);
pinMode(rightMotorIn3, OUTPUT);
pinMode(rightMotorIn4, OUTPUT);
Serial.begin(38400);
BT.begin(38400);
servo1.attach(6);
servo2.attach(7);
servo1.write(80);
servo2.write(80);
}

void loop()
{
if(BT.available() > 0)
{
   command = BT.read();
      Serial.println(command);
      switch (command) {
      
      case 'A':
      backward();
      
      break;
      
      case 'B':
      forward();
      
      break;
   
      case 'C':
      left();
      break;
      
      case 'D':
      right();
      break;
   
      case 'S':
      motorstop();
      break;
      
      case 'E':
      ang = ang + 3;
      if (ang > 180) ang = 180;
      servo1.write(ang);
      servo2.write(ang);
      delay(40);
            
      break;

      case 'F':
      ang = ang - 3;
      if (ang > 180) ang = 180;
      servo1.write(ang);
      servo2.write(ang);
      delay(40);      
            
      break;

      case 'G':
      
       servo1.write(80);
       servo2.write(80);
      
      break;
      }
}   
}

void motorstop()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, LOW);
}

void forward()
{
digitalWrite(leftMotorIn1, HIGH);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, HIGH);
digitalWrite(rightMotorIn4, LOW);
}

void backward()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, HIGH);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, HIGH);
}

void right()
{
    digitalWrite(leftMotorIn1, HIGH);
    digitalWrite(leftMotorIn2, LOW);
    digitalWrite(rightMotorIn3, LOW);
    digitalWrite(rightMotorIn4, HIGH);
}

void left()
{
    digitalWrite(leftMotorIn1, LOW);
    digitalWrite(leftMotorIn2, HIGH);
    digitalWrite(rightMotorIn3, HIGH);
    digitalWrite(rightMotorIn4, LOW);
}

超新手 發表於 2018-2-14 19:55:26

本帖最後由 超新手 於 2018-2-14 20:14 編輯

應該是
ang = ang - 3;
if (ang > 180) ang = 180;
寫錯了,改成
ang = ang - 3;
if (ang < 0) ang = 0;
試看看
另外,ang 也要給初始值
int ang=80;

f660229 發表於 2018-2-14 20:44:50

可以動作了只是角度上升下降的時候是 1格1格的動   請問這是因為電流不足的問題嗎?還是程式有問題?如果是電流不足的話外並連1個電池合可以解決嗎??

超新手 發表於 2018-2-14 20:52:55

你不就是要一格一格的動?
和你說的“.....角度慢慢上升or下降......”
意思不是一樣嗎?

f660229 發表於 2018-2-15 14:17:23

現在是動一下停一下這樣慢慢地動我希望它的動作是持續性的中間部會斷掉

超新手 發表於 2018-2-15 14:40:51

你可以試著調整
ang =ang+3; 和 ang =ang-3; 中的
3這個數字,試著調大或調小
到你希望的數字,可能可以“接近”你要的目的
如果要很順,程式就要大改了

f660229 發表於 2018-2-19 01:26:08

現在出現新的問題了   當我藍芽連線的時候2個伺服馬達會自己動作 要按下上升or下降的按鈕後才會停止
請求1. 能否請教大改要如何改??
請求2. 我現在加了避障模組上去但我的模組不會動作下面是現在的程式
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Servo.h>

Servo servo1;
Servo servo2;
SoftwareSerial BT(11,12);
const int leftMotorIn1 = 2;
const int leftMotorIn2 = 3;
const int rightMotorIn3 = 4;      
const int rightMotorIn4 = 5;
char command;
int ang1 = 80;
int ang2 = 80;
int IR_Objects = 8;
int IR = 0;

void setup()
{
pinMode(IR_Objects,INPUT);
pinMode(leftMotorIn1, OUTPUT);
pinMode(leftMotorIn2, OUTPUT);
pinMode(rightMotorIn3, OUTPUT);
pinMode(rightMotorIn4, OUTPUT);
Serial.begin(38400);
BT.begin(38400);
servo1.attach(6);
servo2.attach(7);
servo1.write(80);
servo2.write(80);
}

void loop()
{
IR=digitalRead(IR_Objects);
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, LOW);
delay(IR);

if(BT.available() > 0)
{
   command = BT.read();
      Serial.println(command);
      switch (command) {
      
      case 'A':
      backward();
      
      break;
      
      case 'B':
      forward();
      
      break;
   
      case 'C':
      right();
      break;
      
      case 'D':
      left();
      break;
   
      case 'S':
      motorstop();
      break;
      
      case 'E':
      ang1 = ang1 + 3;
      if (ang1 > 180) ang1 = 180;
      ang2 = ang2 - 3;
      if (ang2 < 0) ang2 = 0;
      servo1.write(ang1);
      servo2.write(ang2);
      delay(40);
            
      break;

      case 'F':
      ang1 = ang1 - 3;
      if (ang1 < 0) ang1 = 0;
      ang2 = ang2 + 3;
      if (ang2 > 180) ang2 = 180;
      servo1.write(ang1);
      servo2.write(ang2);
      delay(40);      
            
      break;

      case 'G':
      
       servo1.write(80);
       servo2.write(80);
      
      break;
      }
}   
}

void motorstop()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, LOW);
}

void forward()
{
digitalWrite(leftMotorIn1, HIGH);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, HIGH);
digitalWrite(rightMotorIn4, LOW);
}

void backward()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, HIGH);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, HIGH);
}

void right()
{
    digitalWrite(leftMotorIn1, HIGH);
    digitalWrite(leftMotorIn2, LOW);
    digitalWrite(rightMotorIn3, LOW);
    digitalWrite(rightMotorIn4, HIGH);
}

void left()
{
    digitalWrite(leftMotorIn1, LOW);
    digitalWrite(leftMotorIn2, HIGH);
    digitalWrite(rightMotorIn3, HIGH);
    digitalWrite(rightMotorIn4, LOW);
}

超新手 發表於 2018-2-19 07:16:25

本帖最後由 超新手 於 2018-2-19 07:18 編輯

1. 要大改?效果不見得很好,
    建議你先調整值
    不過要調整這些東西
    必須等你所有功能(像避障)加上去再說
2. 避障功能必須判斷 ir 的值, 再執行 stop
   而且如果遇到障礙,就不處理上面來的運動命令
   免得走走停停
3.會動是因為連線時, 你設“啟動計時為真”
   又沒將變數 a 變數清為 0
   簡單的解法就是將“啟動計時為假”即可

f660229 發表於 2018-2-19 10:42:07

這是我現在改完的程式   自走車不會動伺服馬達OK了
請問我程式哪裡出了問題?要怎麼改比較好?

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

Servo servo1;
Servo servo2;
SoftwareSerial BT(11,12);
const int leftMotorIn1 = 2;
const int leftMotorIn2 = 3;
const int rightMotorIn3 = 4;      
const int rightMotorIn4 = 5;
char command;
int ang1 = 80;
int ang2 = 80;
const int irPin = 8;
int val = HIGH;

void setup()
{
pinMode(irPin,INPUT);
pinMode(leftMotorIn1, OUTPUT);
pinMode(leftMotorIn2, OUTPUT);
pinMode(rightMotorIn3, OUTPUT);
pinMode(rightMotorIn4, OUTPUT);
Serial.begin(38400);
BT.begin(38400);
servo1.attach(6);
servo2.attach(7);
servo1.write(80);
servo2.write(80);
}

void loop()
{
if(BT.available() > 0)
{
   command = BT.read();
      Serial.println(command);
      switch (command) {
      
      case 'A':
      backward();
      
      break;
      
      case 'B':
      forward();
      
      break;
   
      case 'C':
      right();
      break;
      
      case 'D':
      left();
      break;
   
      case 'S':
      motorstop();
      break;
      
      case 'E':
      ang1 = ang1 + 3;
      if (ang1 > 180) ang1 = 180;
      ang2 = ang2 - 3;
      if (ang2 < 0) ang2 = 0;
      servo1.write(ang1);
      servo2.write(ang2);
      delay(40);
            
      break;

      case 'F':
      ang1 = ang1 - 3;
      if (ang1 < 0) ang1 = 0;
      ang2 = ang2 + 3;
      if (ang2 > 180) ang2 = 180;
      servo1.write(ang1);
      servo2.write(ang2);
      delay(40);      
            
      break;

      case 'G':
      
       servo1.write(80);
       servo2.write(80);
      
      break;
      }
}   
int val = digitalRead(irPin);
if (val == LOW)
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, LOW);
}
}

void motorstop()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, LOW);
}

void forward()
{
digitalWrite(leftMotorIn1, HIGH);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, HIGH);
digitalWrite(rightMotorIn4, LOW);
}

void backward()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, HIGH);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, HIGH);
}

void right()
{
    digitalWrite(leftMotorIn1, HIGH);
    digitalWrite(leftMotorIn2, LOW);
    digitalWrite(rightMotorIn3, LOW);
    digitalWrite(rightMotorIn4, HIGH);
}

void left()
{
    digitalWrite(leftMotorIn1, LOW);
    digitalWrite(leftMotorIn2, HIGH);
    digitalWrite(rightMotorIn3, HIGH);
    digitalWrite(rightMotorIn4, LOW);
}

超新手 發表於 2018-2-19 13:18:27

本帖最後由 超新手 於 2018-2-19 13:20 編輯

你確定在有障礙時,
int val = digitalRead(irPin);
的值是 low 嗎?
有沒有先寫個小程式,確認一下 ir 的輸出是不是
和自己想的相同

f660229 發表於 2018-2-19 19:11:31

本帖最後由 f660229 於 2018-2-19 19:24 編輯

硬體接線接錯   現在可以動作了可是避障模組會不穩定   自走車會走走停停的   請問是出了什麼問題嗎?

超新手 發表於 2018-2-19 21:06:14

初步是看不出程式有什麼問題
要不要先單獨測一下 ir
接上 usb, 利用 serial
把 ir 值 在序列監視視窗中print 出來看看
距離多少會 high ,多少距離會變 low

f660229 發表於 2018-2-20 14:04:24

請問要如何 把 ir 值 在序列監視視窗中print 出來??

超新手 發表於 2018-2-20 16:14:18

Serial.println(val);
頁: [1]
查看完整版本: 藍芽控制 自走車+2顆伺服馬達