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

Arduino Programming Basic - If and Loop

RonWang10个月前 (04-08)电子编程129

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

if and Loop Define 条件和循环语句定义

C arduino program If loop sentence

if 条件语句 1-8

int ledPin=13;
int delayPeriod =100;
void setup()
{
pinMode(ledPin,OUTPUT);
}
void loop()
{
  digitalWrite(ledPin,HIGH);
  delay(delayPeriod);
  digitalWrite(ledPin,LOW);
  delay(delayPeriod);
  delayPeriod =delayPeriod +100 ;
  if (delayPeriod>3000);
  {
  delayPeriod =100;
  }
}

For 循环语句例子 1-9

int ledPin=13;
int delayPeriod =100;
void setup()
{
pinMode(ledPin,OUTPUT);
}
void loop()
{
   for (int i=0; i<20;i++)
    {
      digitalWrite(ledPin,HIGH);
      delay(delayPeriod);
      digitalWrite(ledPin,LOW);
      delay(delayPeriod);
      }
      delay(3000);
}

这个程序的缺点是loop函数会运行较长的时间,处理器会被for循环语句占用,别的进程就无法调用处理,所以改进算法是尽可能快的让loop函数执行完,把进程空出来处理别的程序。

改进算法 1-9a

int ledPin=13;
int delayPeriod =100;
int count = 0;
void setup()
{
  pinMode(ledPin,OUTPUT);
}
void loop()
{
      digitalWrite(ledPin,HIGH);
      delay(delayPeriod);
      digitalWrite(ledPin,LOW);
      delay(delayPeriod);
      count++ ;
      if (count == 20)
      {
        count = 0;
        delay(3000); 
       }
}

While 循环 1-10

int ledPin=13;
int delayPeriod =100;
void setup()
{
pinMode(ledPin,OUTPUT);
}
void loop()
{
  int i=0;
  while(i<20)
   {
      digitalWrite(ledPin,HIGH);
      delay(delayPeriod);
      digitalWrite(ledPin,LOW);
      delay(delayPeriod);
      i++ ;
    }
  delay(3000);
}

Code written on Arduino IDE Software

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

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

标签: Arduino

相关文章

Arduino Project 030 - Line Following Robot

Arduino Project 030 - Line Following Robot

Project 30 Line Following RobotUse L298P Driver Line Following Robot Line o...

Arduino Project 020 -  LED Dot Matrix Display - Beat Heart

Arduino Project 020 - LED Dot Matrix Display - Beat Heart

You’re going to use the same circuit, but with a slight variation in the code to create a multi-fram...

Arduino Project 005 - LED Chase Lights

Arduino Project 005 - LED Chase Lights

You’re going to use a string of LEDs (10 in total) to make an LED chase effect, similar to that used...

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 028B - Basic Stepper Control (Unipolar)

Arduino Project 028B - Basic Stepper Control (Unipolar)

In this very simple project, you will connect up a stepper motor and then get the Arduino to control...

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...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。