深入arduino

发布于 2021-11-17  496 次阅读


输入上拉模式

/*
  Input Pull-up Serial

  This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
  input on pin 2 and prints the results to the Serial Monitor.

  The circuit:
  - momentary switch attached from pin 2 to ground
  - built-in LED on pin 13

  Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
  20K-ohm resistor is pulled to 5V. This configuration causes the input to read
  HIGH when the switch is open, and LOW when it is closed.

  created 14 Mar 2012
  by Scott Fitzgerald

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/InputPullupSerial
*/

void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin 2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);

}

void loop() {
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton
  Serial.println(sensorVal);

  // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
  // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);
  }
}

INPUT_PULLUP

说明
Arduino 微控制器自带内部上拉电阻。如果你需要使用该内部上拉电阻,可以通过pinMode()将引脚设置为输入上拉(INPUT_PULLUP)模式。

注意:当Arduino引脚设置为输入(INPUT)模式或者输入上拉(INPUT_PULLUP)模式,请勿将该引脚与负压或者高于5V的电压相连,否则可能会损坏Arduino控制器。了解更多如何使用上拉电阻或者下拉电阻的内容,请参阅本站相关内容。

设置Arduino引脚为输入上拉模式示例程序

在本示例中,我们将通过pinMode()语句将Arduino引脚2配置为输入模式。在此示例中,当开关闭合后,引脚2将获得低电平开关信号,引脚13旁的LED(如下照片红圈所示)将被点亮。反之,该LED为熄灭状态。

本示例程序Arduino Uno连接说明(如下图)

file

代码注释

/*
设置Arduino引脚
为输入上拉(INPUT_PULLUP)
模式示例程序

v1.0
Created 2016
by 太极创客
www.taichi-maker.com

说明:
本程序旨在演示如何将Arduino引脚设置为
输入上拉(INPUT_PULLUP)模式。
当按钮被按下后,引脚13旁的LED将会点亮。
获得具体连接电路图,请参阅太极创客网站。

This example code is in the public domain.
*/

void setup() {
    //将引脚2设置为输入上拉(INPUT_PULLUP)模式
    pinMode(2, INPUT_PULLUP);
    //将引脚13设置为输出模式
    pinMode(13, OUTPUT);

}

void loop() {
    // 检查引脚2的输入情况
    // 将2号引脚输入赋给变量val
    // 开关闭合后,引脚2将获得
    // 低电平信号(val = LOW)
    int val = digitalRead(2);

    // 开关闭合后,引脚2将获得
    // 低电平信号(val = LOW)
    // 在 val = LOW时,点亮
    // 引脚13旁的LED。
    // 否则保持LED熄灭状态
  if (val == HIGH) {
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);
  }
}

MC猜数

file

按钮:同侧不相连

rand()

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
   int i, n;
   time_t t;

   n = 5;

   /* 初始化随机数发生器 */
   srand((unsigned) time(&t));

   /* 输出 0 到 49 之间的 5 个随机数 */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d\n", rand() % 50);
   }

  return(0);
}

模拟输入

控制LED灯亮度

file

file

示例代码

file

/*
25 模拟输出1 - analogWrite
太极创客
www.taichi-maker.com

此程序用于太极创客<<零基础入门学用Arduino教程>> 
25 模拟输出1 - analogWrite

演示如何通过两个按键开关通过analogWrite指令
进行模拟输出操作。具体电路和其它信息请参考
太极创客网站本教程相关页面。

2017-04-28
*/
boolean pushButton1;   // 创建布尔型变量用来存储按键开关1的电平状态
boolean pushButton2;   // 创建布尔型变量用来存储按键开关2的电平状态
int ledPin = 9;        //LED引脚号
int brightness = 128;  //LED亮度参数

void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT_PULLUP); //将引脚2设置为输入上拉模式
  pinMode(8, INPUT_PULLUP); //将引脚8设置为输入上拉模式
  pinMode(ledPin, OUTPUT);  //将LED引脚设置为输出模式
  Serial.begin(9600);      //启动串口通讯
}

void loop() {
  // put your main code here, to run repeatedly:
  pushButton1 = digitalRead(2); //读取引脚2电平状态并将其赋值给布尔变量
  pushButton2 = digitalRead(8); //读取引脚8电平状态并将其赋值给布尔变量

  if (!pushButton1 && brightness > 0){     // 当按下按键开关1并且LED亮度参数大于0
    brightness--;                          // 减低LED亮度参数
                                           //(brightness-- 相当于  brightness = brightness - 1;)
  } else if (!pushButton2 && brightness < 255) {  //当按下按键开关2并且LED亮度参数小于255
    brightness++;                                 //增加LED亮度参数
                                                  //(brightness++ 相当于  brightness = brightness + 1;)
  }
  analogWrite(ledPin, brightness);         //模拟输出控制LED亮度
  Serial.println(brightness);              //将LED亮度参数显示在串口监视器上
  delay(10);
}

电位器

file

file

file

file

file

file

file

电位器代码


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

/*
  模拟输入

  本程序旨在演示如何使用analogRead()读取Arduino的引脚电平。
  通过调节电位器, A0引脚的输入电压将在0V-5V之间。
  该输入电压将被映射到数值0-1023之间,并显示在串口监视器中。

 电路连接:
   电位器中间引脚连接到模拟输入A0引脚
   电位器两端引脚分别连接在Arduino +5V和接地引脚

 太极创客 www.taichi-maker.com
 2017-01-08

 This example code is in the public domain.

 */

void setup() {
  // 串口通讯初始化(9600 bps):
  Serial.begin(9600);
}

