|
楼主 |
发表于 2022-8-25 13:56
|
显示全部楼层
改成这样还是不行,能帮我改改吗,谢谢!
- int motorPin1 = 13;
- int motorPin2 = 5;
- int motorPin3 = 4;
- int motorPin4 = 0;
- const int key1=12; // 正传按钮
- const int key2=14; // 反转按钮
- int motorSpeed = 1200; //设置步进速度
- int zheng=0; //变量zheng
- int fan=0; //变量fan
- int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
- void setup() {
- pinMode(motorPin1, OUTPUT);
- pinMode(motorPin2, OUTPUT);
- pinMode(motorPin3, OUTPUT);
- pinMode(motorPin4, OUTPUT);
- pinMode(key1,INPUT);
- pinMode(key2,INPUT);
- Serial.begin(115200);
- }
- void loop()
- {
- key_scan();
- }
- int key_scan() //检测外部按键是否有按下;
- {
- if(digitalRead(key1) == HIGH) //按键1检测
- {
- delay(10); //前沿延时消抖;
- if(digitalRead(key1) == HIGH)
- {
- while(digitalRead(key1) == HIGH);
- if(zheng==0)
- {
- zheng=1;
- clockwise(); //正传
- }
- else
- {
- stop1();
- zheng=0;
- }
- }
- }
- if(digitalRead(key2) == LOW) //按键2检测
- {
- delay(10); //前沿延时消抖;
- if(digitalRead(key2) == LOW)
- {
- while(digitalRead(key2) == LOW);
- if(fan==0)
- {
- fan=1;
- anticlockwise(); //反转
- }
- else
- {
- stop1();
- fan=0;
- }
- }
-
-
- }
- }
- void clockwise() //顺时针
- { for(int j= 0;j<5;j++)
- {
- for(int i = 7; i >= 0; i--)
- {
- setOutput(i);
- delayMicroseconds(motorSpeed);
- }
- }
- }
- void anticlockwise() //逆时针
- {
-
- { for(int i = 0; i < 8; i++)
- {
- setOutput(i);
- delayMicroseconds(motorSpeed);
- }
- }
- }
- void setOutput(int out)
- {
- digitalWrite(motorPin1, bitRead(lookup[out], 0));
- digitalWrite(motorPin2, bitRead(lookup[out], 1));
- digitalWrite(motorPin3, bitRead(lookup[out], 2));
- digitalWrite(motorPin4, bitRead(lookup[out], 3));
- }
-
- void stop1() {
- digitalWrite(motorPin1, 0);
- digitalWrite(motorPin2, 0);
- digitalWrite(motorPin3, 0);
- digitalWrite(motorPin4, 0);
- }
复制代码
|
|