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

1 に答える 1

4

上部に到達してボタンを押すと、コードはカメラで写真を撮り (0.5 秒)、下に移動する方向を設定しますが、実際には下に移動しません。次のループでは、方向が下なので 1 ステップ下に移動しますが、1 ステップが非常に小さいため、ボタンが押されたままになる可能性があります。そのため、別の写真 (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 
  }
}
于 2013-07-10T12:59:31.510 に答える