当前位置: 首页 > 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);

  }

}

}

实验步骤

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

实验结果

思考与反馈

http://www.lryc.cn/news/395761.html

相关文章:

  • pytorch通过 tensorboardX 调用 Tensorboard 进行可视化
  • linux查看目录下的文件夹命令,find 查找某个目录,但是不包括这个目录本身?
  • 单一设备上的 2 级自动驾驶:深入探究 Openpilot 的奥秘
  • 向github远程仓库中push,要求使用token登录
  • 最全windows提权总结(建议收藏)
  • Could not find Chrome (ver.xxxxx). This can occur if either\n
  • Conmi的正确答案——ESP32-C3开启安全下载模式
  • 从零开始实现大语言模型(一):概述
  • 科普文本分类背后的数学原理——最新版《数学之美》第14、15章读书笔记
  • 华为云生态和快速入门
  • 卷积神经网络——LeNet——FashionMNIST
  • k8s-第十二节-DaemonSet
  • Mysql-内置函数
  • 新浪API系列:支付API打造无缝支付体验,畅享便利生活(3)
  • 终于弄明白了什么是EI!
  • 微信小程序常见页面跳转方式
  • Vim常用整理快捷键
  • 【docker 把系统盘空间耗没了!】windows11 更改 ubuntu 子系统存储位置
  • 前端如何让网页页面完美适配不同大小和分辨率屏幕
  • gitlab-runner安装部署CI/CD
  • 数据分析案例-2024 年全电动汽车数据集可视化分析
  • H桥驱动器芯片详解
  • 哪个充电宝口碑比较好?怎么选充电宝?2024年口碑优秀充电宝推荐
  • Memcached 介绍与详解及在Java Spring Boot项目中的使用与集成
  • 淮北在选择SCADA系统时,哪些因素会影响其稳定性?
  • Linux: 命令行参数和环境变量究竟是什么?
  • 数学系C++ 类与对象 STL(九)
  • CSS技巧专栏:一日一例 2.纯CSS实现 多彩边框按钮特效
  • JCEF 在idea 开发 java 应用
  • 绝区伍--2024年AI发展路线图