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

Arduino Programming Basic - Variables

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

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://parentscn.com/?id=244

标签: Arduino

相关文章

Arduino UNO Mp3音乐播放代码

Arduino UNO Mp3音乐播放代码

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

Arduino Project 035 - Keypad 4X4 or 4X3

Arduino Project 035 - Keypad 4X4 or 4X3

Most of the time we are used key, button, or switch to get input value in our projects. When we inte...

Arduino Project 023C - Liquid Crystal Displays - Serial to Display

Arduino Project 023C - Liquid Crystal Displays - Serial to Display

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14...

Arduino Project 023D - Liquid Crystal Displays - Custom Character

Arduino Project 023D - Liquid Crystal Displays - Custom Character

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14...

Arduino Project 045 - RFID Servo and LED Control System

Arduino Project 045 - RFID Servo and LED Control System

Arduino Programming Basic -- RFID Servo and LED Control System Project 45 RFID Servo...