arduino代码合集

Menu

步进电机测试程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Arduino控制NEMA步进电机测试程序 (2018-09-25)
by 太极创客(www.taichi-maker.com)
本示例程序旨在演示如何通过Arduino控制NEMA步进电机。
用户可通过串口监视器控制电机的各个功能
如需获得本示例程序详细电路信息以及如何使用Arduino控制电机的更多知识,请参考太极创客网站:
http://www.taichi-maker.com/homepage/reference-index/motor-reference-index/arduino-a4988-nema-stepper-motor/

控制指令:
顺时针旋转/逆时针旋转 - x0/x1
运行步数 - z100(走100步)
步进方式 - b1(全步),b2(半步),b4(四分),b8(8分),b16(16分)
速度 - d2000(转动速度2000)
允许工作/禁止工作(enable/disable) - g1/g0
睡眠 - m0(sleep)/m1(awake)
*/

// A4988引脚连接Arduino引脚编号
const int dirPin = 2; // Direction
const int stepPin = 3; // Step
const int sleepPin = 4; // Sleep
const int resetPin = 5; // Reset
const int ms3Pin = 6; // Ms3
const int ms2Pin = 7; // Ms2
const int ms1Pin = 8; // Ms1
const int enPin = 9; // Enable

// 步进电机旋转一周步数
const int STEPS_PER_REV = 200;

char cmd; //用户指令字符
int data; //用户指令数据
int motorSpeed = 2000; //电机转速(数值越小速度越小)

void setup() {
// 设置引脚模式
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(sleepPin,OUTPUT);
pinMode(resetPin,OUTPUT);
pinMode(ms3Pin,OUTPUT);
pinMode(ms2Pin,OUTPUT);
pinMode(ms1Pin,OUTPUT);
pinMode(enPin,OUTPUT);

// 初始化引脚状态
digitalWrite(sleepPin, HIGH);
digitalWrite(resetPin, HIGH);
digitalWrite(enPin, LOW);

// 初始化电机步进模式为全步进
digitalWrite(ms1Pin, LOW);
digitalWrite(ms2Pin, LOW);
digitalWrite(ms3Pin, LOW);

Serial.begin(9600);
Serial.println("++++++++++++++++++++++++++++++++++");
Serial.println("+ Taichi-Maker A4988 Steper Demo +");
Serial.println("+ www.taichi-maker.com +");
Serial.println("++++++++++++++++++++++++++++++++++");
Serial.println("");
Serial.println("Please input motor command:");
}

void loop() {
if (Serial.available()) { // 检查串口缓存是否有数据等待传输
cmd = Serial.read(); // 获取电机指令中电机编号信息
Serial.print("cmd = ");
Serial.print(cmd);
Serial.print(" , ");

data = Serial.parseInt();
Serial.print("data = ");
Serial.print(data);
Serial.println("");

runUsrCmd();
}
}

//此函数用于运行用户指令
void runUsrCmd(){
switch(cmd){
case 'x': // 设置步进电机旋转(顺时针/逆时针)
Serial.print("Set Rotation To ");
if (data == 0){
digitalWrite(dirPin, 0);
Serial.println("Clockwise.");
} else {
digitalWrite(dirPin, 1);
Serial.println("Counter Clockwise.");
}
break;

case 'g': // 设置A4988 enable功能
Serial.print("Set Motor To ");
if (data == 0){
digitalWrite(enPin, 1);
Serial.println("Disable.");
} else {
digitalWrite(enPin, 0);
Serial.println("Enable.");
}
break;

case 'm': // 设置A4988 sleep功能
Serial.print("Set Motor To ");
if (data == 0){
digitalWrite(sleepPin, 0);
Serial.println("Sleep.");
} else {
digitalWrite(sleepPin, 1);
Serial.println("Awake.");
}
break;

case 'b': // 设置步进模式
if (data == 1 || data == 2 || data == 4 || data == 8 || data == 16){
Serial.print("Set Motor Step Control To ");
setStepMode(data);
} else {
Serial.println("Wrong Step Mode Cmd!");
}
break;

case 'z': // 设置步进电机运行步数
runStepper(motorSpeed, data);
break;

case 'd': // 设置步进电机运行速度
motorSpeed = data;
Serial.print("Set Motor Speed To ");
Serial.println(data);
break;

default: // 未知指令
Serial.println("Unknown Command");
}
}

//运行步进电机
void runStepper (int rotationSpeed, int stepNum){
for(int x = 0; x < stepNum; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(rotationSpeed);
digitalWrite(stepPin,LOW);
delayMicroseconds(rotationSpeed);
}
}

