Arduino 内置示例——数字部分示例

内置示例的数字部分,包含如下实验例程:


image.png
  • 不用delay的闪烁:不用delay()函数来闪烁一个LED灯
  • 按键:使用一个按键来控制一个LED灯
  • 防抖:读取一个按键,然后滤掉噪音
  • 数字输入上拉:用pinMode()来声明输出上拉
  • 侦察状态改变:计算按键按下的次数
  • 音调键盘:一个使用压力传感器和压电扬声器的三键音乐键盘
  • 音调旋律:用压力扬声器弹奏一个旋律
  • 多重音调:利用tone()命令使多个扬声器发出声音
  • 高音追随:根据一个模拟输入来决定压力扬声器的音调

不用delay的闪烁

有时候你需要同时做两件事。如你可能想闪烁一个LED灯,同时读取一个按键。这种情况下你不能使用delay()。如果Arduino因为delay()函数被暂停时有按键按下,你的程序会错过这次的按键按下。

实现流程:
这个程序示范怎样不用delay()来闪烁一个LED灯。它打开LED灯,并标注一个时间。然后每次运行完loop(),它都会查一下有没有超过需要的闪烁时间。如果它超过了,它就会切换LED灯为打开或者关闭状态,然后标注新的时间。在这种方法里LED灯不断闪烁,同时这个程序的执行从来没有漏掉一个指令。

追根寻底这个例程教会我们怎样设置一个简单的定时器。

先连接电阻的一端到Arduino的PIN13引脚。再连接LED灯的长引脚(正脚,也叫阳极)到电阻的另一端。最后连接LED灯的短引脚(负脚,也叫负极)到Arduino的GND引脚。如图所示


image.png

大部分Arduino开发板上面就有一个LED灯连接到PIN13。如果你不连接硬件就运行这个例子,你应该也可以看到LED闪烁。

不用delay的闪烁.gif

示例代码:

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  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(ledPin, ledState);
  }
}

按键

当你按下按键或者开关时,它们会连接电路的两点。这篇文章举例了当你按下按键时,怎么打开pin13的内置LED灯

image.png

连接3根线到开发板。最开始两根,红和黑,连接到面包板上的两个长垂直行来提供5V电源电压和地。第三根线从数字引脚pin2连接到按钮的一个引脚。按钮的同一个引脚连接下拉电阻(10k ohm)到地。按钮的另一个引脚连接到5V电源。

按钮或者开关连接电路的两点。按钮是断开的(未按),按钮两个引脚是没有接通的,所以这个引脚连接到地(通过一个下拉电阻),读取为低电平或者0。当如果按钮是闭合的(未按),按钮两个引脚是接通的,所以这个引脚连接到5V,读取为高电平,或者1。

当按钮是断开的(没有按下),按钮两个引脚是没有接通的,所以你也可以反方向连接这个电路,上拉电阻使输入引脚为高电平,当按下按钮时引脚变为低电平。如果这样做,程序应该反过来,当你按下按键时,LED正常发光或者熄灭。

如果你没有连接到数字I/O口到任何地方,LED灯可能会不规则闪烁。这是因为输入引脚处于悬浮状态——它没有固定连接到电源或者地,并且它会随机在高电平和低电平之间切换。这是你需要下拉电阻的原因。

这里电源线和底线也可以如下简单连接


按键.gif

示例代码:

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

防抖

当按下时,按键经常产生错误的开/关变迁,这是机械物理的问题:这些变迁可能被读取为在短时间内多次按下,从而使程序变笨。这个例子示范了怎样使一个输入防抖,这意味着在一个短时间内检查两次来确保这个按键是确实被按下了。如果没有防抖,按一次这个按键可能会引起不可预测的结果。这个程序利用millis()函数来保持按下的时间间隔。


image.png

以下程序基于Limor Fried的防抖版本,但是她的例子的逻辑是反过来的。在她的例子里,当闭合时开关为低电平,而断开时开关未高电平。这里当按下开关时为高电平,没有按下时为低电平。

示例代码:

// constants won't change. They're used here to set pin numbers:
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);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // 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) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // 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) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

数字输入上拉

