Robofun 機器人論壇

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

自走車 程式延遲

[複製鏈接]
跳轉到指定樓層
1#
發表於 2017-12-20 18:41:57 | 顯示全部樓層 回帖獎勵 |倒序瀏覽 |閱讀模式
我的自走車功能是有減速馬達&伺服馬達的組合   減速馬達用來驅動輪子的  伺服馬達有另外的功能    減速馬達&伺服馬達程式分開用的話動作正常   但何在一起後動作雖然一樣  但會有不定時的延遲    請問要怎麼解決???(下面是減速馬達+伺服馬達的程式)

#include<SoftwareSerial.h>
#include <Servo.h>
#include <Wire.h>
Servo UDservo;
Servo LRservo;
SoftwareSerial BT (11,12);
char command;
float ang = 90;
const int leftMotorIn1 = 4;
const int leftMotorIn2 = 5;
const int rightMotorIn3 = 6;      
const int rightMotorIn4 = 7;
bool isForward = true;

void setup()
{

  pinMode(leftMotorIn1, OUTPUT);
  pinMode(leftMotorIn2, OUTPUT);
  pinMode(rightMotorIn3, OUTPUT);
  pinMode(rightMotorIn4, OUTPUT);
  BT.begin(38400);
  Serial.begin(38400);
  UDservo.attach(2);
  LRservo.attach(3);
  UDservo.write(90);
  LRservo.write(90);
}
void loop()
{
if (BT.available() > 0)
{
command = BT.read();
Serial.println(command);
switch (command) {
case'A':
ang = ang + 3;
if (ang > 180) ang = 180;
UDservo.write(ang);
delay(40);
break;
case 'B':
ang = ang -3;
if (ang < 0) ang = 0;
UDservo.write(ang);
delay(40);
break;

case'C':
ang = ang -3;
if (ang < 0) ang = 0;
LRservo.write(ang);
delay(40);
break;
case'D':
ang = ang + 3;
if (ang > 180) ang = 180;
LRservo.write(ang);
delay(40);
break;
}
}
int inSize;
  char input;
  if( (inSize = (BT.available())) > 0) { //讀取藍牙訊息
      Serial.print("size = ");
      Serial.println(inSize);
      Serial.print("Input = ");
      Serial.println(input=(char)BT.read());
      
      if( input == 'E' ) {
        forward();
        isForward = true;
      }
      
      if( input == 'F' ) {
        backward();
        isForward = false;
      }
   
      if( input == 'H' ) {
        left();
      }
      
      if( input == 'G' ) {
        right();
      }
   
      if( input == 'I' ) {
        motorstop();
      }
  }
}

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 left()
{
  
    digitalWrite(leftMotorIn1, HIGH);
    digitalWrite(leftMotorIn2, LOW);
    digitalWrite(rightMotorIn3, LOW);
    digitalWrite(rightMotorIn4, LOW);
  
}

void right()
{

    digitalWrite(leftMotorIn1, LOW);
    digitalWrite(leftMotorIn2, LOW);
    digitalWrite(rightMotorIn3, HIGH);
    digitalWrite(rightMotorIn4, LOW);
  
}
2#
 樓主| 發表於 2017-12-22 23:48:26 | 顯示全部樓層
本帖最後由 f660229 於 2017-12-23 00:35 編輯

請問 ---在loop() {...} 中,把
Int inSize;
char input;
以下的程式都拿掉(整合到上面的 switch中)
然後在 loop 中的 switch(command)   {...} 中增加
case 'E' : ~ case 'I':---
是指把馬達的程式整個改掉嗎?還是改掉input=(char)BT.read();砍掉  然後把馬達的程式直接複製貼上?

UDservo & LRservo  我換成不同變數了 。請問設同一個變數會有什麼問題嗎??
3#
 樓主| 發表於 2017-12-23 23:48:17 | 顯示全部樓層
本帖最後由 f660229 於 2017-12-23 23:52 編輯

謝謝     動作不會延遲了
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

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

GMT+8, 2024-5-6 13:19 , Processed in 0.219713 second(s), 6 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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