LED颜色响应光传感器值

时间:2015-12-17 03:31:57

标签: arduino led

您好,我不确定是否有人可以帮助我,我正在尝试将我放在我的arduino上的RGB LED灯改变颜色,这取决于从Photoresistor模块读取的光量。我不确定如何编写该部分并可以使用一些帮助。

到目前为止我的代码很简单,我是编程和Arduino板的新手,而且大多只是搞乱。该代码目前将从光敏电阻,温度和湿度传感器读取读数,并在串行监视器中为您打印出来。我想将LED合并到项目中,当光线值低于200时变为红色,200-400之间变为紫色或蓝色,400以上变为绿色。

我的代码:

#include <DHT.h>
#include <avr/pgmspace.h>
#define DHTPIN 2     
#define DHTTYPE DHT11   


DHT dht(DHTPIN, DHTTYPE);
int sensorPin = 2; //Light Sesnor
int value = 0;

int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the  blue LED
int greenpin =9;// select the pin for the green LED



void setup() {
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT); 
  Serial.begin(9600);

  delay(500);//Delay to let system boot

  delay(1000);//Wait before accessing Sensor

  dht.begin();
}

void loop() {


  value = analogRead(sensorPin);
  float h = dht.readHumidity(); //Humididty reading

  float f = dht.readTemperature(true);// Temp reading in *F


  if (isnan(h) || isnan(f)) { //Check to see if the readings came through
    Serial.println("Sorry but I Failed to read from your sensor Ashley!");
    return;
  }


   String outputString = String(value) + ", " + String(h) + ", " + String(f);
   Serial.println(outputString);

  if(lightvalue < 200) analogWrite(redPin, HIGH);  //For light values less than 200 red led

  if(lightvalue >= 200 && lightvalue <=400) analogWrite(bluePin, HIGH); //For light values between 200 and 400 Blue LED

  if(lightvalue > 400) analogWrite(greenPin, HIGH);//For light levels higher than 400 Green LED

  delay(5000);


}

以上是我到目前为止创建的代码。正如我所说,它需要从光敏电阻和温度和湿度传感器读取并显示它们。我想以某种方式编码这个也读取光敏电阻值并改变读取值上的LED。代码工作到一个点,颜色将出现在具有指定光值的LED上,但颜色将不会“清除”或在读数后消失,因此最终每种颜色,红色,绿色和蓝色都保持打开而不仅仅是根据阅读从一种颜色变为另一种颜色。每次读数后如何清除LED?

-Ashley

1 个答案:

答案 0 :(得分:1)

创建一个包含您关注比例的颜色的数组,并使用README of respond.js将输入映射到数组。

uint8_t colors[3][] = {
  {255, 0, 0},
  {0, 0, 255},
  {0, 255, 0}
};

 ...
uint8_t *entry[3] = colors[map(..., ..., ..., 0, sizeof(colors) / 3 - 1)];
 ...

奖励分数map()