零基础入门学arduino基础知识篇 (基础知识和按键逻辑电路)

关键词(保留字)、注释语句

函数:一个小括号、一个大括号


创建变量:

      变量名称不可变,变量的数值可变

        变量类型 变量名称 ;

创建变量的注意事项:

        连续的一个单词

        不能数字开头

        不能有“#”灯符号

        不能关键字

代码良好习惯
变量储存内容和名称对应
如用currentTemperature存储当前温度
int currentTemperature ; //第二个单词大写
int current_Temperature ;//下划线加大写做分隔
int current_temperature ;//下划线分隔

给变量赋值
int current_temperature =30 ;

节6 程序结构 

程序基本结构

程序运行方式

在程序中使用变量

//在程序中使用变量

int OnTime  =3000 ; 
int OffTime  =5000 ; 

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(OnTime);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(OffTime);                      // wait for a second
}

节7 变量的基本操作

变量的类型

变量的运算 

变量的作用域

int类型:±3.2w;

long长整型:±21亿

float单精度:1w 亿 没问题

double双精度:同float

全局变量和局部变量。

        这涉及到变量的作用范围


节8  程序函数1

程序函数的基本概念

程序函数的参数

在程序中使用函数

delay(1000)

函数名称、参数、返回值


节9  程序函数2

函数的应用

点亮LED的电路

PinMode();DigitalWrite()函数

//Pin_Mode()函数说明
param1:引脚名称
param2:
        输出模式;(可以输出电流)
        输入模式;
        上拉模式;
Pin_Mode(5,OUTPUT)

digital_Write(5,HIGH)将引脚设置为高电平状态。

Pin_Mode       ~   电源
digital_Write  ~   开关

节10 数字输出 1

LED 电流特性、电压特性

电阻的作用


 节11 面包板

同一排的孔(绿色)算一个模板 (上图)

黄色的孔算一个结点

连接电路,通国Pin_Mode函数和DigitalWrite函数直接点亮 


节12 按键开关

按键原理介绍

输入模式原理

按键:相连不同侧;同侧不相连

                产生一个数字信号。

               设置为INPUT数字输入状态的时候,可以识别读取两种状态

当开关没有按下去的时候,引脚是高电平 

 按下开关的时候,引脚二接地,受到低电平

上拉电阻(阻值10k)为了不要让arduino提前退休

引脚2在INPUT模式时不接外接任何的电路。是引脚浮空现象,随机读取高低电平。


p14 数字输入2  按键开关控制电路与程序

按键开关电路搭建

程序函数的使用

串口的使用

/*
  DigitalReadSerial

  Reads a digital input on pin 2, prints the result to the Serial Monitor
\\读取端口2的状态,将结果写在串口显示器当中
  This example code is in the public domain.
  https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;  \\与前文类似那个LItin,这里是将引脚2定义了一个名称

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);//当前按钮状态,读取引脚的数据。返回HIGH或LOW,

//这里HIGH和LOW都是关键字,分别对应1和0,所以有看到的现象。         
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);  // delay in between reads for stability
}

串口通信:便于传递串口的信息。只有一个LED来检测是肯定不够的。

解释:Serial.begin(9600);

        调用serial库里的begin函数,传输速率为9600

        作用:启动串口通信;

解释:Serial.Println(buttonState)

         调用serial库里的PrintLine函数,读取buttonState,并且输入到串口显示器当中去。


1-15 逻辑控制

输入上拉模式

逻辑运算

输入上拉模式:自动启用内部上拉电阻,无需另外附加

void setup() {
    pinMode(2,INPUT_PULLUP);
    pinMode(13,OUTPUT);
}

void loop() {
 int Sensorval = digitalRead(2);
  if( Sensorval == HIGH){
      digitalWrite(13,LOW);
  }
  else{
    digitalWrite(13,HIGH);
  }
}

函数的总结

serial.begin(9600)
Serial.println(sensorVal);
pinMode()
digitalwrite()
digitalread()


1-16 逻辑控制2

布尔类型

boolean

非0值存储为1;

0存储为0;

 

这个函数的现象和上一个正好相反,这里按下之后pushbutton的值为0非1,在程序里执行输出低电平的方式。


 1-17 逻辑运算3

解决上节问题,怎样用bool型变量来实现按下开关亮,松开灭?

布尔运算符号

与或非

与:&&

或:||

非:!

到目前为止;主要讲解了pin引脚的上拉输入模式配合按键在逻辑程序中控制LED灯的亮与灭。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值