码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-20 18:01:56      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

由于我使用的是共阴极的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);
}

 

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

标签:

原文地址:http://www.cnblogs.com/sun_catboy/p/5889691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!