gao55372849 发表于 2014-2-7 17:04:51

步进电机跟LED

弄了一个步进电机和几个LED想让电机跟小灯协同运转。但是小灯不合拍不知道为什么!
代码贴出来希望有人能给改进一下



int motorPin1 = 8; // Blue - 28BYJ48 pin 1
int motorPin2 = 9; // Pink - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
int motorPin5 = 4; // Blue - 28BYJ48 pin 1
int motorPin6 =5; // Pink - 28BYJ48 pin 2
int motorPin7 = 6; // Yellow - 28BYJ48 pin 3
int motorPin8 = 7; // Orange - 28BYJ48 pin 4

int motorPin9 = 14; // Blue - 28BYJ48 pin 1
int motorPin10 =15; // Pink - 28BYJ48 pin 2
int motorPin11 = 16; // Yellow - 28BYJ48 pin 3
int motorPin12 = 17; // Orange - 28BYJ48 pin 4

int ledPin13 = 12; // led

// each pin of a motor
int IN1,IN2,IN3,IN4;
// Red - 28BYJ48 pin 5 (VCC)
int motorSpeed = 1500; //能够设定步进速度
int lookup = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

//////////////////////////////////////////////////////////////////////////////

void setup() {
//确定电机针脚为输出
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(motorPin3, OUTPUT);
    pinMode(motorPin4, OUTPUT);
    pinMode(motorPin5, OUTPUT);
    pinMode(motorPin6, OUTPUT);
    pinMode(motorPin7, OUTPUT);
    pinMode(motorPin8, OUTPUT);
    pinMode(motorPin9, OUTPUT);
    pinMode(motorPin10, OUTPUT);
    pinMode(motorPin11, OUTPUT);
    pinMode(motorPin12, OUTPUT);
    pinMode(ledPin13, OUTPUT);
    Serial.begin(9600);

}

// 使电机1正转200步,电机2正转200步,电机1反转200步,电机2反转200步
void loop(){

    rotate(1,0,200);
    delay(10000);   

    rotate(2,0,200);
    rotate(1,1,200);
    rotate(3,1,200);
    digitalWrite(ledPin13, HIGH);//点亮LED

delay(1000);

digitalWrite(ledPin13, LOW);//关掉LED

delay(1000);

    rotate(2,0,200);
    rotate(3,1,200);
}

void set_pin(int no) {
    switch(no) {
    case 1:
      IN1 = motorPin1;
      IN2 = motorPin2;
      IN3 = motorPin3;
      IN4 = motorPin4;
      break;
    case 2:
      IN1 = motorPin5;
      IN2 = motorPin6;
      IN3 = motorPin7;
      IN4 = motorPin8;
      break;
    case 3:
      IN1 = motorPin9;
      IN2 = motorPin10;
      IN3 = motorPin11;
      IN4 = motorPin12;
      break;
    default:
      break;
    }
}

void do_rotate(int dir, int step)
{
    if (dir == 0){
      clockwise(step);
    }
    else {
      anticlockwise(step);      
    }
}

// 电机转动函数,no是电机号,dir是方向(0->顺时针,1->逆时针),step是旋转的步数
void rotate(int no, int dir, int step)
{
    set_pin(no);
    do_rotate(dir, step);
    stop(no);
}


// 向电机发出脉冲
void setOutput(int out)

{
    digitalWrite(IN1, bitRead(lookup, 0));
    digitalWrite(IN2, bitRead(lookup, 1));
    digitalWrite(IN3, bitRead(lookup, 2));
    digitalWrite(IN4, bitRead(lookup, 3));

}

//以下为定义电机顺时针旋转函数
// step 参数为旋转的步数
void clockwise(int step)

{
    for (;step > 0;step--){
      for(int i = 7; i >= 0; i--)
      {
            setOutput(i);
            delayMicroseconds(motorSpeed);
      }
    }
}

//以下为定义电机逆时针旋转函数
// step 参数为旋转的步数

void anticlockwise(int step)

{
    for (; step > 0; step--) {
      for(int i = 0; i < 8; i++)
      {
            setOutput(i);
            delayMicroseconds(motorSpeed);
      }
    }
}

// 等待一段时间
void wait(int time)
{
    int time1 = millis(); //记录时间
    while (millis() - time1< time) {} // 忙等
}

//关闭电机函数, no 是电机号 (1 ... n)
void stop(int no)
{
    set_pin(no);
    digitalWrite(IN1, 0);
    digitalWrite(IN2, 0);
    digitalWrite(IN3, 0);
    digitalWrite(IN4, 0);
}

现在的情况是电机转灯也亮不是转完再亮的
页: [1]
查看完整版本: 步进电机跟LED