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); } } } |||||
| 实验步骤 | 在上次作业基础上加上了按钮,中断 |||||
| 实验结果 | |||||
| 思考与反馈 | 无 |||||

相关推荐
小马同志( ̄^ ̄)ゞ14 分钟前
物联网新闻2024.09.16-2024.09.22
物联网
启明云端wireless-tag17 分钟前
ESP32无线WiFi蓝牙SOC,设备物联网通信方案,启明云端乐鑫代理商
嵌入式硬件·物联网·wifi·esp32·乐鑫·wifi模组
鸽子汤1972 小时前
想高效开发?从文件系统开始着手。。。
嵌入式硬件·物联网·硬件工程
钡铼技术4 小时前
通过iFIX在ARMxy边缘计算网关上实现维护管理
人工智能·物联网·边缘计算·钡铼技术·armxy边缘计算网关
华清远见IT开放实验室7 小时前
【项目案例】物联网比较好的10+练手项目推荐,附项目文档/源码/视频
物联网·音视频
limingade7 小时前
手机实时提取SIM卡打电话的信令和声音-新的篇章(一、可行的方案探讨)
物联网·算法·智能手机·数据分析·信息与通信
思为无线NiceRF14 小时前
全双工多路并发、低延时数传解决行业信号拥堵问题
物联网
东华果汁哥21 小时前
【嵌入式人工智能】嵌入式AI在物联网中如何应用
人工智能·物联网
启明云端wireless-tag1 天前
设备稳定流畅视频体验,乐鑫ESP32-S3芯片方案无线音视频传输通信
物联网·音视频·交互·乐鑫·wifi模组
神一样的老师1 天前
近乎实时的物联网数据管道架构
物联网·架构