Hello! I'm trying to get a program to act the way I want it to. I'm trying to make it so that when the button from the remote control is pressed, the code starts reading the photoresistor value and constantly refreshes that value, and depending on what that value is a set of LEDs turn on or off. Currently the code runs as follows, I can press the button on the remote and the photoresistor value is read, but if the value of the photoresistor is changed it's not updated unless the remote button is pressed again. The code works the way I want it to only when the code for the IR sensor is removed. The first set of codes includes the IR sensor and doesn't let the photoresistor update it's value, while the second set of codes doesn't include the IR sensor but has the photoresistor working the way I want it too. I have also attached a simple schematic of the project. Thank you!
FIRST SET OF CODES:
#include <IRremote.h> // Infrared remote library
int RECV_PIN = 11;
int GreenLED = 10;
int YellowLED = 12;
int RedLED = 13;
int sensorPin = A0;
int sensorValue = 0;
IRrecv irrecv(RECV_PIN); //Create the receiver object
decode_results results; //When a signal is received, information is stored into "results"
void setup(){
irrecv.enableIRIn(); //Begin the receiving IR Signal
pinMode(RedLED, OUTPUT);
pinMode(YellowLED,OUTPUT);
pinMode(GreenLED,OUTPUT);
digitalWrite(RedLED, LOW);
digitalWrite(YellowLED, LOW);
digitalWrite(GreenLED, LOW);
}
void loop() {
if (irrecv.decode(&results)) {
irrecv.resume(); //After receiving, this must be called to reset the receiver and prepare it to receive another signal
sensorValue = analogRead(sensorPin); //reads value of Photoresistor
if (results.value == 0xFF30CF){ //activates code when this signal is received
if(sensorValue < 600){ // if light shining on photoresistor red and yellow LED turn on
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);
digitalWrite(YellowLED, HIGH);
sensorValue = analogRead(sensorPin); //reads value of Photoresistor
}
else{ // if no light shining on photoresistor green led LED turn on
digitalWrite(RedLED, LOW);
digitalWrite(GreenLED, HIGH);
digitalWrite(YellowLED, LOW);
sensorValue = analogRead(sensorPin); //reads value of Photoresistor
}}}}
SECOND SET OF CODES:
int GreenLED = 10;
int YellowLED = 12;
int RedLED = 13;
int sensorPin = A0;
int sensorValue = 0;
void setup(){
pinMode(RedLED, OUTPUT);
pinMode(YellowLED,OUTPUT);
pinMode(GreenLED,OUTPUT);
digitalWrite(RedLED, LOW);
digitalWrite(YellowLED, LOW);
digitalWrite(GreenLED, LOW);
}
void loop() {
sensorValue = analogRead(sensorPin); //reads value of Photoresistor
if(sensorValue < 600){ // if light shining on photoresistor red and yellow LED turn on
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);
digitalWrite(YellowLED, HIGH);
sensorValue = analogRead(sensorPin); //reads value of Photoresistor
}
else{ // if no light shining on photoresistor green led LED turn on
digitalWrite(RedLED, LOW);
digitalWrite(GreenLED, HIGH);
digitalWrite(YellowLED, LOW);
sensorValue = analogRead(sensorPin); //reads value of Photoresistor
}}