这个例子示范了用pinMode()来上拉输入引脚。在你的Arduino和电脑之间创建一个串口通讯来监视开关的状态。

总的来说,当输入为高电平,开发板上pin13的LED灯将会被打开;而为低电平时,这个LED灯将会熄灭。


image.png

连接两根线到Arduino开发板。黑色线把地和按键的一个引脚连在一起。第二根线连接数字引脚pin 2到按钮的另一个引脚。

当你按下时,按钮或者开关连接电路的两点。按钮是断开的(未按),按钮两个引脚是没有接通的。因为pin2的内置上拉是正极且连接到5V,所以当按钮断开时我们读到的是高电平。当按钮是闭合的,因为pin2连接到地,所以Arduino读到低电平.

image.png
image.png
image.png
image.png

示例代码:

void setup() {
  //开启串口,设置波特率为9600 bits
  Serial.begin(9600);
  //初始化数字引脚pin2,因为你要读取按钮的输出
  pinMode(2, INPUT_PULLUP);
  //把作为LED灯的pin13初始化为输出引脚
  pinMode(13, OUTPUT);

}
/*
初始化完成。当按钮被按下,5V电压会流过你的电路,而当它没有被按下,这个输入引脚就会链接到通过10k ohm电阻连接到地。这是数字输入,意味着开关只有开(1,或者高电平)和关(0,或者低电平)的状态,中间什么都没有。
*/

void loop() {
  //读取开关按键信息
  int sensorVal = digitalRead(2);
  //将信息输出值串口终端
  Serial.println(sensorVal);

/*当你打开Arduino IDE的串口监视器,你会看见“0”的数据流(如果开关打开)或者“1”的数据流(如果开关闭合)
当开关为高电平时,pin13的LED灯会变亮;开关为低电平时,LED灯熄灭
*/
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);
  }
}

侦察状态改变

也可以叫做边沿监测

如果你让按键工作,你经常会想做一些要按下很多次按钮的才有响应的动作。这时你需要知道按钮的从闭合到断开的变化状态,然后记录这个状态发生了多少次。这叫状态变化检测或者边沿检测。在这个教程里我们学习怎样检查状态变化,然后我们把相关信息发送到串口监视器里,并记录4次LED灯的开和关。


image.png

连接3根线到开发板。最开始两根,红和黑,连接到面包板上的两个长垂直行来提供5V电源电压和地。第三根线从数字引脚pin2连接到按钮的一个引脚。按钮的同一个引脚连接下拉电阻(10k ohm)到地。按钮的另一个引脚连接到5V电源。

按钮或者开关连接电路的两点。按钮是断开的(未按),按钮两个引脚是没有接通的,所以这个引脚连接到地(通过一个下拉电阻),读取为低电平或者0。当如果按钮是闭合的(未按),按钮两个引脚是接通的,所以这个引脚连接到5V,读取为高电平,或者1。

如果你没有连接到数字I/O口到任何地方,LED灯可能会不规则闪烁。这是因为输入引脚处于悬浮状态——它没有固定连接到电源或者地,并且它会随机在高电平和低电平之间切换。这是你需要下拉电阻的原因。

示例代码:

下面的代码连续读取按钮的状态。然后通过循环来对比这个按钮的状态和它上一段时间的状态。如果当前按键的状态和之前的状态不一样并且当前状态是高电平,那么这个按键刚从关变为开。然后程序增加按键按下的计数器。

这个程序也检查按键按下次数的值,并且如果它是4的倍数,它会打开pin13的LED灯,否则这个LED灯饰熄灭的。

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}
image.png

image.png

音调键盘

这个例子展示怎么用tone()命令根据按下的传感器来产生不同的音调。

把扬声器的一端通过一个100 ohm电阻连接到数据引脚pin8,并且把它的另一端接地。

用5V电源为你的三个FSRS(或者其他模拟传感器)并行供电。把传感器分别连到模拟pin0-2上,并且每一个输入都用一个10k电阻下拉到地。


image.png

下面程序读取三个模拟传感器,每个都在数组里匹配一个数值。如果任何传感器都高于给定的阈值,对应的声音就会响起。

