2014-10-30 90 views
0

基本上我試圖用兩個按鈕控制一個伺服器(一個用於前進,一個用於後退)。但是,我的代碼不起作用,我不知道爲什麼。本質上,我使用Sweep和Button示例來製作此代碼。然而,除非我的連接出現問題,否則它似乎無法工作。爲什麼我的伺服控制器不工作?

#include <Servo.h> 
 

 
Servo servoOne; 
 
int servoOnePos = 0; 
 
const int buttonUpPin = 13; 
 
const int buttonDownPin = 12; 
 
int buttonUpState = 0; 
 
int buttonDownState = 0; 
 

 
void setup() { 
 
    servoOne.attach(11); 
 
    pinMode(buttonUpPin, INPUT); 
 
    pinMode(buttonDownPin, INPUT); 
 
} 
 

 
void loop() { 
 
    buttonUpState = digitalRead(buttonUpPin); 
 
    buttonDownState = digitalRead(buttonDownPin); 
 
    if (buttonUpState == HIGH) { 
 
    for (servoOnePos < 180; servoOnePos += 1;) { 
 
     servoOne.write(servoOnePos); 
 
     delay(15); 
 
    } 
 
    } else if (buttonDownState == HIGH) { 
 
    for (servoOnePos <= 180; servoOnePos = servoOnePos - 1;) { 
 
     servoOne.write(servoOnePos); 
 
     delay(15); 
 
    } 
 
    } 
 
}

回答

0
#include <Servo.h> 

Servo servoOne; 

int pos = 90; 
const int maxDeg = 160; 
const int minDeg = 5; 

const int buttonUpPin = 13; 
const int buttonDownPin = 12; 

const int leftPin = 3; 
const int rightPin = 2; 

int leftPressed = 0; 
int rightPressed = 0; 

void setup() 
{ 
    servoOne.attach(11); 
    pinMode(buttonUpPin, INPUT); 
    pinMode(buttonDownPin, INPUT); 
} 

void loop() 
{ 
leftPressed = digitalRead(leftPin); 
rightPressed = digitalRead(rightPin); 

if(leftPressed){ 
    if(pos < maxDeg) { 
    pos += 3; 
    } 
    servoOne.write(pos);  
} 
if(rightPressed){ 
    if(pos > minDeg) { 
    pos -= 3; 
    } 
    servoOne.write(pos); 
    } 
    delay(15);      
} 
+0

還不行。基本上,啓動後,它只是旋轉一下,然後放慢速度,然後完全停止。 – dfgsdfg 2014-10-30 23:18:39

+0

檢查我的編輯。這應該工作 – Manos 2014-10-30 23:37:11

相關問題