我有一个步进电机,我用它来驱动自动扫描仪上的升降机(如果你有兴趣获得关于它的全部功能的更详细的描述,我很乐意效劳)。
无论如何,我目前遇到的问题是,当升降机到达扫描仪顶部时,它会按下一个按钮,触发相机拍照,然后应该降低并重复该过程。
然而,问题在于,一旦电机触发按钮,它似乎就会忘记它的位置,而不是去我为它构建的预设距离,要么保持原位,要么降低预期距离的一小部分。
我对此事的想法是,我认为这要么是我的代码和我控制按钮的方式有问题,要么是需要对按钮进行去抖动以便我将其作为常数值读取的问题。
代码如下:
//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);
}