Posted on

Arduino驱动ULN2003(28BYJ48步进电机)

int p1 = 11;
int p2 = 12;
int p3 = 18;
int p4 = 31;
void setup() {
  pinMode(p1,OUTPUT);
  pinMode(p2,OUTPUT);
  pinMode(p3,OUTPUT);
  pinMode(p4,OUTPUT);
}
int steps=0;
void loop() {
  step(steps);
  steps++;
  if(steps>7){
    steps=0;
  }
  delayMicroseconds(1200);
}
void step(int t){
  switch(t){
    case 0:{
      digitalWrite(p1,HIGH);
      digitalWrite(p2,LOW);
      digitalWrite(p3,LOW);
      digitalWrite(p4,LOW);
    }
    break;
    case 1:{
      digitalWrite(p1,HIGH);
      digitalWrite(p2,HIGH);
      digitalWrite(p3,LOW);
      digitalWrite(p4,LOW);
    }
    break;
    case 2:{
      digitalWrite(p1,LOW);
      digitalWrite(p2,HIGH);
      digitalWrite(p3,LOW);
      digitalWrite(p4,LOW);
    }
    break;
    case 3:{
      digitalWrite(p1,LOW);
      digitalWrite(p2,HIGH);
      digitalWrite(p3,HIGH);
      digitalWrite(p4,LOW);
    }
    break;
    case 4:{
      digitalWrite(p1,LOW);
      digitalWrite(p2,LOW);
      digitalWrite(p3,HIGH);
      digitalWrite(p4,LOW);
    }
    break;
    case 5:{
      digitalWrite(p1,LOW);
      digitalWrite(p2,LOW);
      digitalWrite(p3,HIGH);
      digitalWrite(p4,HIGH);
    }
    break;
    case 6:{
      digitalWrite(p1,LOW);
      digitalWrite(p2,LOW);
      digitalWrite(p3,LOW);
      digitalWrite(p4,HIGH);
    }
    break;
    case 7:{
      digitalWrite(p1,HIGH);
      digitalWrite(p2,LOW);
      digitalWrite(p3,LOW);
      digitalWrite(p4,HIGH);
    }
    break;
  }
}