繁体   English   中英

如何使用 arduino 作为 LED controller?

[英]How do I use an arduino as an LED controller?

我是一个 C++ 新手,但我最近得到了一个 arduino,我正在用它做我的第一个项目。 我可能会在这里跳入深渊,但我已经有了一些电子产品的经验。 我对 python 也有一些经验。

我想使用其中两个LED 灯条和这个arduino 入门套件制作精美的室内照明设置。

我需要制作一大堆预设,这些预设可以使用套件中包含的 IR 遥控器快速激活。

我需要能够将条带分割成单独的区域,我可以使用这些区域轻松设置预设。 也许我希望预设 1 成为 50% 亮度的深蓝色天花板灯,而 window 以 25% 的亮度点亮橙色。

我知道要实现这一点,我希望遥控器上的每个按钮都调用不同的 function。 我已经编写了一些Psuedocode ,我想要一些关于学习如何在 arduino 上实现此功能的最佳方法的建议。

我真的需要这个项目的软件方面的帮助,硬件方面都很好。

免责声明:我从高中起就没有接触过编码,所以我的伪代码可能是垃圾。 我只是希望它是模糊的可以理解的

//Psuedocode attempt to make an LED thingy


// Importing needed libraries
Import fastled
Import IRremote

define num_led 300
define data_pin 5

// defining various zones. Zone 1 = Under cabinet, Zone 2 = Around window etc.
Global zone1 = num_led [0:24]
Global zone2 = num_led [24:172]
Global zone3 = num_led [173:277]
Global zone4 = num_led [278:299]

Def preset1()
    for led in zone1:
        ledRGB = (160, 0, 210)
    for led in zone2:
        ledRGB = (120, 0, 24)
    for led in zone3:
        ledRGB = (0, 0, 0)
    for led in zone4:
        ledRGB = (100,100,100)
// Make a whole bunch more of these normal presets

Def preset6()
    for led in zone1:
        do cool fastled animation
    for led in zone2:
        do different fastled animation
    for led in zone3:
        ledRGB = (0, 0, 0)
    for led in zone4:
        ledRGB = (0, 0, 0)

// Make a whole bunch more of these fancy presets

Def BrightnessUp()
    i = 0
    for num_led in zone1 and zone2 and zone3 and zone4:
        if i in ledRGB > 0: //This is to minimise colour shift. Any RGB channel set at 0 won’t increase.
            i = i+10
        else:
            return

// Similar for brightness down
        


void setup() { 
    
    FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
{



//The following is mostly copy pasted C++ code with modifications 
//It works to understand the IR remote inputs and call functions based on input

const int RECV_PIN = 7;

IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;

void setup(){
  irrecv.enableIRIn();
  irrecv.blink13(true);
}

void loop(){
  if (irrecv.decode(&results)){
 
        if (results.value == 0XFFFFFFFF)
          results.value = key_value;

        switch(results.value)
          case 0xFF30CF:
          Serial.println("1");
     Call preset1()
          break ;
          case 0xFF18E7:
          Serial.println("2");
     Call preset2()
          break ;
          case 0xFF7A85:
          Serial.println("3");
     Call preset3()
          break ;
         //ETC for every number on remote
      case 0xFFE01F:
          Serial.println("-");
     Call BrigtnessDown()
          break ;  
          case 0xFFA857:
          Serial.println("+");
     Call BrightnessUp()
          break ;  
     
        }
        key_value = results.value;
        irrecv.resume(); 
  }
}

要遵循的步骤是

  1. 从 GitHub 安装 FastLED 库: FastLED Lib
  2. 从 GitHub 安装 IRremote 库: IRremote Lib
  3. 通过 Arduino IDE 将其导入您的 Arduino 库文件夹: Importing a.zip Library

这是我们需要的程序的基本结构。

#include "FastLED.h"
#include "IRremote.h"
#define DATA_PIN 5   // digital pin of your arduino
#define NUM_LEDS 300

CRGB leds[NUM_LEDS];

//Zones array
int zone1[2] = {0, 24};    //For zone1: Start,End
int zone2[2] = {25, 172};  //For zone2: Start,End
int zone3[2] = {173, 277}; //For zone3: Start,End
int zone4[2] = {278, 299}; //For zone4: Start,End

// For IR receiver
const int RECV_PIN = 3; // Hardware specs allows 3 or 9 pin to be used for ATmega328p
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  //Setup for FastLED
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

  //Setup for IRremote
  pinMode(LED_BUILTIN, OUTPUT); //LED_BUILTIN is the inbuild led on Arduino UNO at Pin13
  irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true); // Enable blinking the LED when during reception
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    switch (results.value) {
      case 0xFF30CF:
        Serial.println("1");
        preset1();
        break;
      case 0xFF18E7:
        Serial.println("2");
        //preset2();  //TODO: Complete this preset2
        break;
      case 0xFF7A85:
        Serial.println("3");
        //preset3(); //TODO: Complete this preset3
        break;
      //ETC for every number on remote
      case 0xFFE01F:
        Serial.println("-");
        brightnessDown();
        break;
      case 0xFFA857:
        Serial.println("+");
        brightnessUp();
        break;
      default:
        Serial.println("Not Understood.");
        break;
    }
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}

void preset1()
{
  //Zone 1
  for (int i = zone1[0]; i <= zone1[1]; i++) {
    leds[i] = CRGB(160, 0, 210);
  }

  //Zone 2
  for (int i = zone2[0]; i <= zone2[1]; i++) {
    leds[i] = CRGB(120, 0, 24);
  }

  //Zone 3
  for (int i = zone3[0]; i <= zone3[1]; i++) {
    leds[i] = CRGB(0, 0, 0);
  }

  //Zone 4
  for (int i = zone4[0]; i <= zone4[1]; i++) {
    leds[i] = CRGB(100, 100, 100);
  }

  //To set the LEDs
  FastLED.show();
}

void brightnessUp()
{
  for (int i = 0; i < NUM_LEDS; i++) {
    if (leds[i].r > 0)
      leds[i].r = leds[i].r + 10;
    if (leds[i].g > 0)
      leds[i].g = leds[i].g + 10;
    if (leds[i].b > 0)
      leds[i].b = leds[i].b + 10;
  }
  //To set the LEDs
  FastLED.show();
}

void brightnessDown()
{
  for (int i = 0; i < NUM_LEDS; i++) {
    if (leds[i].r > 10)
      leds[i].r = leds[i].r - 10;
    if (leds[i].g > 10)
      leds[i].g = leds[i].g - 10;
    if (leds[i].b > 10)
      leds[i].b = leds[i].b - 10;
  }
  //To set the LEDs
  FastLED.show();
}
    

注意:此代码是在提供的伪代码和其中实现的逻辑的帮助下创建的,您可能必须更改某些内容才能使其产生最佳结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM