请设计一个基于ArduinoUNO开发板的智能控制系统,系统包含带上拉电阻的按键模块、带上拉电阻的LED指示灯、数码管模块和舵机模块,具体功能要求如下。当按键按下LED指示灯常亮,舵机位于0度位置。(20分)当按键按下LED指示灯常亮,舵机位于0度位置,并且舵机从初始0度运行至175度。(20分)当按键按下LED指示灯常亮,舵机位于0度位置,并且舵机从初始0度运行至175度,然后从175度返回至0度,并循环摆动(30分)数码管同步显示舵机摆动次数,(0度~120度 ~0度)
时间: 2024-03-09 13:46:13 浏览: 49
基于STM32的智能家居管理系统的研究与设计-论文
首先,需要准备以下硬件:
- Arduino UNO开发板
- 按键模块
- LED指示灯
- 数码管模块
- 舵机模块
接下来,我们可以使用下面的代码实现上述要求:
```
#include <Servo.h>
#define BUTTON_PIN 2
#define LED_PIN 3
#define SERVO_PIN 9
#define DIGIT1_PIN 4
#define DIGIT2_PIN 5
#define DIGIT3_PIN 6
#define DIGIT4_PIN 7
int buttonState = 0;
int servoAngle = 0;
int servoDirection = 1;
int servoCount = 0;
Servo servo;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
pinMode(DIGIT1_PIN, OUTPUT);
pinMode(DIGIT2_PIN, OUTPUT);
pinMode(DIGIT3_PIN, OUTPUT);
pinMode(DIGIT4_PIN, OUTPUT);
servo.attach(SERVO_PIN);
}
void loop() {
buttonState = digitalRead(BUTTON_PIN);
// 按键按下
if (buttonState == LOW) {
digitalWrite(LED_PIN, HIGH);
// 舵机从0度转到175度
for (int i = 0; i <= 175; i++) {
servo.write(i);
delay(15);
}
// 舵机从175度转回0度
for (int i = 175; i >= 0; i--) {
servo.write(i);
delay(15);
}
// 循环摆动
while (1) {
// 舵机从0度转到175度
for (int i = 0; i <= 175; i++) {
servo.write(i);
delay(15);
}
// 舵机从175度转回0度
for (int i = 175; i >= 0; i--) {
servo.write(i);
delay(15);
}
servoCount++;
displayNumber(servoCount);
delay(1000);
}
} else {
digitalWrite(LED_PIN, LOW);
servo.write(0);
}
}
void displayNumber(int number) {
int digit1 = number / 1000;
int digit2 = (number / 100) % 10;
int digit3 = (number / 10) % 10;
int digit4 = number % 10;
digitalWrite(DIGIT1_PIN, HIGH);
digitalWrite(DIGIT2_PIN, LOW);
digitalWrite(DIGIT3_PIN, LOW);
digitalWrite(DIGIT4_PIN, LOW);
displayDigit(digit1);
digitalWrite(DIGIT1_PIN, LOW);
digitalWrite(DIGIT2_PIN, HIGH);
digitalWrite(DIGIT3_PIN, LOW);
digitalWrite(DIGIT4_PIN, LOW);
displayDigit(digit2);
digitalWrite(DIGIT1_PIN, LOW);
digitalWrite(DIGIT2_PIN, LOW);
digitalWrite(DIGIT3_PIN, HIGH);
digitalWrite(DIGIT4_PIN, LOW);
displayDigit(digit3);
digitalWrite(DIGIT1_PIN, LOW);
digitalWrite(DIGIT2_PIN, LOW);
digitalWrite(DIGIT3_PIN, LOW);
digitalWrite(DIGIT4_PIN, HIGH);
displayDigit(digit4);
}
void displayDigit(int digit) {
switch (digit) {
case 0:
digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, HIGH);
digitalWrite(E_PIN, HIGH);
digitalWrite(F_PIN, HIGH);
digitalWrite(G_PIN, LOW);
break;
case 1:
digitalWrite(A_PIN, LOW);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, LOW);
digitalWrite(E_PIN, LOW);
digitalWrite(F_PIN, LOW);
digitalWrite(G_PIN, LOW);
break;
case 2:
digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, LOW);
digitalWrite(D_PIN, HIGH);
digitalWrite(E_PIN, HIGH);
digitalWrite(F_PIN, LOW);
digitalWrite(G_PIN, HIGH);
break;
case 3:
digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, HIGH);
digitalWrite(E_PIN, LOW);
digitalWrite(F_PIN, LOW);
digitalWrite(G_PIN, HIGH);
break;
case 4:
digitalWrite(A_PIN, LOW);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, LOW);
digitalWrite(E_PIN, LOW);
digitalWrite(F_PIN, HIGH);
digitalWrite(G_PIN, HIGH);
break;
case 5:
digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, LOW);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, HIGH);
digitalWrite(E_PIN, LOW);
digitalWrite(F_PIN, HIGH);
digitalWrite(G_PIN, HIGH);
break;
case 6:
digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, LOW);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, HIGH);
digitalWrite(E_PIN, HIGH);
digitalWrite(F_PIN, HIGH);
digitalWrite(G_PIN, HIGH);
break;
case 7:
digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, LOW);
digitalWrite(E_PIN, LOW);
digitalWrite(F_PIN, LOW);
digitalWrite(G_PIN, LOW);
break;
case 8:
digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, HIGH);
digitalWrite(E_PIN, HIGH);
digitalWrite(F_PIN, HIGH);
digitalWrite(G_PIN, HIGH);
break;
case 9:
digitalWrite(A_PIN, HIGH);
digitalWrite(B_PIN, HIGH);
digitalWrite(C_PIN, HIGH);
digitalWrite(D_PIN, HIGH);
digitalWrite(E_PIN, LOW);
digitalWrite(F_PIN, HIGH);
digitalWrite(G_PIN, HIGH);
break;
}
}
```
这段代码包含了按键模块的检测、LED指示灯的控制、舵机运动的控制和数码管模块的显示。其中,舵机运动部分使用了 for 循环来让舵机从初始角度到目标角度旋转,并使用 while 循环来让舵机循环摆动。同时,使用了 displayNumber 函数来将舵机摆动次数显示在数码管上。
阅读全文