CODE | Serial Monitor - Arduino Programming Basic

RonWang4 years ago (2022-07-01)电子编程 COD16

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

Serial Monitor 串口监视器

Arduino Serial Monitor

串口监视器 Serial.println  1-5

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println(1234);
}

void loop() {
  // put your main code here, to run repeatedly:
  
}


串口显示一个运算的结果 1-6

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  int a=2;
  int b=4;
  int c= a+b;
  Serial.println(c);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
}


串口显示一个摄氏度转换华氏度的运算结果 1-7   

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  int degC=20;
  int degF;
  degF=degC*9/5 + 32;
  Serial.println(degF);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
}


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

Share with Friends:

Related Articles

Centos Install OSCommerce 4.0

Centos Install OSCommerce 4.0

osCommerce is a Free shopping cart and open source Ecommerce software. You can choose to host your o…

C05 |   C/C++ Develop Environment

C05 | C/C++ Develop Environment

C语言教程05: C/C++开发环境C 语言编译器可以分为 C 和 C++两大类,其中 C++是 C 的超集,也支持 C 语言编程。事实上,编译器的选择不是最重要的,它们都可以完成基本的 C 语言编译…

CODE | Shift Register 8-Bit Binary Counter - Arduino Project 017

CODE | Shift Register 8-Bit Binary Counter - Arduino Project 017

In this project, you’re going to use additional ICs (Integrated Circuits) in the form of shift regis…

CODE | Simple Motor Control - Arduino Project 015

CODE | Simple Motor Control - Arduino Project 015

First, you’re going to simply control the speed of a DC motor in one direction, using a power transi…

About microcontroller board Arduino

About microcontroller board Arduino

What is Arduino?Update time: Jan.1.2023Arduino designs, manufactures, and supports electronic device…

Arduino software Download

Arduino software Download

Arduino Web EditorStart coding online and save your sketches in the cloud. The most up-to-date versi…

Post a Comment

Anonymous

Feel free to share your thoughts and opinions here.