|
想請問一下大家,我目前寫出來的程式是碼達1轉完後碼達2才開始轉,但我希望的是2顆馬達同時轉動,想請問一下大家要如何修改?
以下是我的程式:
#include<Stepper.h>
const int PIRSensor = 7 ; // 紅外線動作感測器連接的腳位
const int stepsPerRevolution1 = 2048; //馬達1一圈2048步
const int stepsPerRevolution2 = 2048; //馬達2一圈2048步
int sensorValue = 0; // 紅外線動作感測器訊號變數
int pos = 0;
Stepper myStepper1(stepsPerRevolution1, 8, 10, 9, 11);
Stepper myStepper2(stepsPerRevolution2, 2, 4, 3, 5);
const int pd6 = 6; // 高
const int senRead6 = 2; // 高
const int ledr = 13;
int limit = 850;
int c = 0;
void setup() {
pinMode(PIRSensor, INPUT);
myStepper1.setSpeed(5);
myStepper2.setSpeed(6);
Serial.begin(9600);
pinMode(pd6,OUTPUT);
pinMode(ledr,OUTPUT);
digitalWrite(pd6,HIGH);
}
void loop(){
sensorValue = digitalRead(PIRSensor); // 讀取 PIR Sensor 的狀態
if (sensorValue == HIGH && pos == 0 && c==0 ) {
delay(500) ;
Serial.println("clockwise");
myStepper1.step(388);
myStepper2.step(-388);
pos = 388;
}
else if(sensorValue == LOW && pos == 388 && c==0) {
delay(500);
Serial.println("counterclockwise");
myStepper1.step(-388);
myStepper2.step(388);
pos = 0;
}
int val3 = analogRead(senRead6);
if (val3>=limit) {
digitalWrite(ledr,HIGH);
c = 1;
}
else if(val3<=limit) {
digitalWrite(ledr,LOW);
c = 0;
}
} |
|