我有一台步进电机,我用它来为自动扫描仪上的升降机供电(如果你有兴趣更详细地描述它的全部功能,我很乐意帮忙)。
无论如何,我目前遇到的问题是,当电梯到达扫描仪的顶部时,它会按下一个按钮,触发相机拍照,然后降低并重复该过程。
然而,问题在于,一旦电机触发按钮,它似乎就会失去它的位置,而不是达到我为它建造的预设距离,或者保持原位,或者降低一小部分预期距离。
我对这个问题的想法是,我认为这要么是我的代码问题,也是我控制按钮的方式,或者需要去除按钮的问题,以便我将其作为一个恒定价值。
代码如下:
//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);
}
答案 0 :(得分: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
}
}