• Ardunio控制RGB的LED灯显示彩虹渐变色.


    由于我使用的是共阴极的RGB LED,如果你的是共阳极的,接线的时候要注意一下.

    其他没什么不同

    //定义RGB色彩的输出I/O
    int redPin = 11;
    int greenPin = 10;
    int bluePin = 9;
    
    //标记颜色变化的方式,增加值还是减小值
    bool redBool =false;
    bool greenBool=true;
    bool blueBool=false;
    //颜色值,初始化为0,127,255
    int redVal =0;
    int greenVal=127;
    int blueVal=255;
    
    void setup()
    {
      pinMode(redPin, OUTPUT);
      pinMode(greenPin, OUTPUT);
      pinMode(bluePin, OUTPUT);
    }
    
    /**
     * 改变颜色的增减顺序
     */
    void changeStatus()
    {
       if (redVal==0)
      {
        redBool=true;
      }
      else if (redVal==255)
      {
        redBool=false;
      }
    
      if (greenVal==0)
      {
        greenBool=true;
      }
      else if (greenVal==255)
      {
        greenBool=false;
      }
    
      if (blueVal==0)
      {
        blueBool=true;
      }
      else if (blueVal==255)
      {
        blueBool=false;
      }  
    }
    
    /**
     * 改变颜色的变化量,增加还是减少
     */
    void changeColorVal()
    {
        if (redBool)
      {
        redVal++;
      }
      else
      {
        redVal--;
      }
      if (greenBool)
      {
        greenVal++;
      }
      else
      {
        greenVal--;
      }
      if (blueBool)
      {
        blueVal++;
      }
      else
      {
        blueVal--;
      }
    }
    /**
     * 设置led灯颜色
     */
    void setColor(int red, int green, int blue)
    {
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);
    }
    void loop()
    {
      //更新颜色变化状态
      changeStatus();
      //更新颜色值
      changeColorVal();
      //设置颜色
      setColor(redVal, greenVal, blueVal);
      delay(50);
    }
  • 相关阅读:
    爬虫心得
    WSL windows子系统ubuntu18.04建设自己的乌云
    WSL windwos 子系统 ubuntu18.04安装mysql
    python 163 email 554
    Centos 安装Oracle
    JS带进度 文件 重复 自动 异步上传
    xadmin 小组件默认折叠
    grep
    sed
    awk
  • 原文地址:https://www.cnblogs.com/sun_catboy/p/5889691.html
Copyright © 2020-2023  润新知