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に持っています。私は再生と停止のボタンで統一していますが、私の再生ボタンを押すとモーターは動きますが、もう一つのステップ.....誰かが私を助けてくれる?
ありがとう
Serial.setTimeout(1)が機能しました! – Sholyu