#include <IRremote.hpp>
#include<LiquidCrystal.h>
#define MOTOR_EN_1_2 11
#define MOTOR_IN1 10
#define MOTOR_IN2 4
#define PHOTORESISTOR A0
#define LED_RED 3
#define LED_GREEN 5
#define GAS_SENSOR A1
//LCD
#define LCD_RS A3
#define LCD_EN A4
#define LCD_D4 6
#define LCD_D5 7
#define LCD_D6 8
#define LCD_D7 9
LiquidCrystal lcd(LCD_RS,LCD_EN,LCD_D4,LCD_D5,LCD_D6,LCD_D7);
#define slow 64
#define normal 128
#define fast 255
//LCD Modes
const char LCD_MODE_MotorwithLDR=0;
const char LCD_MODE_MotorwithGasSensor=1;
const char LCD_MODE_WithRemote=2;
const char LCD_MODE_Automatic=3;
int InitialLCD_MODE;
int LCD_MODE;
int IntensityValue;
int sensorValue;
//photoresistor
const int THRESHOLD_value= 500;
int PreviousValueofPhotoresistor;
const int Min_value=6;
const int Max_value=679;
unsigned long InitialTimeofGasSensor=millis();
unsigned long GasSensordelayduration=30;
int PreviousValueofGasSensor;
const int THRESHOLD_valueofSensor=200;
const int Min_valueSensor=10;
const int Max_valueSensor=380;
//pushbuttons
#define PB1 12
unsigned long InitialpresstimeButton1=millis();
unsigned long PB1delayduration=60;
byte buttonstatePB1=LOW;
#define PB2 13
unsigned long InitialpresstimeButton2=millis();
byte buttonstatePB2=LOW;
//IR
#define IR_RECV 2
#define IR_UP 0xF50ABF00
#define IR_DOWN 0xF708BF00
#define IR_EQUAL 0xFA05BF00
#define IR_STOP 0xFD02BF00
IRrecv irrecv(IR_RECV);
//decode_results results;
unsigned long PhotoresistorStartTime=millis();
unsigned long PhotoresitorDelay=50;
void forward_rev()
{
analogWrite(MOTOR_EN_1_2, slow);
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
}
void backward_rev()
{
analogWrite(MOTOR_EN_1_2, normal);
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, HIGH);
}
void brake()
{
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, LOW);
}
bool is_motor_running=false;
long command;
int MotorFunctionwithPhotoresistor(int IntensityValue)
{
if(!is_motor_running){
if(IntensityValue<=THRESHOLD_value && IntensityValue!=PreviousValueofPhotoresistor)
{
forward_rev();
delay(10);
brake();
}
if(IntensityValue> THRESHOLD_value && IntensityValue!=PreviousValueofPhotoresistor )
{
backward_rev();
delay(10);
brake();
}
if(IntensityValue==Max_value or IntensityValue==Min_value)
{
brake();
}
PreviousValueofPhotoresistor=IntensityValue;
//Serial.println(IntensityValue);
return IntensityValue;
}
is_motor_running=false;
}
int MotorFunctionwithGasSensor(int sensorValue)
{
if(!is_motor_running){
if(sensorValue<=THRESHOLD_valueofSensor && sensorValue !=PreviousValueofGasSensor)
{
forward_rev();
delay(10);
brake();
}
if(sensorValue>THRESHOLD_valueofSensor && sensorValue !=PreviousValueofGasSensor)
{
backward_rev();
delay(10);
brake();
}
if(sensorValue==Max_valueSensor or sensorValue==Min_valueSensor)
{
brake();
}
PreviousValueofGasSensor=sensorValue;
//Serial.println(sensorValue);
return sensorValue;
}
is_motor_running=false;
}
bool LDRMode=false;
bool SensorMode=false;
void selectMode()
{
if(!is_motor_running){
if(LCD_MODE==LCD_MODE_MotorwithLDR){
MotorFunctionwithPhotoresistor(IntensityValue);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MotorwithLDR");
lcd.setCursor(0,1);
lcd.print(IntensityValue);
}
if(LCD_MODE==LCD_MODE_MotorwithGasSensor)
{
MotorFunctionwithGasSensor(sensorValue);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MotorwithSensor");
lcd.setCursor(0,1);
lcd.print(sensorValue);
}
}
is_motor_running=false;
}
void toggleLCDScreen()
{
switch(LCD_MODE){
case LCD_MODE_MotorwithLDR:
{
LCD_MODE =LCD_MODE_MotorwithGasSensor;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select for");
lcd.setCursor(0,1);
lcd.print("Gas Mode");
break;
}
case LCD_MODE_MotorwithGasSensor:
{
LCD_MODE= LCD_MODE_MotorwithLDR ;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select for");
lcd.setCursor(0,1);
lcd.print("LDR Mode");
break;
}
}
}
void stopMotor()
{
if(LCD_MODE==LCD_MODE_MotorwithLDR or LCD_MODE==LCD_MODE_MotorwithGasSensor){
brake();
lcd.clear();
lcd.print("function stopped");
}
}
void IRCommands(long command)
{
switch(command){
case IR_UP:
{
toggleLCDScreen();
break;
}
case IR_DOWN:
{
toggleLCDScreen();
break;
}
case IR_EQUAL:
{
selectMode();
break;
}
case IR_STOP:
{
stopMotor();
break;
}
}
}
void toggleInitialLCDscreen()
{
switch(InitialLCD_MODE){
case LCD_MODE_WithRemote:{
InitialLCD_MODE=LCD_MODE_Automatic;
break;
}
case LCD_MODE_Automatic:{
InitialLCD_MODE=LCD_MODE_WithRemote;
break;
}
default:{
//do nothing
}
}
if(InitialLCD_MODE==LCD_MODE_WithRemote){
lcd.setCursor(0,0);
lcd.print("Select for");
lcd.setCursor(0,1);
lcd.print("Automatic controlled");
}
else{
InitialLCD_MODE=LCD_MODE_Automatic;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select for");
lcd.setCursor(0,1);
lcd.print("Remote control");
}
}
void SelectSetting()
{
if(InitialLCD_MODE==LCD_MODE_WithRemote){
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Automate mode");
lcd.setCursor(0,1);
lcd.println("Activated");
digitalWrite(LED_RED,HIGH);
}
else{
digitalWrite(LED_RED,LOW);
}
if(InitialLCD_MODE==LCD_MODE_Automatic){
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Remote mode");
lcd.setCursor(0,1);
lcd.println("Activated");
digitalWrite(LED_GREEN,HIGH);
}
else{
digitalWrite(LED_GREEN,LOW);
}
}
bool AutomatedMode=false;
bool RemoteMode=false;
void OperateSettings(long command,int sensorValue,int IntensityValue)
{
if(!RemoteMode && digitalRead(LED_GREEN)==HIGH){
IRCommands(command);
RemoteMode=false;
}
if(!AutomatedMode && digitalRead(LED_RED)==HIGH){
AutomatedMotor(sensorValue,IntensityValue);
AutomatedMode=false;
}
}
int AutomatedMotor(int sensorValue,int IntensityValue)
{
if(sensorValue<=THRESHOLD_valueofSensor && sensorValue !=PreviousValueofGasSensor)
{
if(IntensityValue<=THRESHOLD_value && IntensityValue!=PreviousValueofPhotoresistor){
forward_rev();
delay(10);
brake();
}
}
if(sensorValue>THRESHOLD_valueofSensor && sensorValue !=PreviousValueofGasSensor)
{
if(IntensityValue> THRESHOLD_value && IntensityValue!=PreviousValueofPhotoresistor ){
backward_rev();
delay(10);
brake();
}
}
if(sensorValue==Max_valueSensor or sensorValue==Min_valueSensor)
{
if(IntensityValue==Max_value or IntensityValue==Min_value){
brake();
}
}
PreviousValueofGasSensor=sensorValue;
Serial.println(sensorValue);
return sensorValue;
PreviousValueofPhotoresistor=IntensityValue;
Serial.println(IntensityValue);
return IntensityValue;
}
void setup() {
Serial.begin(9600);
pinMode(MOTOR_EN_1_2,OUTPUT);
pinMode(MOTOR_IN1,OUTPUT);
pinMode(MOTOR_IN2,OUTPUT);
pinMode(GAS_SENSOR,INPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(PB1,INPUT);
pinMode(PB2,INPUT);
IrReceiver.begin(IR_RECV,ENABLE_LED_FEEDBACK);
lcd.begin(16,2);
lcd.print("Initializing...");
delay(100);
lcd.clear();
}
void loop() {
unsigned long TimeNow=millis();
if(TimeNow-InitialpresstimeButton1>PB1delayduration)
{
byte newbuttonstatePB1=digitalRead(PB1);
if(buttonstatePB1!=newbuttonstatePB1)
{
InitialpresstimeButton1=TimeNow;
buttonstatePB1=newbuttonstatePB1;
if(buttonstatePB1==HIGH)
{
toggleInitialLCDscreen();
}
}
}
if(TimeNow-InitialpresstimeButton2>PB1delayduration)
{
byte newbuttonstatePB2=digitalRead(PB2);
if(buttonstatePB2!=newbuttonstatePB2)
{
InitialpresstimeButton2=TimeNow;
buttonstatePB2=newbuttonstatePB2;
if(buttonstatePB2==HIGH)
{
SelectSetting();
}
}
}
if(TimeNow-InitialTimeofGasSensor>GasSensordelayduration){
InitialTimeofGasSensor+=GasSensordelayduration;
PhotoresistorStartTime+=GasSensordelayduration;
IntensityValue= analogRead(PHOTORESISTOR);
sensorValue= analogRead(A1);
}
OperateSettings(command,sensorValue,IntensityValue);
if(irrecv.decode()){
irrecv.resume();
command= irrecv.decodedIRData.decodedRawData;
Serial.println(command, HEX);
}
}
Hey Everyone,
This is just a prototype project for smart blinds. There is still a lot more to be done. I don't understand why the IR receiver stops working midway. It only outputs as 0 in the serial monitor after a first few inputs. Please let me know if it is anything in the code that causes this.
Thank you for your time