2013-07-09 67 views
1

我有一臺步進電機,用於在自動掃描儀上爲電梯提供動力(如果您有興趣獲得它的全部功能的更大描述,我很樂意提供)。爲什麼我的電機沒有按照我告訴的距離走?

無論如何,我目前遇到的問題是,當電梯到達掃描儀的頂部時,它會按下觸發相機拍攝照片的按鈕,然後應該降低並重復該過程。

但是,問題是,一旦電機觸發按鈕,它似乎失去了原來的位置,而不是去爲我建造的預設距離,它要麼停留在原地,要麼降低一小部分預期的距離。

我對這件事的想法是,我認爲這或者是我的代碼問題,我控制按鈕的方式,或者是需要消除按鈕的問題,以便我將其作爲一個常數值。

的代碼如下:

//Global Variables 
//----------------------------------------------------------------------------- 
const int BUTTON_PIN = 4;        // number of the pushbutton pin 
const int CAMERA_SHOOT_PIN = 2;       // Pin that controls the shoot function 

const int MOTOR_DIRECTION_PIN = 8; 
const int MOTOR_STEP_PIN = 9; 

//Function Prototypes 
//----------------------------------------------------------------------------- 

//If the red button is press, the camera will take a shot 
void checkIfButtonIsPressedAndTakePictureWithCamera(); 

//Moves platform tray down lead screw down to zero??? 
void moveDown(int clicks); 

//Moves platform tray up lead screw up to "maxDistance" 
void moveUp(int clicks); 

//Presses the camera's shoot button and takes a picture 
void shootCamera(); 

//Steps the motor one click 
void stepMotorOneClick(); 

//Steps the motor N clicks 
void stepMotorNClicks(int n); 

//Changes the current motor direction clcokwise 
void toggleDirectionClockwise(); 

//Changes the current motor direction counterclockwise 
void toggleDirectionCounterClockwise(); 


//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 

//Called once when the Arduino is powered up or reset button is hit 
/*****************************************************************************/ 
void setup() 
/*****************************************************************************/ 
{ 
    Serial.begin(9600); //Initializes serial port at baud rate of 9600 bps 
    pinMode(BUTTON_PIN, INPUT); //set that the button is an input 
    pinMode(CAMERA_SHOOT_PIN, OUTPUT); // set the pin that controls the shoot function 

    //Setup motor pins 
    pinMode(MOTOR_DIRECTION_PIN, OUTPUT);  
    pinMode(MOTOR_STEP_PIN, OUTPUT); 
    digitalWrite(MOTOR_DIRECTION_PIN, LOW); 
    digitalWrite(MOTOR_STEP_PIN, LOW); 


    //moveUp(3600); 
} 

int clicks = 0; 
int moveDirection = 1; 

//Called over and over again after setup() executes 
/*****************************************************************************/ 
void loop() 
/*****************************************************************************/ 
{ 
    clicks = clicks + 1; 
    if(clicks > 7000) 
    { 
    moveDirection = -moveDirection; 
    clicks = 0; 
    } 

    switch(moveDirection) 
    { 
    case 1: moveUp(1); break; 
    case -1: moveDown(1); break; 
    case 0: break; 
    default: break; 
    }; 

    checkIfButtonIsPressedAndTakePictureWithCamera(); 
} 

//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 
//----------------------------------------------------------------------------- 

//Function Implemented 
//----------------------------------------------------------------------------- 
/*****************************************************************************/ 
void checkIfButtonIsPressedAndTakePictureWithCamera() 
/*****************************************************************************/ 
{ 
    if (digitalRead(BUTTON_PIN) == HIGH) //If the button is pressed, run through the my functions 
    { 
    //START CRAPPY HACK 
    if (moveDirection == 0) 
     moveDirection = 1; 
    else 
    { 
     moveDirection = 0; 
     shootCamera(); 
     moveDirection=-1; 
    } 
    //END CRAPPY HACK 
    } 
} 

/*****************************************************************************/ 
void toggleDirectionClockwise() 
/*****************************************************************************/ 
{ 
    digitalWrite(8, LOW); 
} 

/*****************************************************************************/ 
void toggleDirectionCounterClockwise() 
/*****************************************************************************/ 
{ 
    digitalWrite(8, HIGH); 
} 

/*****************************************************************************/ 
void stepMotorOneClick() 
/*****************************************************************************/ 
{ 
    digitalWrite(MOTOR_STEP_PIN, HIGH); 
    delayMicroseconds(40);   
    digitalWrite(MOTOR_STEP_PIN, LOW); 
    delayMicroseconds(40); 
} 

/*****************************************************************************/ 
void stepMotorNClicks(int n) 
/*****************************************************************************/ 
{ 
    for(int c = 0; c < n; c++) 
    stepMotorOneClick(); 
} 

/*****************************************************************************/ 
void moveDown(int clicks) 
/*****************************************************************************/ 
{ 
    //counterclock 
    toggleDirectionCounterClockwise(); 
    stepMotorNClicks(clicks); 
} 

/*****************************************************************************/ 
void moveUp(int clicks) 
/*****************************************************************************/ 
{ 
    //clockwise 
    toggleDirectionClockwise(); 
    stepMotorNClicks(clicks); 
} 

/*****************************************************************************/ 
void shootCamera() 
/*****************************************************************************/ 
{ 
    digitalWrite(CAMERA_SHOOT_PIN,HIGH); //SHOOT 
    delay(500); 
    digitalWrite(CAMERA_SHOOT_PIN,LOW); 
    delay(1); 
} 

回答

4

當它到達頂部和按壓按鈕,代碼需要的圖片與相機(0.5秒),然後設置爲向下方向 - 但沒有按實際上並沒有下降。因此,在下一個循環中,方向已關閉,因此它向下移動一步,但按鈕仍可能被按下,因爲一步很小。所以它需要另一張照片(0.5秒)等等等等。

它結束了在頂部拍幾張照片,因爲開關被按下。

拍攝照片後,您可能需要將系統向下移動一些步驟。或者不要拍下方向朝下的照片。

void checkIfButtonIsPressedAndTakePictureWithCamera() 
/*****************************************************************************/ 
{ 
    if (digitalRead(BUTTON_PIN) == HIGH) //If the button is pressed, run through the my functions 
    { 
    //START CRAPPY HACK 
    if (moveDirection == 0) 
     moveDirection = 1; 
    else if(moveDirection != -1) //If it's not moving down already 
    { 
     moveDirection = 0; 
     shootCamera(); 
     moveDirection=-1; 
    } 
    //END CRAPPY HACK 
    } 
} 
+0

謝謝我的好朋友! – MikePinnell

相關問題