Problem with IR signal Transmission

I am using arduino to control my appliances in my room and my hometheatre through IR LED…
The IR LED is controlling fine, but after transmitting it is not getting turned off, it is still transmitting signal.
For example,
If I send power command,
the hometheatre switches on and after a second it switches off again.
How to rectify it…?
What changes to make to in the coding…?

I have attached my coding.

Final.ino (3.46 KB)

I think you really want:-if(serial.available()>0)notwhile(serial.available())
And even so, you’re relying on a full message having arrived.

Then, even if nothing is received, “a” already holds the last String received, so the action will be repeated on each iteration of ‘loop(0)’

You could clear “a” after any action is taken, then only your first conditional would execute:-

if (a == "shutdown")
{
    digitalWrite(RELAY1, HIGH);
    digitalWrite(RELAY2, HIGH);
    digitalWrite(RELAY3, HIGH);
    digitalWrite(RELAY4, HIGH);
    digitalWrite(RELAY5, HIGH);
    digitalWrite(RELAY6, HIGH);
    digitalWrite(RELAY7, HIGH);
    digitalWrite(RELAY8, HIGH);
    delay(500);
    a="";    // <<<<<<<<<<<<<<<<<<<< Clear "a"
}

If nothing is received, (or after clearing “a” as above), this will always execute:-

if (a == "")
{
    digitalWrite(RELAY1, HIGH);
    digitalWrite(RELAY2, HIGH);
    digitalWrite(RELAY3, HIGH);
    digitalWrite(RELAY4, HIGH);
    digitalWrite(RELAY5, HIGH);
    digitalWrite(RELAY6, HIGH);
    digitalWrite(RELAY7, HIGH);
    digitalWrite(RELAY8, HIGH);
    delay(100);
}

Is that what you want?
These lines might be better in ‘setup()’ instead:-

digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);
digitalWrite(RELAY7, HIGH);
digitalWrite(RELAY8, HIGH);

then you could remove this conditional from ‘loop()’:-

if (a == "")
{
    digitalWrite(RELAY1, HIGH);
    digitalWrite(RELAY2, HIGH);
    digitalWrite(RELAY3, HIGH);
    digitalWrite(RELAY4, HIGH);
    digitalWrite(RELAY5, HIGH);
    digitalWrite(RELAY6, HIGH);
    digitalWrite(RELAY7, HIGH);
    digitalWrite(RELAY8, HIGH);
    delay(100);
}

There might be more, but I’m busy on something else right now. The above should help get you started.

One other thing. You shouldn’t need to explicitly make pin 3 LOW. The IRremote library should take care of that.

Edit: I should point out that this isn’t really a “Sensors” question. It should be in “Programming Questions”

For others, here’s Balu_Vna’s code:-

#include<IRremote.h>

String a;
IRsend irsend;
#define RELAY1  2
#define RELAY2  4
#define RELAY3  5
#define RELAY4  6
#define RELAY5  7
#define RELAY6  8
#define RELAY7  9
#define RELAY8  10
void setup()
{
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
    pinMode(3, OUTPUT);
    pinMode(RELAY1, OUTPUT);
    pinMode(RELAY2, OUTPUT);
    pinMode(RELAY3, OUTPUT);
    pinMode(RELAY4, OUTPUT);
    pinMode(RELAY5, OUTPUT);
    pinMode(RELAY6, OUTPUT);
    pinMode(RELAY7, OUTPUT);
    pinMode(RELAY8, OUTPUT);
}