//设置步进模式
void setStepMode(int modeNum){
switch(modeNum){
case 1: // 全步进
digitalWrite(ms1Pin, LOW);
digitalWrite(ms2Pin, LOW);
digitalWrite(ms3Pin, LOW);
Serial.println(F("Stepping Mode: Full"));
break;

case 2: // 半步进
digitalWrite(ms1Pin, HIGH);
digitalWrite(ms2Pin, LOW);
digitalWrite(ms3Pin, LOW);
Serial.println(F("Stepping Mode: 1/2"));
break;

case 4: // 1/4 步进
digitalWrite(ms1Pin, LOW);
digitalWrite(ms2Pin, HIGH);
digitalWrite(ms3Pin, LOW);
Serial.println(F("Stepping Mode: 1/4"));
break;

case 8: // 1/8 步进
digitalWrite(ms1Pin, HIGH);
digitalWrite(ms2Pin, HIGH);
digitalWrite(ms3Pin, LOW);
Serial.println(F("Stepping Mode: 1/8"));
break;

case 16: // 1/16 步进
digitalWrite(ms1Pin, HIGH);
digitalWrite(ms2Pin, HIGH);
digitalWrite(ms3Pin, HIGH);
Serial.println(F("Stepping Mode: 1/16"));
break;
}
}

门禁控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <SPI.h>
#include <MFRC522.h>
#include <IRremote.h>

// 引脚 宏定义
#define NFC_SS 10
#define NFC_RST 9

#define SETEP_EN 4
#define SETEP_STEP 3
#define SETEP_DIR 2

#define RECV_PIN 8

#define SWITCH_PIN 7

// 变量及全局维护列表
MFRC522 rfid(NFC_SS,NFC_RST);
IRrecv irrecv(RECV_PIN);

decode_results results; //NFC card read info.
byte Access[5][4]={{170,76,149,250},{242,46,53,46},{128,139,228,206},{240,191,231,206},{154,40,151,250}};

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
irrecv.enableIRIn(); // 初始化红外接收器
Serial.println(F("418 门禁控制系统 v2.0"));
// pin mode setting
pinMode(SETEP_EN,OUTPUT);
pinMode(SETEP_STEP,OUTPUT);
pinMode(SETEP_DIR,OUTPUT);
digitalWrite(SETEP_EN,LOW);
pinMode(SWITCH_PIN,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
/**
* 红外信号检测.
*/
if (irrecv.decode(&results)) {
Serial.print("检测到红外信号:");
Serial.println(results.value, DEC);//以16进制换行输出接收代码
if(results.value==16712445 || results.value==16753245 || results.value==16761405){
door_open();
}
//delay(200);
irrecv.resume(); // 接收下一个值
}
/**
* NFC检测.
*/

if( rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()){
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
Serial.println(F("The NUID tag is:"));
Serial.print(F("In hex: "));
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
Serial.print(F("In dec: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();

for (byte index=0;index<5;index++){
if (rfid.uid.uidByte[0] == Access[index][0] &&
rfid.uid.uidByte[1] == Access[index][1] &&
rfid.uid.uidByte[2] == Access[index][2] &&
rfid.uid.uidByte[3] == Access[index][3] ) {
Serial.println(F("Verify!"));
Serial.println("Door Opening use nfc");
door_open();
} else {
Serial.println(F("unVerify!"));
}
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}

}

int lock(){
int check;
float sum = 0;
//Serial.println(My_A5*(5.0/1023.0));
for(int i=0;i<10;i++){
check = digitalRead(SWITCH_PIN);
sum += check;
delay(1);
}
if ((sum/10) > 4.8){
return true;
} else {
return false;
}
}

void door_open(){
int x;
int xyz=250;
digitalWrite(SETEP_DIR,HIGH); // 设置高电平的方向
for(x = 0; x < xyz; x++) // 重复200次,即走200步
{
digitalWrite(SETEP_STEP,HIGH); // Output high
delayMicroseconds(800); // 等待800微秒
digitalWrite(SETEP_STEP,LOW); // Output low
delayMicroseconds(800); // 等待800微秒
if (lock()){
xyz = x;
break;
}
}
delay(1000); //暂停1000豪秒(相当于1秒)

digitalWrite(SETEP_DIR,LOW); // 设置低电平的方向

for(x=0; x < xyz; x++) // 重复200次
{
digitalWrite(SETEP_STEP,HIGH); // Output high
delayMicroseconds(800); // 等待800微秒
digitalWrite(SETEP_STEP,LOW); // Output low
delayMicroseconds(800); // 等待800微秒
}
delay(1000); //
}

/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}

/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}