Arduino中文教程----实现指令控制电灯开关

in utopian-io •  7 years ago  (edited)

What Will I Learn?

  • 实践使用Arduino的串口

  • 实现用指令控制LED的亮灭

Requirements

  • Arduino UNO

    图片.png

  • Arduino IDE开发环境

    图片.png

  • 串口调试助手

    图片.png

Difficulty

  • 基础

Tutorial Contents

一、实践使用Arduino的串口

  • 打开arduino.exe开发环境

    图片.png

  • 对Arduino串口进行初始化

    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600); 
    }
    
    
  • 当串口有数据接收到时,获取储存在缓冲区的串口数据

    if(Serial.available())
      {
        rec =  Serial.read();
        Serial.print(rec);
      
      }
    
    
  • 向串口发送数据

    图片.png

  • 打印数据显示

图片.png

  • 对获取到的据进行判断

    if(Serial.available())
      {
        rec =  Serial.read();
        if(rec == '1')
          Serial.print("this is the number 1");
        else if(rec == '2')
            Serial.print("this is the number 2");
      
      }
    

二、实现电脑发送指令控制LED的亮灭

  • 需求分析:结合步骤一,可以成功的获取串口发送的数据。当获取到串口数据的时候,加以判断

    • 若指令为“on”则提供引脚以高电平,也就是点亮LED
    • 若指令为“off”则提供引脚以低电平,也就是熄灭LED
  • 连接线路

    ArduinoLED
    D13长脚
    GND短脚

    图片.png

  • 整合代码

    char rec;
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      pinMode(13,OUTPUT);
       digitalWrite(13,LOW);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      if(Serial.available())
      {
        rec =  Serial.read();
        Serial.print(rec);
        if(rec == 'on')
        {
          digitalWrite(13,HIGH);
          }
        else if (rec == 'off')
        {
           digitalWrite(13,LOW);
          }
        
      }
    }
    
    
  • 运行测试

    GIF.gif

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

hi good morning,thank you

Hey @cha0s0000 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x