我是靠谱客的博主 幸福冰棍,最近开发中收集的这篇文章主要介绍arduino嵌入式开发--使用涂鸦wbr3远程控制LED呼吸灯,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

wbr3模组通讯串口使用 Arduino 引脚 0 和引脚 1,因此给 Arduino 开发板下载例程时,
请勿插三明治开发板(或长按三明治开发板板载 reset 按键),否则串口会互相干扰导致无
法正常下载程序。

以下是完整代码,实现涂鸦智能远程控制呼吸灯启停。

呼吸灯逻辑代码是在loop函数中监测arduino 10#引脚状态,如果为HIGH则启动,否则停止
通过3#引脚输出PWM来实现LED的明暗变化。

/*
 * @FileName: start.ino
 * @Author: Tuya
 * @Email: 
 * @LastEditors: Tuya
 * @Date: 2021-04-10 11:24:27
 * @LastEditTime: 2021-04-28 19:48:31
 * @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
 * @Company: http://www.tuya.com
 * @Description: This demo is based on the Arduino UNO, and the LEDs on the UNO board are controlled by the Tuya Smart App. 
 *               Enter network connection mode when Pin7 to GND.
 * @Github:https://github.com/tuya/tuya-wifi-mcu-sdk-arduino-library
 */

#include <TuyaWifi.h>
#include <SoftwareSerial.h>

TuyaWifi my_device;


/* Current LED status */
unsigned char led_state = 0;
/* Connect network button pin */
int key_pin = 7;
int led = 3;
int pinBtn = 10;
/* Data point define */
#define DPID_SWITCH 20
#define DPID_LIGHT 21 //灯的亮度 DP 6
#define DPID_MODE 22 //灯的工作模式 DP 
/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. 
 *                                     dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP
*/
unsigned char dp_array[][2] = {
{DPID_SWITCH, DP_TYPE_BOOL}, 
{DPID_LIGHT, DP_TYPE_VALUE}, 
{DPID_MODE, DP_TYPE_ENUM}
}; 

unsigned char pid[] = {"6nwnhhsm5qiscvj6"};
unsigned char mcu_ver[] = {"1.0.0"};

/* last time */
unsigned long last_time = 0;

void setup() 
{
  // Serial.begin(9600);
  Serial.begin(9600);

  //Initialize led port, turn off led.
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(led, OUTPUT);
  pinMode(pinBtn,OUTPUT);
  //Initialize networking keys.
  pinMode(key_pin, INPUT_PULLUP);

  //Enter the PID and MCU software version
  my_device.init(pid, mcu_ver);
  //incoming all DPs and their types array, DP numbers
  my_device.set_dp_cmd_total(dp_array, 3);
  //register DP download processing callback function
  my_device.dp_process_func_register(dp_process);
  //register upload all DP callback function
  my_device.dp_update_all_func_register(dp_update_all);

  last_time = millis();
}

void loop() 
{
  my_device.uart_service();

  //Enter the connection network mode when Pin7 is pressed.
  if (digitalRead(key_pin) == LOW) {
    delay(80);
    if (digitalRead(key_pin) == LOW) {
      my_device.mcu_set_wifi_mode(SMART_CONFIG);
    }
  }
  /* LED blinks when network is being connected */
  if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) && (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) && (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {
    if (millis()- last_time >= 500) {
      last_time = millis();

      if (led_state == LOW) {
        led_state = HIGH;
      } else {
        led_state = LOW;
      }
      digitalWrite(LED_BUILTIN, led_state);
    }
  }

  delay(10);
  if(digitalRead(pinBtn)==HIGH){
    breathingLed();}
}

/**
 * @description: DP download callback function.
 * @param {unsigned char} dpid
 * @param {const unsigned char} value
 * @param {unsigned short} length
 * @return {unsigned char}
 */
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)
{
  switch(dpid) {
    case DPID_SWITCH:
      led_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */
      if (led_state) {
        //Turn on
        digitalWrite(pinBtn, 1);
      } else {
        //Turn off
        digitalWrite(pinBtn, 0);
      }
      //Status changes should be reported.
      my_device.mcu_dp_update(dpid, value, length);
    break;

    default:break;
  }
  return SUCCESS;
}

/**
 * @description: Upload all DP status of the current device.
 * @param {*}
 * @return {*}
 */
void dp_update_all(void)
{
  my_device.mcu_dp_update(DPID_SWITCH, led_state, 1);
}
void breathingLed(){
     for (int a=0; a<=255;a++)                //循环语句,控制PWM亮度的增加
  {
    analogWrite(led,a);
    delay(8);                             //当前亮度级别维持的时间,单位毫秒            
  }
  for (int a=255; a>=0;a--)             //循环语句,控制PWM亮度减小
  {
    analogWrite(led,a);
    delay(8);                             //当前亮度的维持的时间,单位毫秒  
  }
}

在这里插入图片描述

最后

以上就是幸福冰棍为你收集整理的arduino嵌入式开发--使用涂鸦wbr3远程控制LED呼吸灯的全部内容,希望文章能够帮你解决arduino嵌入式开发--使用涂鸦wbr3远程控制LED呼吸灯所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部