-2

Hi there I wasn't sure if anyone could help me I am trying to get my RGB led light that I have put on my arduino to change color depending on the amount of light read from a Photoresistor module. I'm not really sure how to go about coding that part and could use some help.

The code I have so far is simple, I'm new to programing and Arduino boards and am mostly just messing around. The code currently will take in readings from a photoresistor, and a temp and humidity sensor and print them out for you in the Serial monitor. I would like to incorporate the LED into the project and have it turn Red when light values are below 200, purple or blue when between 200-400, and green for 400 and above.

My code:

#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);


}

So above is the code I've created so far. As I said it takes readings from a photoresistor and a temp and humidity sensor and displays them. I would like to somehow code this to also read the photoresistor value and change the LED baised on the reading value. The code works to a point the colors will appear on the LED with the specified light values but the colors will not "clear" or go away after a reading so eventually each color, red, green, and blue are left on and will not just change from one color to the other based on the reading. How do I clear the LED after each reading?

-Ashley

mm19
  • 87
  • 1
  • 10
  • AshleyMadison, what issue are you experiencing exactly? What does your current code produce for you? You need to EDIT your code sample and question to specify a problem. – Mathemats Dec 17 '15 at 03:35
  • I don't have an issue with this code above, I'm just trying to incorporate a RGB led into this code that responds to the photoresistor readings this code produces. I am not very good at coding so I was hoping someone would be able to help guide me along on how to code something like that. – mm19 Dec 17 '15 at 03:38
  • I suggest checking out Google then. SO is concerned with developing solutions to problems (this is an important part) for developers now and in the future. Check out http://stackoverflow.com/help/how-to-ask when you stumble in the future. If you're looking for a guide then Google is filled with results. – Mathemats Dec 17 '15 at 03:42
  • Thats why I've posted this here because I cannot find relevant information on google that will help me with coding this. I've found postings about many topics on RGB led's but nothing that is sufficient. – mm19 Dec 17 '15 at 03:47
  • `if(value < 200) digitalWrite(redPin, HIGH);`.....same with other values. What problem have you? – jabujavi Dec 17 '15 at 10:47
  • I have the LED working now the only problem i have is the colors will not "clear" from the led after a reading. So if it reads values for blue it turns blue and stays on as blue then if it reads values for red it is red and blue. same for green its all three of the color. I don't know how to clear the color form the LED between each sensor reading. – mm19 Dec 17 '15 at 15:42
  • Using something that specifies all three components instead of brute-forcing comparisons would solve this. – Ignacio Vazquez-Abrams Dec 17 '15 at 16:14
  • Sorry, but... if `digitalWrite(redPin, HIGH);` is the instruction to set the red pin on, can you guess what is the instruction to turn it off? And after you can turn it down, simply set the behavior of all the leds in each instruction instead of turning on only one – frarugi87 Dec 17 '15 at 16:19

1 Answers1

1

Create an array that contains the colors in the proportion you care about, and use map() to map the input to the array.

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

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

For bonus marks put the array in flash.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Thanks for the post. I will look more into putting the input into an array. I appreciate your help! – mm19 Dec 17 '15 at 03:51