Robofun 機器人論壇

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

步進馬達加上極限開關

[複製鏈接]
跳轉到指定樓層
1#
發表於 2016-3-16 20:57:30 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
我想請問一下,我最近打了一個程式,但發現步進馬達常常位移,因此我想在旁加入一個極限開關,來取代步進馬達反轉750的動作,
也就是說當我碼達碰到極限開關時即停止動作,等到下一個條件來臨時,再正轉750,請問這樣的程式要如何寫出來?

以下是我目前沒加入極限開關的程式:

#include<Stepper.h>
const int PIRSensor1 = 7 ;                // 紅外線1動作感測器連接的腳位
const int PIRSensor2 = 5;                // 紅外線2動作感測器連接的腳位
Stepper stepper(200, 8, 10, 9, 11);       // 馬達1的PIN腳
const int stepsPerRevolution1 = 200;      // 馬達1一圈200步
const int openbutton = 0;                      // 紅色LED的PIN腳
const int closebutton = 1;
const int resetbutton = 3;
const int ledr  = 13;
int sensorValue1 = 0;                     // 紅外線動作感測器1訊號變數
int sensorValue2 = 0;                     // 紅外線動作感測器2訊號變數
int pos = 0;                              // 步進馬達動作變數
int c = 0;                                // 偵測滿為變數
int open1 = 0;
int close1 = 0;
int reset1 = 0;

void setup() {
  Serial.begin(9600);                     // 設定頻率
  pinMode(PIRSensor1, INPUT);             // 人體紅外線1為輸入
  pinMode(PIRSensor2, INPUT);             // 人體紅外線2為輸入
  stepper.setSpeed(12);                  // 將馬達的速度設定成RPM      
  pinMode(openbutton,INPUT);                   // 設led為輸出
  pinMode(closebutton,INPUT);
  pinMode(resetbutton,INPUT);
  pinMode(ledr,OUTPUT);
  
}

void loop(){

sensorValue1 = digitalRead(PIRSensor1);  // 讀取 PIR Sensor 的狀態
sensorValue2 = digitalRead(PIRSensor2);  // 讀取 PIR Sensor 的狀態
open1        = digitalRead(openbutton);
close1       = digitalRead(closebutton);
reset1       = digitalRead(resetbutton);

if (open1 == HIGH && c == 0 && sensorValue1 == LOW){
  stepper.step(-750);
  pos = 750;
  c = 3;
}

if (close1 == HIGH && c == 4 && sensorValue1 == LOW){
  stepper.step(750);
  pos = 0;
  c = 4;
}

if (reset1 == HIGH && sensorValue1== LOW && c == 3 ){
  stepper.step(750);
  pos = 0;
  c = 0;
}

if (reset1 == HIGH && sensorValue1 == LOW && c == 4 ){
  pos = 0;
  c = 0;
}
  
if (sensorValue1 == HIGH && pos == 0  && c == 0 ){   
stepper.step(-750);                     // 步進馬達反轉750步
pos = 750;

}
if(sensorValue1 == LOW && pos == 750)  {
stepper.step(750);                     // 步進馬達正轉750步
pos = 0;
}


if  (sensorValue2 == LOW   ) {  
digitalWrite(ledr,LOW);
c=0;
}

if (sensorValue2 == HIGH ) {
digitalWrite(ledr,HIGH);
c=1;
}


}
2#
發表於 2016-3-17 08:15:11 | 只看該作者
把 stepper.step(750); 改成750次的迴圈
然後一次走一步
看到 SENSOR 就跳出迴圈

或是可以在SETUP() 的時侯, 就直接讓馬達回到原點
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

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

GMT+8, 2024-4-29 07:39 , Processed in 0.176981 second(s), 7 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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