当前位置:首页 > 科学研究 > 电子编程 > 正文内容

Arduino Programming Basic - Variables

RonWang1年前 (2024-03-25)电子编程421

Arduino 程序基础,介绍Arduino程序的基本组成,第一部分编写了10个例子,关于变量及变量名称,串口监视器,if循环,for循环,while循环等。第二部分介绍了函数,全局变量,局部变量和静态变量,数据类型,Bollean运算,注释语句等。

变量及变量名称定义

Blink LED 闪灯一次 1-1

void setup() {
  // put your setup code here, to run once:
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
}
void loop() {
  // put your main code here, to run repeatedly:
  
}


Blink LED 连续闪烁 1-2 

void setup() {
  // put your setup code here, to run once:
  pinMode(13,OUTPUT);
  
}
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(13,HIGH);
  delay(500);
  digitalWrite(13,LOW);
}

LED在LOW被关闭的一瞬间立刻被点亮了,速度很快,所以人类肉眼看起来就是常亮的。

在最后再加一句delay(500); 程序看起来就运行正常了,灯开始闪烁了。

001 LED Blink Circuit


001 LED Blink Schematic

Blink LED 连续闪烁 1-3 // 变量

int ledPin = 13;
int delayPeriod = 500 ;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin,OUTPUT);
  
}
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(ledPin,HIGH);
  delay(delayPeriod);
  digitalWrite(ledPin,LOW);
  delay(delayPeriod);
}


Blink LED 连续闪烁 1-4 // 变量+循环


int ledPin = 13;
int delayPeriod = 100 ;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin,OUTPUT);
  
}
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(ledPin,HIGH);
  delay(delayPeriod);
  digitalWrite(ledPin,LOW);
  delay(delayPeriod);
  delayPeriod =delayPeriod +100 ;
}


局部变量Local Variables,全局变量Global Variables,静态变量 Static Variables 1-5


int ledPin=13;
int delayPeriod =200;

void setup()
{
pinMode(ledPin,OUTPUT);
}

void loop()
{ 
  static int count=0;
  digitalWrite(ledPin,HIGH);
  delay(delayPeriod);
  digitalWrite(ledPin,LOW);
  delay(delayPeriod);
  count ++ ;
  if (count == 20)
  {
   count=0 ;
   delay(3000);
 }
}

静态变量的意义是变量只在函数第一次运行的时候被初始化,而不是每次运行时都初始化。区别 如果我们不对count=0;定义为Static时,程序在每次循环时都执行一次count=0,所以程序无法正常运行。因为count的值一直等于0,它永远都不会达到20,所以LED会一直闪下去。


Code written on Arduino IDE Software

版权声明:本文为原创文章,版权归donstudio所有,欢迎分享本文,转载请保留出处!

本文链接:http://www.parentscn.com/?id=244

标签: Arduino

相关文章

Arduino UNO Mp3音乐播放代码

Arduino UNO Mp3音乐播放代码

Arduino UNO Mp3音乐播放代码今天我们将使用Arduino UNO 和SD卡制作音乐播放器。这个播放器不需要添加多余的模块,只需要SD读卡器和Arduino UNO开发板就可以播放音频文件...

Arduino Project 042 - SD Card Information Basic

Arduino Project 042 - SD Card Information Basic

Arduino Programming Basic -- Reading and Writing to an SD CardProject 42A SD Card InformationArduino...

Arduino Project 007 - Pulsating Lamp

Arduino Project 007 - Pulsating Lamp

You are now going try a more advanced method of controlling LEDs. So far, you have simply turned the...

Arduino Project 036 - DTH Temperature and Humid Sensor

Arduino Project 036 - DTH Temperature and Humid Sensor

The DHT11 Temperature and Humidity Sensor senses, measures and regularly reports the relative humidi...

Arduino Project 044 - Simple RFID Reader

Arduino Project 044 - Simple RFID Reader

Arduino Programming Basic -- Making an RFID ReaderProject 44 Simple RFID ReaderWiring Diagram Betwee...

Arduino Project 021 -  LED Dot Matrix Display - Scrolling Message

Arduino Project 021 - LED Dot Matrix Display - Scrolling Message

There are many different ways to drive LEDs. Using shift registers is one way and they have their a...