2016-05-27 15 views
2
int motorPin1 = 8; 
int motorPin2 = 9; 
int motorPin3 = 10; 
int motorPin4 = 11; 
int delayTime = 2; 
char myCol[20]; 

void setup() { 
    Serial.begin (9600); 
    pinMode(motorPin1, OUTPUT); 
    pinMode(motorPin2, OUTPUT); 
    pinMode(motorPin3, OUTPUT); 
    pinMode(motorPin4, OUTPUT); 
} 


void loop() { 
    int lf = 1; 
    Serial.readBytesUntil(lf, myCol, 1); 
    if (strcmp(myCol, "p") == 0) 
    { 
    Play(); 
    } 
    if (strcmp(myCol, "s") == 0) 
    { 
    Stop(); 
    } 
} 



void Play() 
{ 
    digitalWrite(motorPin1, HIGH); 
    digitalWrite(motorPin2, HIGH); 
    digitalWrite(motorPin3, LOW); 
    digitalWrite(motorPin4, LOW); 
    delay(delayTime); 
    digitalWrite(motorPin1, LOW); 
    digitalWrite(motorPin2, HIGH); 
    digitalWrite(motorPin3, HIGH); 
    digitalWrite(motorPin4, LOW); 
    delay(delayTime); 
    digitalWrite(motorPin1, LOW); 
    digitalWrite(motorPin2, LOW); 
    digitalWrite(motorPin3, HIGH); 
    digitalWrite(motorPin4, HIGH); 
    delay(delayTime); 
    digitalWrite(motorPin1, HIGH); 
    digitalWrite(motorPin2, LOW); 
    digitalWrite(motorPin3, LOW); 
    digitalWrite(motorPin4, HIGH); 
    delay(delayTime); 
} 

void Stop() 
{ 
    digitalWrite(motorPin1, LOW); 
    digitalWrite(motorPin2, LOW); 
    digitalWrite(motorPin3, LOW); 
    digitalWrite(motorPin4, LOW); 
} 

///////////////////////////////////////////// ///////////////私のモーターはArduinoで本当に遅いです

using UnityEngine; 
    using System.Collections; 
    using System.IO.Ports; 
    using System.Threading; 

    public class Sending : MonoBehaviour { 

    //public static SerialPort sp = new SerialPort("COM4", 9600, Parity.None, 8,  StopBits.One); 
    public static SerialPort sp = new SerialPort("COM3", 9600); 

    void Start() { 
     OpenConnection(); 
    } 



    public void OpenConnection() 
    { 
     if (sp != null) 
     { 
     if (sp.IsOpen) 
     { 
      sp.Close(); 
      } 
     else 
     { 
      sp.Open(); 
     } 
     } 
    } 

    void OnApplicationQuit() 
    { 
     sp.Close(); 
    } 

    public static void play(){ 
     sp.Write("p"); 
    } 


    public static void stop(){ 
     sp.Write("s"); 
    } 
} 

//////////////////////////// //////////////

using UnityEngine; 
    using System.Collections; 

    public class Play : MonoBehaviour { 

    // Use this for initialization 
    void Start() { 

    } 

    // Update is called once per frame 
    void Update() { 

    } 

    void OnMouseDown() { 
     Sending.play(); 
    } 

} 

///////////////////////////// /////////////////////////

using UnityEngine; 
    using System.Collections; 

    public class Stop : MonoBehaviour { 


    void OnMouseDown() 
    { 
     Sending.stop(); 
    } 

} 

/////////////////////////////////////////////////////////////////////////

こんにちは、私はこのコードを私のarduinoとunityに持っています。私は再生と停止のボタンで統一していますが、私の再生ボタンを押すとモーターは動きますが、もう一つのステップ.....誰かが私を助けてくれる?

ありがとう

答えて

1

...

最初のループ()はあなたのプレイ()ルーチンを呼び出すためにチャンスを取得する前に行うために呼び出し「Serial.readBytesUntil(...)」でありますモータはステップを踏む。シリアル接続から読み取る文字がない場合、最終的にタイムアウトします。

もっと短いタイムアウトのためにSerial.setTimeout(1)を試してみてください(あるいは0を試してみてください)。 (解決策として使用しないでください)

私は、モータのステッピングからコマンド処理を分離すると思います。

幸運を祈る!

+0

Serial.setTimeout(1)が機能しました! – Sholyu

1

ディレイ機能は、ミリ秒単位で引数のかかるので、一度に2ミリ秒のためのあなたのモーターを実行しています。代わりに2000を試してください。以下は、delay()関数への参照です。アルドゥイーノの

デジタルピンのみ20ミリアンペア(Arduinoの宇野R3)の周りに、通常出力:

https://www.arduino.cc/en/Reference/Delay

それはまだ、次の考慮動作しない場合

。モータの場合、これでは十分ではないことが多く、バッテリやDC電源から外部から供給されるモータシールドが必要になります。

https://www.arduino.cc/en/Main/ArduinoMotorShieldR3

あなたは、特に複数のモーターを、実行する場合は、直接、Arduinoのためのあなたは彼らがゆっくり実行したり、ほとんどまったく実行することを期待することができます。私はここで推測を取っている

+0

私は2000年を置くと、私のモーターは1ステップで8秒かかりますが、それはさらに遅くなります – Sholyu

+0

通常の趣味の電気モーターを使用している間は、遅くする。 ハードウェアの完全なセットアップが必要な場合は、 – NoobPointerException

+0

シールドがすでに使用されていて、遅延の変更が行われていません – Sholyu

関連する問題