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

相关推荐
云山工作室2 小时前
基于物联网的风机故障检测装置的设计与实现
单片机·物联网·毕业设计·毕设
『往事』&白驹过隙;7 小时前
操作系统(Linux Kernel 0.11&Linux Kernel 0.12)解读整理——内核初始化(main & init)之硬盘初始化
linux·c语言·数据结构·物联网·操作系统
亿坊电商7 小时前
基于亿坊PHP框架构建物联网解决方案的优势分析!
开发语言·物联网·php
云上的阿七1 天前
云计算如何与物联网(IoT)结合?
物联网·云计算
7yewh1 天前
嵌入式知识点总结 ARM体系与架构 专题提升(二)-ARM处理器
arm开发·stm32·单片机·嵌入式硬件·mcu·物联网·51单片机
7yewh1 天前
嵌入式知识点总结 ARM体系与架构 专题提升(三)-中断与异常
arm开发·stm32·单片机·嵌入式硬件·mcu·物联网·51单片机
中科岩创2 天前
广东某海水取排水管线工程边坡自动化监测
大数据·物联网
fan_0002 天前
LKT4304新一代算法移植加密芯片,守护 物联网设备和云服务安全
物联网·安全
7yewh2 天前
嵌入式知识点总结 ARM体系与架构 专题提升(四)-编程
arm开发·stm32·单片机·嵌入式硬件·mcu·物联网·51单片机
『往事』&白驹过隙;2 天前
操作系统(Linux Kernel 0.11&Linux Kernel 0.12)解读整理——内核初始化(main & init)之缓冲区的管理
linux·c语言·数据结构·物联网·操作系统