IF statement runs even though condition is not satisfied

hey guys, here is the program i am using for my project. Now if pin 5 is applied voltage from outsource, pin 2 becomes high..i.e. through IF condition...but here if the outsource voltage is not applied then also IF statements (the last IF condition) run and pin 2 becomes high..PLz..Help.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7,8);
int led1 = 4;
const int SW = 5;
const int led2 = 2;
int val = 0;
const byte address[6] = "00001";

void setup()
{
pinMode(led1, OUTPUT);
pinMode(SW, INPUT);
pinMode(led2, OUTPUT);

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);

radio.begin();
radio.openReadingPipe(0,address);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS);
radio.startListening();
}

void loop()
{
char text[32] = "";

if(radio.available())
{
radio.read(&text, sizeof(text));
String transdata = String(text);
if(transdata == "robot all")
{
digitalWrite(led1, HIGH);

}
if(transdata == "robot 01")
{
digitalWrite(led1, HIGH);

}
if(transdata == "robot off")
{
digitalWrite(led1, LOW);

}
}

val = digitalRead(SW);
delay(1000);

if (val == HIGH)
{
digitalWrite(led1, LOW);
delay(5000);
digitalWrite(led2, HIGH);
delay(30000);
digitalWrite(led2, LOW);
}
}

The problem description is not easy to follow, but have you a pullup or pull down resistor on the input pin 5 ? From the code, I guess it should be a pull down resistor (of about 10K).

How is the input wired ?
Do you have a pulldown resistor in place or is the pin floating at an unknown voltage ?

yes, there is 10k pull down resistor.

You don't appear to have any code to switch the LEDs when SW goes LOW. How are you determining that the "if condition" is not behaving correctly ?

You'll have other problems because of the long delays in the loop() and should consider another solution.

radio.read(&text, sizeof(text));

should be

radio.read(text, sizeof(text));

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Simple nRF24L01+ Tutorial

...R

arduino_new:

radio.read(&text, sizeof(text));

should be

radio.read(text, sizeof(text));

Nope.

&text works fine and is consistent with what you would have to do if you were not using an array

It is one of the silly inconsistencies in C/C++ that the & is not necessary with an array

...R

I do not have any problems regarding radio...it works fine. the problem is that the LED2 mentioned in the code should get HIGH only when SW is high but the LED2 gets HIGH even when the value of SW is LOW.

Where does the command data come from? If it's from Serial Monitor, try adding \r to your test cases: "robot all\r"

String class is bad, don't use it. But if you insist on using it, try using equals() to compare instead of ==.

transdata.equals("...");

the problem is that the LED2 mentioned in the code should get HIGH only when SW is high but the LED2 gets HIGH even when the value of SW is LOW.

How do you know that the value of val changes in the program ?
Personally I would print its value before testing it in the code.

arduino_new:
String class is bad, don't use it. But if you insist on using it, try using equals() to compare instead of ==.

transdata.equals("...");

The equals method is identical to ==. There's no advantage to using equals instead of ==.