程序用一个额外的库文件:pitches.h。这个库文件包括所有典型的音调值。举个例子,NOTE_C4是middle C。NOTE_FS4是F sharp,等等。这个tone()命令表格是由Brett Hagman写的。无论什么时候你想制作一些音调,你会觉得它很有用。

为了制作pitches.h库文件,点到下图的按钮并选择"New Tab",或者用快捷键Ctrl+Shift+N.

示例代码:

#include "pitches.h"

const int threshold = 10;    // minimum reading of the sensors that generates a note

// notes to play, corresponding to the 3 sensors:
int notes[] = {
  NOTE_A4, NOTE_B4, NOTE_C3
};

void setup() {

}

void loop() {
  for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
    // get a sensor reading:
    int sensorReading = analogRead(thisSensor);

    // if the sensor is pressed hard enough:
    if (sensorReading > threshold) {
      // play the note corresponding to this sensor:
      tone(8, notes[thisSensor], 20);
    }
  }
}

手头没有压力的器件,这里先不演示了。


音调旋律

用tone()函数弹奏一个旋律

这个例子展示怎么用tone()命令来产生音乐。它弹奏一小段你可能听过的旋律。


image.png

示例代码:

程序用一个额外的库文件:pitches.h。这个库文件包括所有典型的音调值。举个例子,NOTE_C4是middle C。NOTE_FS4是F sharp,等等。这个tone()命令表格是由Brett Hagman写的。无论什么时候你想制作一些音调,你会觉得它很有用。

#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

会有一段音乐通过蜂鸣器响起。


多重音调

用tone()函数在多个扬声器上弹奏音调

这个例子展示怎样用tone()命令在多个输出上弹奏不同的音调。

tone()命令是通过Atmega的内置定时器来工作的,设置你想要的频率,并且用定时器来产生一个输出脉冲。因为它只用一个定时器,所以你只能一个定时器弹奏一个音调。然而你可以按顺序地在不同的引脚上弹奏音调。为了达到这个目的,你需要在移到下一个引脚前关闭这个引脚的定时器。

image.png

示例代码:
程序在按顺序弹奏每一个扬声器的音调之前先关闭前一个扬声器。每个音调持续时间的注释和它后面的延时是一样的。

void setup() {

}

void loop() {
  // turn off tone function for pin 8:
  noTone(8);
  // play a note on pin 6 for 200 ms:
  tone(6, 440, 200);
  delay(200);

  // turn off tone function for pin 6:
  noTone(6);
  // play a note on pin 7 for 500 ms:
  tone(7, 494, 500);
  delay(500);

  // turn off tone function for pin 7:
  noTone(7);
  // play a note on pin 8 for 300 ms:
  tone(8, 523, 300);
  delay(300);
}

高音追随

用tone()函数来高音追随

这个例子展示怎么用tone()命令来产生一个模拟输入的音调。用上光敏电阻器,你的Arduino或Genuino开发板会变成一个简单的轻电子琴。


image.png

把扬声器的一段通过一个100 ohm电阻连接到数字引脚pin9,而另一端连接到地。用5V为光敏电阻提供电源,然后把它的另一端连接到模拟引脚A0,并在上面加上一个4.7k下拉电阻(连接到地)。

示例代码:

这个例子的代码是很简单的。只是把一个模拟输入和它的值放到在可听见范围的音调。人类能听到20-20,000Hz的声音,而这个程序最好工作在120-1.500Hz会比较好点。

你需要获得你模拟输入值的实际范围。在所示电路里,模拟输入值范围在400-1000之间。用map()命令来改变这个值来匹配你传感器的范围。

void setup() {
  // initialize serial communications (for debugging only):
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // print the sensor reading so you know its range
  Serial.println(sensorReading);
  // map the analog input range (in this case, 400 - 1000 from the photoresistor)
  // to the output pitch range (120 - 1500Hz)
  // change the minimum and maximum input numbers below depending on the range
  // your sensor's giving:
  int thisPitch = map(sensorReading, 400, 1000, 120, 1500);

  // play the pitch:
  tone(9, thisPitch, 10);
  delay(1);        // delay in between reads for stability
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容