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

Arduino Programming Basic - Variables

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

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 Project 001 - LED Blink

Arduino Project 001 - LED Blink

Arduino 电子编程--灯项目及控制,主要使用Arduino编程控制LED灯,实现基本控制Project 1 LED闪烁,基本的应用Project 3和4红绿灯项目项目1 Project...

Arduino Project 030A - Dual Motor Driver L298N

Arduino Project 030A - Dual Motor Driver L298N

L298N Dual Motor Driver Project Description  The L298N Motor Driver is a control...

Arduino Project 003 - LED Traffic Light

Arduino Project 003 - LED Traffic Light

You are now going to create a set of traffic lights that will change from green to red, via amber, a...

C语言调试运行环境TurboC的安装

C语言调试运行环境TurboC的安装

上大学时学习《C语言程序设计》(第二版)作者谭浩强,大部分编程时间是在学校机房度过,每次点开桌面的TurboC图标,就开始在里面敲代码,然后保存程序Abc.C,下一步进行编译,如果编译成功的话,就可以...

Arduino Project 012 - Piezo Sounder Melody Player

Arduino Project 012 - Piezo Sounder Melody Player

Rather than using the piezo to make annoying alarm sounds, why not use it to play a melody? You are...

Arduino Project 026 - Dual Servo Control

Arduino Project 026 - Dual Servo Control

This project you’ll create another simple project, but this time you’ll control two servos using com...