|
楼主 |
发表于 2022-8-25 18:32
|
显示全部楼层
按键1,按一下,就会抖一下;按键2直接没有反应了
- int motorPin1 = 13;
- int motorPin2 = 5;
- int motorPin3 = 4;
- int motorPin4 = 0;
- const int KEY1=12; // 正传按钮
- const int KEY2=14; // 反转按钮
- int motorSpeed = 1200; //设置步进速度
- int KEY1_NUM = 0;
- int KEY2_NUM = 0;
- 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_PULLUP);
- pinMode(KEY2,INPUT_PULLUP);
- Serial.begin(115200);
- }
- void loop()
- {
- ScanKey();
- }
- void ScanKey() //自定义函数
- {
- if(digitalRead(KEY1) == LOW)
- {
- delay(30);
- if(digitalRead(KEY1) == LOW)
- {
- if (KEY1_NUM == 0) {KEY1_NUM = 1;while(digitalRead(KEY1) == LOW);}
- else if (KEY1_NUM == 1) {KEY1_NUM = 0;while(digitalRead(KEY1) == LOW);}
- }
- while(digitalRead(KEY1) == LOW);
- clockwise(); //正传
- }
- if(digitalRead(KEY2) == LOW) //按键2检测
- {
- delay(30);
- if(digitalRead(KEY2) == LOW)
- {
- if (KEY2_NUM == 0) {KEY2_NUM = 1;while(digitalRead(KEY2) == LOW);}
- else if (KEY2_NUM == 1) {KEY2_NUM = 0;while(digitalRead(KEY2) == LOW);}
- }
- while(digitalRead(KEY2) == LOW);
- anticlockwise(); //反转
-
- }
- }
- 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);
- }
复制代码 |
|