hdu物联网硬件实验3 按键和中断

|---------|---|----|---|----|---|
| 学院 | | 班级 | | 学号 | |
| 姓名 | | 日期 | | 成绩 | |
| 实验题目 | 按键和中断 |||||
| ||||||
| 实验目的 | 实现闪灯功能转换 |||||
| 硬件原理 | 无 |||||
| 关键代码及注释 | /* Button Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 2. The circuit: * LED attached from pin 13 to ground * pushbutton attached to pin 2 from +3.3V * 10K resistor attached to pin 2 from ground * Note: on most Arduinos there is already an LED on the board attached to pin 13. created 2005 by DojoDave <http://www.0j0.org> modified 30 Aug 2011 by Tom Igoe modified Apr 27 2012 by Robert Wessels This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button */ // constants won't change. They're used here to // set pin numbers: const int buttonPin = PUSH2; // the number of the pushbutton pin const int ledPin1 = GREEN_LED; // the number of the LED pin const int ledPin2 = YELLOW_LED; const int ledPin3 = RED_LED; int ledState = LOW; int ledState1 = LOW; int ledState2 = LOW; int ledState3 = LOW; volatile bool state = HIGH; int count = 0; // variables will change: int buttonState = 0; // variable for reading the pushbutton status long previousMillis = 0; // will store last time LED was updated long term = 0; // the follow variables is a long because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long interval = 1000; void setup() { // initialize the LED pin as an output: pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); //digitalWrite(ledPin,state); pinMode(buttonPin,INPUT_PULLUP); attachInterrupt (buttonPin ,blink ,RISING); } void blink(){ state = !state; } void loop(){ unsigned long currentMillis = millis(); if(state==HIGH){ if(currentMillis - previousMillis > interval) { // save the last time you blinked the LED previousMillis = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState == LOW) ledState = HIGH; else{ ledState = LOW; } // set the LED with the ledState of the variable: digitalWrite(ledPin1, ledState); digitalWrite(ledPin2, ledState); digitalWrite(ledPin3, ledState); } } else{ if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; if (ledState1 == LOW && ledState2 == LOW && ledState3 == LOW) ledState1 = HIGH; else if(ledState1 == HIGH){ ledState1 = LOW; ledState2 = HIGH; } else if(ledState2 == HIGH){ ledState2 = LOW; ledState3 = HIGH; } else if(ledState3 == HIGH){ ledState3 = LOW; ledState1 = HIGH; } digitalWrite(ledPin1, ledState1); digitalWrite(ledPin2, ledState2); digitalWrite(ledPin3, ledState3); } } } |||||
| 实验步骤 | 在上次作业基础上加上了按钮,中断 |||||
| 实验结果 | |||||
| 思考与反馈 | 无 |||||

相关推荐
雪兽软件4 小时前
物联网 (IoT) 将如何改变我们的工作方式
物联网
才盛智能科技5 小时前
K链通×才盛云:自助KTV品牌从0到1孵化超简单
大数据·人工智能·物联网·自助ktv系统·才盛云
三佛科技-134163842128 小时前
宠物洗澡打泡机方案,宠物泡泡机MCU方案开发设计分享
单片机·嵌入式硬件·物联网·智能家居·pcb工艺·宠物
WZGL12309 小时前
“十五五”发展展望:以社区为底座构建智慧康养服务
大数据·人工智能·物联网
北京耐用通信11 小时前
耐达讯自动化Profinet转Devicenet网关:精细化工行业的“协议融合利器”
人工智能·物联网·网络协议·自动化·信息与通信
新新学长搞科研12 小时前
【智慧城市专题IEEE会议】第六届物联网与智慧城市国际学术会议(IoTSC 2026)
人工智能·分布式·科技·物联网·云计算·智慧城市·学术会议
牵牛老人12 小时前
Qt中集成 MQTT 来实现物联网通信:从原理到实战全解析
开发语言·qt·物联网
上海合宙LuatOS12 小时前
LuatOS ——Modbus RTU 通信模式
java·linux·服务器·开发语言·网络·嵌入式硬件·物联网
国产化创客12 小时前
ESPHome 核心原理+全流程开发与集成
物联网·智能家居·智能硬件
想放学的刺客13 小时前
单片机嵌入式试题(第30期)全局变量“满天飞“!!!局限性和影响有哪些,什么情况下才不得不使用?
单片机·嵌入式硬件·mcu·物联网·51单片机