Arduino初学者-Digital例程

前言

例程均为官方提供,非常适合用来入门arduino。这篇帖子用来记录下自己学习例程的点滴。
参考:
官方例程
官方函数

Debouce(防按键抖动)

  • 接线图示

  • 按键:按下后4个引脚全短接,主要有一下两种按键。在这里插入图片描述
    在这里插入图片描述

  • 官方代码如下:

    
    const int buttonPin = 2;    // the number of the pushbutton pin
    const int ledPin = 13;      // the number of the LED pin
    
    // Variables will change:
    int ledState = HIGH;         // the current state of the output pin
    int buttonState;             // the current reading from the input pin
    int lastButtonState = LOW;   // the previous reading from the input pin
    
    // the following variables are unsigned longs because the time, measured in
    // milliseconds, will quickly become a bigger number than can be stored in an int.
    unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
    unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
    
    void setup() {
      pinMode(buttonPin, INPUT); //设定Pin2为输入
      pinMode(ledPin, OUTPUT);  //设定Pin13为输出(系统自带Led,输出模式下内部给1它就亮了)
    
      // set initial LED state
      digitalWrite(ledPin, ledState); //Pin 13置1
    }
    
    void loop() {
      // read the state of the switch into a local variable:
      int reading = digitalRead(buttonPin); //digitalRead()只返回0/1;analogread()返回0~1023.此处按下按键为1
    
      // check to see if you just pressed the button
      // (i.e. the input went from LOW to HIGH), and you've waited long enough
      // since the last press to ignore any noise:
    
      // If the switch changed, due to noise or pressing:
      if (reading != lastButtonState) {  //Pin2脚不为0
        // reset the debouncing timer
        lastDebounceTime = millis();//记录从运行开始到这步的时间
      }
    
      if ((millis() - lastDebounceTime) > debounceDelay) {// 按键震动时必然<50,这里是一直等到按键震动结束后才开始执行条件内容。
        // whatever the reading is at, it's been there for longer than the debounce
        // delay, so take it as the actual current state:
    
        // if the button state has changed:
        if (reading != buttonState) { //更新按键状态,1为按下。
          buttonState = reading;
    
          // only toggle the LED if the new button state is HIGH
          if (buttonState == HIGH) { //切换Pin13的状态,这儿变量是0/1,不是ledPin(存放引脚号)
            ledState = !ledState;
          }
        }
      }
    
      // set the LED:
      digitalWrite(ledPin, ledState);
    
      // save the reading. Next time through the loop, it'll be the lastButtonState:
      lastButtonState = reading;
    }
    
  • 结果:按一次换一次灯(此程序重在去抖)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Arduino OLED 程是一种可以在Arduino开发板上控制OLED显示屏的程序代码。这个程是基于Arduino库的,通过在代码中调用库函数来实现与OLED显示屏的通信和控制。 使用Arduino OLED程的第一步是将OLED显示屏连接到Arduino开发板。一般来说,我们需要将OLED显示屏的VCC引脚连接到Arduino的5V引脚,GND引脚连接到Arduino的GND引脚,SCL引脚连接到Arduino的A5引脚,SDA引脚连接到Arduino的A4引脚。 在代码中,我们需要包含必要的库文件,如Adafruit_SSD1306.h库和Adafruit_GFX.h库,这些库提供了适用于OLED显示屏的函数和方法。 接下来,我们需要创建一个Adafruit_SSD1306对象,并在设置函数中指定OLED显示屏的宽度和高度。然后,通过调用begin函数初始化显示屏。 在主循环中,我们可以使用一系列函数来控制OLED显示屏的各种功能。如,我们可以使用clearDisplay函数清除显示屏,使用setTextSize函数设置文本大小,使用setTextColor函数设置文本颜色,使用setCursor函数设置文本光标位置,然后使用print函数在显示屏上显示文本。 除了文本,我们还可以在OLED显示屏上显示图形,如圆形、矩形和直线。只需调用相应的函数,设置参数即可。 总之,Arduino OLED 程是一种通过编写代码来控制OLED显示屏的方法。通过使用适当的库和函数,我们可以实现各种功能,包括显示文本和图形。这是一种简单而灵活的方法,可以根据需要自定义OLED显示屏的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值