在这里插入图片描述

//本次实验电机驱动采用共阴极接线方式
//将步进电机的参数写到一个子函数中,并加入限位开关,触动限位开关可实现步进电机停转
//电机驱动A+,A-,B+,B-分别接电机的黑,绿,红,蓝线

#include <Servo.h>
#include <SoftwareSerial.h>

const int X_ENAPin = 7;  // Define X axis Enable pin
const int X_DIRPin = 8;  // Define X axis Direction pin
const int X_PULPin = 9;  // Define X axis Pulse pin

const int Y_ENAPin = 4;  // Define Y axis Enable pin
const int Y_DIRPin = 5;  // Define Y axis Direction pin
const int Y_PULPin = 6;  // Define Y axis Pulse pin

const int X_BUTTON = 11; // Define X axis Button pin
const int Y_BUTTON = 10; // Define Y axis Button pin

const int ServoPin = 12; // Define Servo pin

SoftwareSerial mySerial(2,3);   // Software SerialPort: RX=2, TX=3
Servo servo_12;                 // Define Servo pin
volatile char val;              // Recive the data of the Hardware SerialPort
volatile char val_Soft;         // Recive the data of the SoftWare SerialPort

int number = 0;                 // Use variable make function execution once


void setup()
{
  pinMode (X_ENAPin, OUTPUT);
  pinMode (X_PULPin, OUTPUT);
  pinMode (X_DIRPin, OUTPUT);

  pinMode (Y_ENAPin, OUTPUT);
  pinMode (Y_PULPin, OUTPUT);
  pinMode (Y_DIRPin, OUTPUT);

  pinMode (X_BUTTON, INPUT_PULLUP);
  pinMode (Y_BUTTON, INPUT_PULLUP);

  val = ' ';
  val_Soft = ' ';
  Serial.begin(9600);
  mySerial.begin(9600);

  servo_12.attach(12);
  servo_12.write(180);            // Define initial position
  delay(500);
    
}

void loop()
{

  if (number == 0)
  {
    LocationZero();                 // Bring the X and Y axes to the 0 position
    LocationMovePositive(5, 15);    // Move the horizontal and vertical axes to the (5,15) position
    
    number = 1;                     // Change the value of the variable to stop the function
  }
  
  //if (Serial.available() > 0)     // Print data from Hardware Serial
  //{
  //  val = Serial.read();
  //  mySerial.print(val);
  //}

  if (mySerial.available() > 0)     // Print data from Software Serial
  {
    val_Soft = mySerial.read();
    Serial.print(val_Soft);
  }

  // Judge the value received
  switch (val_Soft)
  {
    case '0':
    LocationMoveNegative(5, 10);    // Move the horizontal and vertical axes from (5,15) to the (0, 5) position
    DumpingGarbage();               // Pour the garbage
    LocationMovePositive(5, 10);    // Move the horizontal and vertical axes from (0, 5) to the (5,15) position
    break;
    
    case '1':
    LocationMoveNegative(0, 10);    // Move the vertical   axis from (5,15) to the (5, 5) position
    LocationMovePositive(3, 0);     // Move the horizontal axis from (5, 5) to the (8, 5) position
    DumpingGarbage();               // Pour the garbage
    LocationMoveNegative(3, 0);     // Move the horizontal axis from (8, 5) to the (5, 5) position
    LocationMovePositive(0, 10);    // Move the vertical   axis from (5, 5) to the (5,15) positon
    break;
    
    case '2':
    LocationMoveNegative(5, 15);   
    DumpingGarbage();             
    LocationMovePositive(5, 15);   
    break;
    
    case '3':
    LocationMoveNegative(0, 15);
    LocationMovePositive(3, 0);
    DumpingGarbage();
    LocationMoveNegative(3, 0);
    LocationMovePositive(0, 15);
    break;
  }

  Serial.flush();                   // Clear Software Serial buffer data
  mySerial.flush();                 // Clear Hardware Serial buffer data
  val_Soft = ' ';                   // Make the value of Software Serial is empty
  delay(500);
}
 