void loop()
{
    while (Serial.available())
    {
        a = Serial.readString(); // read the incoming data as string
    }
    if (a == "")
    {
        digitalWrite(RELAY1, HIGH);
        digitalWrite(RELAY2, HIGH);
        digitalWrite(RELAY3, HIGH);
        digitalWrite(RELAY4, HIGH);
        digitalWrite(RELAY5, HIGH);
        digitalWrite(RELAY6, HIGH);
        digitalWrite(RELAY7, HIGH);
        digitalWrite(RELAY8, HIGH);
        delay(100);
    }
    if (a == "shutdown")
    {
        digitalWrite(RELAY1, HIGH);
        digitalWrite(RELAY2, HIGH);
        digitalWrite(RELAY3, HIGH);
        digitalWrite(RELAY4, HIGH);
        digitalWrite(RELAY5, HIGH);
        digitalWrite(RELAY6, HIGH);
        digitalWrite(RELAY7, HIGH);
        digitalWrite(RELAY8, HIGH);
        delay(500);
    }
    if (a == "power up")
    {
        digitalWrite(RELAY1, LOW);
        digitalWrite(RELAY2, LOW);
        digitalWrite(RELAY3, LOW);
        digitalWrite(RELAY4, LOW);
        digitalWrite(RELAY5, LOW);
        digitalWrite(RELAY6, LOW);
        digitalWrite(RELAY7, LOW);
        digitalWrite(RELAY8, LOW);
        delay(500);
    }
    if (a == "light on")
    {
        digitalWrite(RELAY1, HIGH);
        delay(500);
    }
    else if (a == "light off")
    {
        digitalWrite(RELAY1, LOW);
    }
    if (a == "cfl on")
    {
        digitalWrite(RELAY2, HIGH);
        delay(500);
    }
    else if (a == "cfl off")
    {
        digitalWrite(RELAY2, LOW);
    }
    if (a == "fan on")
    {
        digitalWrite(RELAY3, HIGH);
        delay(500);
    }
    else if (a == "fan off")
    {
        digitalWrite(RELAY3, LOW);
    }
    if (a == "hometheatre on")
    {
        digitalWrite(RELAY4, HIGH);
        delay(500);
    }
    else if (a == "hometheatre off")
    {
        digitalWrite(RELAY4, LOW);
    }
    if (a == "nightlamp on")
    {
        digitalWrite(RELAY5, HIGH);
        delay(500);
    }
    else if (a == "nightlamp off")
    {
        digitalWrite(RELAY5, LOW);
    }
    if (a == "plug1 on")
    {
        digitalWrite(RELAY6, HIGH);
        delay(500);
    }
    else if (a == "plug1 off")
    {
        digitalWrite(RELAY6, LOW);
    }
    if (a == "plug2 on")
    {
        digitalWrite(RELAY7, HIGH);
        delay(500);
    }
    else if (a == "plug2 off")
    {
        digitalWrite(RELAY7, LOW);
    }
    if (a == "plug3 on")
    {
        digitalWrite(RELAY8, HIGH);
        delay(500);
    }
    else if (a == "plug3 off")
    {
        digitalWrite(RELAY8, LOW);
    }
    if (a == "pwr")
    {
        irsend.sendNEC(0xEF28D7, 32);
        digitalWrite(3, LOW);
    }
    if (a == "mute")
    {
        irsend.sendNEC(0xEF6897, 32);
        digitalWrite(3, LOW);
    }
    if (a == "cd")
    {
        irsend.sendNEC(0xEF50AF, 32);
        digitalWrite(3, LOW);
    }
    if (a == "reset")
    {
        irsend.sendNEC(0xEF20DF, 32);
        digitalWrite(3, LOW);
    }
    if (a == "bassup")
    {
        irsend.sendNEC(0xEFA05F, 32);
        digitalWrite(3, LOW);
    }
    if (a == "tv")
    {
        irsend.sendNEC(0xEF10EF, 32);
        digitalWrite(3, LOW);
    }
    if (a == "dvd")
    {
        irsend.sendNEC(0xEF906F, 32);
        digitalWrite(3, LOW);
    }
    if (a == "bassdwn")
    {
        irsend.sendNEC(0xEF609F, 32);
        digitalWrite(3, LOW);
    }
    if (a == "prev")
    {
        irsend.sendNEC(0xEF708F, 32);
        digitalWrite(3, LOW);
    }
    if (a == "usb")
    {
        irsend.sendNEC(0xEFA857, 32);
        digitalWrite(3, LOW);
    }
    if (a == "nxt")
    {
        irsend.sendNEC(0xEF30CF, 32);
        digitalWrite(3, LOW);
    }
    if (a == "voldwn")
    {
        irsend.sendNEC(0xEF807F, 32);
        digitalWrite(3, LOW);
    }
    if (a == "volup")
    {
        irsend.sendNEC(0xEF00FF, 32);
        digitalWrite(3, LOW);
    }
    if (a == "pp")
    {
        irsend.sendNEC(0xEFB04F, 32);
        digitalWrite(3, LOW);
    }
}

Thank you...!!! :slight_smile:
Its working...!!!

Balu_Vna:
Thank you...!!! :slight_smile:
Its working...!!!

Oh good. That was easy. :slight_smile: