Arduino智能衣架可做毕设(已开源)

admin 2026-05-19 06:14:07 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文介绍基于Arduino的智能晾衣架开源项目,包含硬件清单(红外/雨滴/光照传感器、舵机、L298N等)、引脚连接图和完整控制代码。系统通过光照强度自动控制衣架伸缩(光照充足时伸出,不足时收回),雨滴传感器实现雨天自动收回功能,红外传感器检测人体触发舵机转动。项目提供可操作的硬件搭建方案和可直接使用的Arduino代码,适合作为物联网入门实践或毕业设计参考。 综合评分: 75 文章分类: 其他


cover_image

Arduino智能衣架 可做毕设 (已开源)

原创

大表哥吆 大表哥吆

kali笔记

2026年5月17日 10:19 甘肃

在小说阅读器读本章

去阅读

本文为大家分享基于Arduino的智能晾衣架。详细描述可参考视频介绍。

硬件清单

  • 红外传感器
  • 舵机
  • 雨滴传感器
  • 光照传感器
  • L298N
  • 减速马达

线路连接

| 硬件组件 | 连接类型 | Arduino引脚 | | — | — | — | | 红外传感器 | 数字 | 6 | | 舵机 | 数字 | 5 | | 雨滴传感器 | 模拟 | A0 | | 光照传感器 | 模拟 | A1 | | L298N IN1 | 数字 | 2 | | L298N IN2 | 数字 | 3 |

视频介绍

代码

#include&nbsp;<Servo.h>

// 定义引脚
constint&nbsp;INFRARED_SENSOR =&nbsp;6; &nbsp;// 红外传感器连接到数字引脚6
constint&nbsp;SERVO_PIN =&nbsp;5; &nbsp; &nbsp; &nbsp; &nbsp;// 舵机连接到数字引脚5
constint&nbsp;RAIN_SENSOR = A0; &nbsp; &nbsp;&nbsp;// 雨滴传感器连接到模拟引脚A0
constint&nbsp;LIGHT_SENSOR = A1; &nbsp; &nbsp;// 光照传感器连接到模拟引脚A1
constint&nbsp;MOTOR_IN1 =&nbsp;2; &nbsp; &nbsp; &nbsp; &nbsp;// L298N IN1连接到数字引脚2
constint&nbsp;MOTOR_IN2 =&nbsp;3; &nbsp; &nbsp; &nbsp; &nbsp;// L298N IN2连接到数字引脚3

// 定义阈值
constint&nbsp;LIGHT_THRESHOLD =&nbsp;500; &nbsp;// 光照阈值,可根据实际环境调整
constint&nbsp;RAIN_THRESHOLD =&nbsp;500; &nbsp;&nbsp;// 雨滴阈值,可根据实际环境调整

// 定义状态
bool&nbsp;isClotheslineOut =&nbsp;false; &nbsp; &nbsp;// 晾衣架是否已伸出
bool&nbsp;isRaining =&nbsp;false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// 是否检测到雨
int&nbsp;servoPosition =&nbsp;0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 舵机当前角度
unsignedlong&nbsp;rainStopTime =&nbsp;0; &nbsp;&nbsp;// 雨停时间
constunsignedlong&nbsp;RAIN_DELAY =&nbsp;3000; &nbsp;// 雨后延迟伸出时间(毫秒)
bool&nbsp;initialCheckDone =&nbsp;false; &nbsp; &nbsp;// 初始化检查完成标志

Servo servo; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 创建舵机对象

void&nbsp;setup()&nbsp;{
// 初始化引脚模式
&nbsp; pinMode(INFRARED_SENSOR, INPUT);
&nbsp; pinMode(RAIN_SENSOR, INPUT);
&nbsp; pinMode(LIGHT_SENSOR, INPUT);
&nbsp; pinMode(MOTOR_IN1, OUTPUT);
&nbsp; pinMode(MOTOR_IN2, OUTPUT);

// 初始化舵机
&nbsp; servo.attach(SERVO_PIN);
&nbsp; servo.write(servoPosition);

// 初始化电机状态
&nbsp; digitalWrite(MOTOR_IN1, LOW);
&nbsp; digitalWrite(MOTOR_IN2, LOW);

// 启动串口通信(用于调试)
&nbsp; Serial.begin(9600);
&nbsp; Serial.println("智能晾衣架系统已启动,正在进行初始化检查...");

// 设备开机初始化检查
&nbsp; initializeSystem();
}

void&nbsp;loop()&nbsp;{
// 检测是否下雨
&nbsp; detectRain();

// 只有在初始化检查完成后才控制晾衣架
if&nbsp;(initialCheckDone) {
&nbsp; &nbsp;&nbsp;// 第一优先级:光照检测控制晾衣架(考虑雨水状态)
&nbsp; &nbsp; controlClotheslineByLight();

&nbsp; &nbsp;&nbsp;// 第三优先级:人体检测控制舵机(独立功能)
&nbsp; &nbsp; controlServoByHuman();
&nbsp; }

&nbsp; delay(100); &nbsp;// 短暂延时,避免循环过快
}

// 设备开机初始化检查
void&nbsp;initializeSystem()&nbsp;{
// 读取当前光照值
int&nbsp;lightValue = analogRead(LIGHT_SENSOR);
&nbsp; Serial.print("初始化检查 - 光照值: ");
&nbsp; Serial.println(lightValue);

// 读取当前雨水值
int&nbsp;rainValue = analogRead(RAIN_SENSOR);
&nbsp; Serial.print("初始化检查 - 雨水值: ");
&nbsp; Serial.println(rainValue);

// 判断是否应该伸出晾衣架
if&nbsp;(lightValue > LIGHT_THRESHOLD && rainValue >= RAIN_THRESHOLD) {
&nbsp; &nbsp; Serial.println("初始化检查 - 光照充足且无雨,伸出晾衣架");
&nbsp; &nbsp; runMotorForward(4000); &nbsp;// 正转4秒
&nbsp; &nbsp; isClotheslineOut =&nbsp;true;
&nbsp; }&nbsp;else&nbsp;{
&nbsp; &nbsp; Serial.println("初始化检查 - 条件不满足,保持晾衣架收回状态");
&nbsp; &nbsp; isClotheslineOut =&nbsp;false;
&nbsp; }

// 记录雨停时间(如果未检测到雨)
if&nbsp;(rainValue >= RAIN_THRESHOLD) {
&nbsp; &nbsp; isRaining =&nbsp;false;
&nbsp; &nbsp; rainStopTime = millis();
&nbsp; }&nbsp;else&nbsp;{
&nbsp; &nbsp; isRaining =&nbsp;true;
&nbsp; }

// 标记初始化检查完成
&nbsp; initialCheckDone =&nbsp;true;
&nbsp; Serial.println("初始化检查完成,开始正常运行");
}

// 检测是否下雨
void&nbsp;detectRain()&nbsp;{
int&nbsp;rainValue = analogRead(RAIN_SENSOR);

if&nbsp;(rainValue < RAIN_THRESHOLD) { &nbsp;// 检测到雨水
&nbsp; &nbsp;&nbsp;if&nbsp;(!isRaining) {
&nbsp; &nbsp; &nbsp; isRaining =&nbsp;true;
&nbsp; &nbsp; &nbsp; Serial.println("检测到雨水");

&nbsp; &nbsp; &nbsp;&nbsp;// 如果晾衣架伸出,立即收回
&nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(isClotheslineOut) {
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("检测到雨水,收回晾衣架");
&nbsp; &nbsp; &nbsp; &nbsp; runMotorBackward(4000); &nbsp;// 反转4秒
&nbsp; &nbsp; &nbsp; &nbsp; isClotheslineOut =&nbsp;false;
&nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
&nbsp; }&nbsp;else&nbsp;{ &nbsp;// 雨停了
&nbsp; &nbsp;&nbsp;if&nbsp;(isRaining) {
&nbsp; &nbsp; &nbsp; isRaining =&nbsp;false;
&nbsp; &nbsp; &nbsp; rainStopTime = millis(); &nbsp;// 记录雨停时间
&nbsp; &nbsp; &nbsp; Serial.println("雨已停,记录时间");
&nbsp; &nbsp; }
&nbsp; }
}

// 光照控制晾衣架(考虑雨水状态)
void&nbsp;controlClotheslineByLight()&nbsp;{
int&nbsp;lightValue = analogRead(LIGHT_SENSOR);
&nbsp; Serial.print("光照值: ");
&nbsp; Serial.println(lightValue);

if&nbsp;(lightValue > LIGHT_THRESHOLD) { &nbsp;// 光照充足
&nbsp; &nbsp;&nbsp;if&nbsp;(!isClotheslineOut) {
&nbsp; &nbsp; &nbsp;&nbsp;// 只有在以下条件都满足时才伸出晾衣架:
&nbsp; &nbsp; &nbsp;&nbsp;// 1. 没有在下雨
&nbsp; &nbsp; &nbsp;&nbsp;// 2. 雨停后已经过了足够的时间(避免短时阵雨反复触发)
&nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(!isRaining && (millis() - rainStopTime > RAIN_DELAY)) {
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("光照充足,且雨停足够长时间,伸出晾衣架");
&nbsp; &nbsp; &nbsp; &nbsp; runMotorForward(4000); &nbsp;// 正转4秒
&nbsp; &nbsp; &nbsp; &nbsp; isClotheslineOut =&nbsp;true;
&nbsp; &nbsp; &nbsp; }&nbsp;elseif&nbsp;(isRaining) {
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println("光照充足,但仍在下雨,不伸出晾衣架");
&nbsp; &nbsp; &nbsp; }&nbsp;else&nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; Serial.print("光照充足,但雨停时间不足 ");
&nbsp; &nbsp; &nbsp; &nbsp; Serial.print((RAIN_DELAY - (millis() - rainStopTime)) /&nbsp;1000);
&nbsp; &nbsp; &nbsp; &nbsp; Serial.println(" 秒,不伸出晾衣架");
&nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
&nbsp; }&nbsp;else&nbsp;{ &nbsp;// 光照弱,收回晾衣架(无论是否下雨)
&nbsp; &nbsp;&nbsp;if&nbsp;(isClotheslineOut) {
&nbsp; &nbsp; &nbsp; Serial.println("光照弱,收回晾衣架");
&nbsp; &nbsp; &nbsp; runMotorBackward(4000); &nbsp;// 反转4秒
&nbsp; &nbsp; &nbsp; isClotheslineOut =&nbsp;false;
&nbsp; &nbsp; }
&nbsp; }
}

// 人体检测控制舵机
void&nbsp;controlServoByHuman()&nbsp;{
int&nbsp;humanDetected = digitalRead(INFRARED_SENSOR);

if&nbsp;(humanDetected == HIGH) { &nbsp;// 检测到人
&nbsp; &nbsp;&nbsp;if&nbsp;(servoPosition ==&nbsp;0) {
&nbsp; &nbsp; &nbsp; Serial.println("检测到人,舵机转动到80度");
&nbsp; &nbsp; &nbsp; servo.write(80);
&nbsp; &nbsp; &nbsp; servoPosition =&nbsp;80;
&nbsp; &nbsp; &nbsp; delay(1000); &nbsp;// 等待舵机转动到位
&nbsp; &nbsp; }
&nbsp; }&nbsp;else&nbsp;{ &nbsp;// 人离开
&nbsp; &nbsp;&nbsp;if&nbsp;(servoPosition ==&nbsp;80) {
&nbsp; &nbsp; &nbsp; Serial.println("人已离开,舵机回到0度");
&nbsp; &nbsp; &nbsp; servo.write(0);
&nbsp; &nbsp; &nbsp; servoPosition =&nbsp;0;
&nbsp; &nbsp; &nbsp; delay(1000); &nbsp;// 等待舵机转动到位
&nbsp; &nbsp; }
&nbsp; }
}

// 电机正转函数
void&nbsp;runMotorForward(int&nbsp;duration)&nbsp;{
&nbsp; digitalWrite(MOTOR_IN1, HIGH);
&nbsp; digitalWrite(MOTOR_IN2, LOW);
&nbsp; delay(duration);
&nbsp; digitalWrite(MOTOR_IN1, LOW);
&nbsp; digitalWrite(MOTOR_IN2, LOW);
}

// 电机反转函数
void&nbsp;runMotorBackward(int&nbsp;duration)&nbsp;{
&nbsp; digitalWrite(MOTOR_IN1, LOW);
&nbsp; digitalWrite(MOTOR_IN2, HIGH);
&nbsp; delay(duration);
&nbsp; digitalWrite(MOTOR_IN1, LOW);
&nbsp; digitalWrite(MOTOR_IN2, LOW);
}

更多好玩项目 关注我们


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:kali笔记 大表哥吆 大表哥吆《Arduino智能衣架 可做毕设 (已开源)》

AI正在重构网络安全行业 网络安全文章

AI正在重构网络安全行业

文章总结: 本文探讨AI如何重构网络安全行业,指出攻击方已利用AI实现分钟级自动化渗透,而防御方借助AI提升告警处理、漏洞管理和威胁溯源效率。文章强调行业竞争核
评论:0   参与:  0