/*
* X轴移动步进电机子函数
* 函数:X_StepperMotor    
* 功能:控制步进电机是否脱机、方向、步数
* 参数:ENA---脱机状态,true为脱机
*      DIR---方向控制
*      steps---步进的步数,若steps为0,则电机上电电磁锁死,不转
*      delayMicroseconds---可更改电机转动速度
* 返回值:Null
*/
void X_StepperMotor(boolean ENA, boolean DIR, int steps)
{
  digitalWrite(X_ENAPin, ENA);
  digitalWrite(X_DIRPin, DIR);
  for (int i = 0; i < steps; i++)
  {
    digitalWrite(X_PULPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(X_PULPin, LOW);
    delayMicroseconds(100);
  }
}

/*
* Y轴移动步进电机子函数
* 函数:Y_StepperMotor    
* 功能:控制步进电机是否脱机、方向、步数
* 参数:ENA---脱机状态,true为脱机
*      DIR---方向控制
*      steps---步进的步数,若steps为0,则电机上电电磁锁死,不转
*      delayMicroseconds---可更改电机转动速度
* 返回值:Null
*/
void Y_StepperMotor(boolean ENA, boolean DIR, int steps)
{
  digitalWrite(Y_ENAPin, ENA);
  digitalWrite(Y_DIRPin, DIR);
  for (int i = 0; i < steps; i++) //Forward XXXX steps
  {
    digitalWrite(Y_PULPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(Y_PULPin, LOW);
    delayMicroseconds(100);
  }
}

/*
* 位置初始化函数
* 函数:LocationZero    
* 功能:使整体移动到0点位置,也就是限位开关位置
* 参数:Null
* 返回值:Null
*/
void LocationZero()
{
  // X轴达到限位开关位置
  while (digitalRead(X_BUTTON) == 1)    // 一直移动X轴直到限位开关被按下
  {
    X_StepperMotor(false, true, 10);
  }
  delay(500);                           // 等待500ms使电流消失
  X_StepperMotor(false, false, 500);    //反向移动X轴500步,留出X轴后期移动的余量
  X_StepperMotor(false, true, 0);       // 电机停转
  delay(500);
  
  // Y轴达到限位开关位置
  while (digitalRead(Y_BUTTON) == 1)    // 一直移动Y轴直到限位开关被按下
  {
    Y_StepperMotor(false, true, 10);
  }
  delay(500);                           // 等待500ms使电流消失
  Y_StepperMotor(false, false, 500);    // 反向移动Y轴500步,留出Y轴后期移动的余量
  Y_StepperMotor(false, true, 0);       // 电机停转
  delay(500);
}

/*
* 倾倒垃圾函数
* 函数:DumpingGarbage   功能:转动舵机,将垃圾倾倒进垃圾桶
* 参数:Null
* 返回值:Null
*/
void DumpingGarbage()
{
  servo_12.attach(12);
  servo_12.write(0);            // 倾倒垃圾
  delay(1000); 

  servo_12.attach(12);
  servo_12.write(180);          // 回到初始位置
  delay(1000);
}

/*
* 横纵坐标正向移动函数
* 函数:LocationMovePositive    
* 功能:控制电机移动到固定坐标位置
* 参数:x---X轴增加的坐标增量
*      y---Y轴增加的坐标增量
* 返回值:Null
*/
void LocationMovePositive(int x, int y)
{
  for (int x_step=0; x_step<x; x_step++)
  {
    X_StepperMotor(false, false, 1000);//此时3200由于在这个循环中,只要不修改成0,则修改他的值没什么影响,转速不会改变
  }
  delay(500);

  for (int y_step=0; y_step<y; y_step++)
  {
    Y_StepperMotor(false, false, 1000); //Y轴反向行走
  }
  delay(500);
}

/*
* 横纵坐标负向移动函数
* 函数:LocationMoveNegative
* 功能:控制电机移动到固定坐标位置
* 参数:x---X轴减少的坐标增量
*      y---Y轴减少的坐标增量
* 返回值:Null
*/
void LocationMoveNegative(int x, int y)
{
  for (int x_step=0; x_step<x; x_step++)
  {
    X_StepperMotor(false, true, 1000);//此时3200由于在这个循环中,只要不修改成0,则修改他的值没什么影响,转速不会改变
  }
  delay(500);

  for (int y_step=0; y_step<y; y_step++)
  {
    Y_StepperMotor(false, true, 1000); //Y轴反向行走
  }
  delay(500);
}