当前位置: 首页 > news >正文

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);

  }

}

}

实验步骤

在上次作业基础上加上了按钮,中断

实验结果

思考与反馈

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 解码技术债:AI代码助手与智能体的革新之道
  • PostgreSQL的使用
  • 【续集】Java之父的退休之旅:从软件殿堂到多彩人生的探索
  • QT学习积累——如何提高Qt遍历list的效率
  • 从GPT-1到GPT-3 预训练语言模型的演进与突破
  • 【INTEL(ALTERA)】为什么我使用 PIO 边缘捕获中断的 Nios® II 设计不能正常工作?
  • 灵活多变的对象创建——工厂方法模式(Python实现)
  • 怎么将几首音乐合并在一起?这四种合并方法大家都在用!
  • 【PyQt5】
  • Mac电脑上有什么好玩的肉鸽游戏推荐 苹果电脑怎么玩以撒的结合
  • 【LeetCode】每日一题:跳跃游戏 II
  • CSS学习
  • gsplat中的3D Gaussian Splatting as Markov Chain Monte Carlo的代码解读
  • Python面试题:在 Python 中,如何使用 `functools` 模块?
  • JS常用正则校验大全
  • 2017 前端面试准备 - 收藏集 - 掘金
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • Create React App 使用
  • CSS魔法堂:Absolute Positioning就这个样
  • Django 博客开发教程 8 - 博客文章详情页
  • eclipse(luna)创建web工程
  • Fastjson的基本使用方法大全
  • input实现文字超出省略号功能
  • JavaScript设计模式系列一:工厂模式
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • Lucene解析 - 基本概念
  • Material Design
  • mongodb--安装和初步使用教程
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • SwizzleMethod 黑魔法
  • WinRAR存在严重的安全漏洞影响5亿用户
  • 爱情 北京女病人
  • 聊聊flink的TableFactory
  • 数据仓库的几种建模方法
  • ​埃文科技受邀出席2024 “数据要素×”生态大会​
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • (1综述)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (PyTorch)TCN和RNN/LSTM/GRU结合实现时间序列预测
  • (ZT)出版业改革:该死的死,该生的生
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (搬运以学习)flask 上下文的实现
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)ssm考试题库管理系统 毕业设计 069043
  • (三)docker:Dockerfile构建容器运行jar包
  • (十一)图像的罗伯特梯度锐化
  • (四)docker:为mysql和java jar运行环境创建同一网络,容器互联
  • (转)原始图像数据和PDF中的图像数据
  • (最简单,详细,直接上手)uniapp/vue中英文多语言切换
  • *p++,*(p++),*++p,(*p)++区别?
  • .Net CF下精确的计时器
  • .NET delegate 委托 、 Event 事件,接口回调
  • .NET企业级应用架构设计系列之开场白
  • .NET轻量级ORM组件Dapper葵花宝典