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