CODE | If and Loop - Arduino Programming Basic

RonWang4 years ago (2022-07-09)科学研究 SCI235

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

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

C arduino program If loop sentence

if 条件语句 1-8

C
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

C
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

C
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

C
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

Share with Friends:

Related Articles

Unnamed

撰写内容中………

The Revolution from Plastic to Teflon Seals

The Revolution from Plastic to Teflon Seals

从塑料到特氟龙材料-密封行业的革命The Revolution from Plastic to Teflon SealsThe term “plastics” is generic way of de…

Unnamed

撰写内容中………

Unnamed

eerer…

Vehicle Stamping Line Fluid Contamination Online Monitoring Management System

Vehicle Stamping Line Fluid Contamination Online Monitoring Management System

汽车冲压线油液污染在线监测管理系统     摘要:设计开发了汽车冲压线液压系统油液污染检测装置,介绍了激光颗粒计数器的关键技术,检测系统的原理图、油液的回路图、系统工作的…

Unnamed

eerer…

Post a Comment

Anonymous

Feel free to share your thoughts and opinions here.