void loop() {
  // 读取模拟输入值:
  int analogInputVal = analogRead(A0);

  // 将结果通过串口监视器显示:
  Serial.println(analogInputVal);
}

analogRead()

说明
本指令用于从Arduino的模拟输入引脚读取数值。Arduino控制器有多个10位数模转换通道。这意味着Arduino可以将0-5伏特的电压输入信号映射到数值0-1023。

换句话说,我们可以将5伏特等分成1024份。0伏特的输入信号对应着数值0,而5伏特的输入信号对应着1023。

例:
当模拟输入引脚的输入电压为2.5伏特的时候,该引脚的数值为512。
(2.5伏特 / 5伏特 = 0.5, 1024 X 0.5 ?=512)

引脚的输入范围以及解析度可以使用analogReference()指令进行调整。

Arduino控制器读取一次模拟输入需要消耗100微秒的时间(0.0001秒)。控制器读取模拟输入的最大频率是每秒10,000次。

注意:在模拟输入引脚没有任何连接的情况下,用analogRead()指令读取该引脚,这时获得的返回值为不固定的数值。这个数值可能受到多种因素影响,如将手靠近引脚也可能使得该返回值产生变化。

语法
analogRead(pin)

参数
pin:被读取的模拟引脚号码

返回值
0到1023之间的值

示例程序
在本示例中,我们将电位器的三个引脚分别连接在5V, GND, 以及A0引脚。通过调节电位器, A0引脚的输入电压将在0V-5V之间。在Arduino内置的模拟数字转换功能作用下,该输入电压将被映射到数值0-1023之间。
(0V对应数值0, 5V对应1023)。这一数值将通过串口监视器显示。

连接说明(如下图)

file

代码

logRead()示例程序Arduino
/*
analogRead()示例程序
v1.0
Created 2016
by 太极创客
www.taichi-maker.com

说明:
本程序旨在演示如何使用analogRead()读取Arduino的引脚电平。
通过调节电位器, A0引脚的输入电压将在0V-5V之间。
该输入电压将被映射到数值0-1023之间,并显示在串口监视器中。
获得具体连接电路图,请参阅太极创客网站。

This example code is in the public domain.
*/

//电位器中间引脚连接在Arduino的A0引脚
//电位器另外两个引脚分别连接在Arduino的5V和GND引脚

//变量val用来存储模拟输入信号
int val = 0;           

void setup()
{
  //Arduino串口通讯初始化 
  Serial.begin(9600); 
}

void loop()
{
  //读取引脚A0输入信号
  val = analogRead(A0);   

  //将A0输入信号转换为0-1023之间的数值
  //并且通过串口监视器显示 
  Serial.println(val); 

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
analogRead()示例程序
v1.0
Created 2016
by 太极创客
www.taichi-maker.com

说明:
本程序旨在演示如何使用analogRead()读取Arduino的引脚电平。
通过调节电位器, A0引脚的输入电压将在0V-5V之间。
该输入电压将被映射到数值0-1023之间,并显示在串口监视器中。
获得具体连接电路图,请参阅太极创客网站。

This example code is in the public domain.
*/

//电位器中间引脚连接在Arduino的A0引脚
//电位器另外两个引脚分别连接在Arduino的5V和GND引脚

//变量val用来存储模拟输入信号
int val = 0;           

void setup()
{
  //Arduino串口通讯初始化 
  Serial.begin(9600); 
}

void loop()
{
  //读取引脚A0输入信号
  val = analogRead(A0);   

  //将A0输入信号转换为0-1023之间的数值
  //并且通过串口监视器显示 
  Serial.println(val); 

}

randomSeed()

说明
randomSeed()函数可用来产生随机种子。单独使用random()函数所产生的随机数,在每一次程序重新启动后,总是重复同一组随机数字。如果希望程序重新启动后产生的随机数值与上一次程序运行时的随机数不同,则需要使用randomSeed()函数。

在实际应用时,可以通过调用analogRead()函数读取一个空引脚,作为随机种子数值。具体操作,本页面后续示例程序将进行说明。

语法
randomSeed(seedVal)

参数
seedVal: 随机种子数值

代码:

long randNumber;

void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(A0)); 
  //将引脚A0放空,每次程序启动时所读取的数值都是不同的。
  //这么做可以产生真正的随机种子值,从而产生随机数值。
}

void loop(){
  randNumber = random(300);  // 产生随机数
  Serial.println(randNumber);

  delay(50);
}

集合

file

代码

/*
  电位器模拟输出

 读取模拟输入引脚,并将读取到的数值映射到0 - 255之间。然后用该映射结果设置
 引脚9的LED亮度,同时通过串口监视器显示这一映射结果。

 电路连接:
     电位器中间引脚连接到模拟输入A0引脚
     电位器两端引脚分别连接在Arduino +5V和接地引脚
   * LED正极通过 限流电阻连接在Arduino的9号引脚
     LED负极接地

 太极创客
 2017-01-08
 http://www.taichi-maker.com

 This example code is in the public domain.

 */

void setup() {

  Serial.begin(9600);  // 串口通讯初始化(9600 bps)
  pinMode(9, OUTPUT);  // 设置9号引脚为输出模式
}

void loop() {
  int analogInputVal = analogRead(A0);  // 读取模拟输入值

  int brightness = map(analogInputVal, 0, 1023, 0, 255); //将模拟输入数值(0 - 1023)等比映射到模拟输出数值区间(0-255)内

  analogWrite(9, brightness);  //根据模拟输入值调节LED亮度

  // 将结果通过串口监视器显示:
  Serial.print("analogInputVal = ");
  Serial.println(analogInputVal);

  Serial.print("brightness = ");
  Serial.println(brightness);

  Serial.println("");
}

总结

file


擦肩而过的概率