#include <IRremote.h>
//#include <ledControl.h>
#include <LiquidCrystal.h>
const int irReceiverPin = 2; // IR receiver pin assignments
IRrecv irrecv(irReceiverPin);
decode_results results;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); //change these values
const int led = 3; //led pin
const int potR = A0; //potentiometer
//Sensor Pin Assignments
const int trigPin = 51;
const int echoPin = 53;
const int servoPin;
String distanceThreshold = "100";
int distanceThresh = distanceThreshold.toInt(); //threshold in centimeters
float time, calculateDistance;
void setup() {
Serial.begin(9600);
//Initialize led
pinMode(led, OUTPUT);
// Initialize LCD
lcd.begin(16, 2);
// Initialize IR remote
irrecv.enableIRIn();
//Initialize potentiometer
pinMode(led, OUTPUT); //led PIN --fix
pinMode(3, INPUT); //Pontentiometer pin --fix
//Initialize Sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
int readPot(int pin) {
return map(analogRead(pin), 0, 1023, 0, 255);
}
boolean sensorMode = true;
boolean remoteMode = false;
boolean ledStatus = false;
void loop() {
//SENSOR MODE
if (sensorMode == true) {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
time = pulseIn(echoPin, HIGH);
remoteMode = false;
lcd.setCursor(0,1);
lcd.print("MODE: SENSOR");
calculateDistance = (0.034/2) * time;
//lcd.print(calculateDistance);
if (calculateDistance <= distanceThresh){
ledStatus = true;
digitalWrite(led, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LED: ON");
lcd.setCursor(0,1);
lcd.print("MODE: SENSOR");
analogWrite(led, readPot(potR));
}
else{
ledStatus = false;
digitalWrite(led, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LED: OFF");
lcd.setCursor(0,1);
lcd.print("MODE: SENSOR");
}
}
//CHECK REMOTE SIGNAL
if (irrecv.decode()) {
// Power button pressed
if (irrecv.decodedIRData.decodedRawData == 1570963200) {
if(remoteMode = true) {
sensorMode = true;
}
else if (sensorMode == true) {
remoteMode = true;
}
//REMOTE MODE
if (remoteMode == true){
lcd.setCursor(0,1);
lcd.print("MODE: REMOTE");
sensorMode = false;
}
}
if (irrecv.decodedIRData.decodedRawData == 1738080000) { //volume up
ledStatus = true;
digitalWrite(led, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LED: ON");
lcd.setCursor(0,1);
lcd.print("MODE: REMOTE");
} else if (irrecv.decodedIRData.decodedRawData == 4244832000) { //volume down
ledStatus = false;
digitalWrite(led, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LED: OFF");
lcd.setCursor(0,1);
lcd.print("MODE: REMOTE");
}
irrecv.resume(); // Receive the next value
}
//led IS TRUE
if (ledStatus == true){
//If the led is ON, the potentiometer will change the brightness of the led
int potValue = analogRead(4); //reads the potentiometer value
//--fix potentiometer pin
int ledValue = map(potValue, 0, 1023, 0 ,255);
digitalWrite(led, HIGH);
//maps the potentiometer value that ranges from 0 - 1023 to the led brightness which ranges from 0 - 255
analogWrite(3, ledValue); //--fix led Pin
}
delay(1000);
}