high/low- on/off read through bluetooth

i want to read the status to tell its high or low once only whenever the pin 7 status is changed
so, code is running in loop is there method to reply once and wait until the status changes and then reply again.hope this time made less mistakes

#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);
void setup() {
  Serial.begin(9600);
  Serial.print("Arduino is ready");
  BTserial.begin(9600);
  BTserial.print("Bluetooth is ready");
}
void loop() {
  int INPIN = 7;
  int val = 0;
  pinMode(INPIN, INPUT);
  val = digitalRead(INPIN);
  switch (val) {
    case 0:
      BTserial.println("off");
      break;
    case 1:
      BTserial.println("on");
      break;
  }
  delay(1000);
}

See the "State Change" example.

i want to read the status to tell its high or low once only whenever the pin 7 status is changed

How are you going to tell that it has changed status, if you only read it once? You must read the pin over and over. You can WRITE only once when the state changes.

Why are you setting the mode of the pin in loop()?

Look at the state change detection example.

yes worked thanks,but it does not show the status at start,when changed its working with some issues like again continue to print off/on

#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);
int INPIN = 7;
int val = 0;
int previousval =0;
void setup() {
  Serial.begin(9600);
  Serial.print("Arduino is ready");
  BTserial.begin(9600);
  BTserial.print("Bluetooth is ready");
  pinMode(INPIN, INPUT);
}
void loop() {
 val = digitalRead(INPIN);
  if (val != previousval){
    if (val == HIGH){
      BTserial.println("on");
    }
    else if(val == LOW) {
   BTserial.println("off");   
    }
     delay(1000);
  }
previousval=val;
}

can i stop it printing off&on when the wire is removed.not able to read the data at very first

Ransher:
yes worked thanks,but it does not show the status at start,when changed its working with some issues like again continue to print off/on

So, once again we will ask you how the switch is actually wired.

i am not using a switch,pin 7 is either grounded or connected to 5v.
is there need of switch?can't code be run prefectly without swith?if no why?

is there need of switch?

No, but there IS a need for a pullup or pulldown resistor if you do not want a floating pin problem, like you have now. Fortunately, the Arduino has internal pullup resistors on all pins, activated using INPUT_PULLUP as the mode.

floating problem is solved,thanks.
last one thing why its not showing status when code is uploaded first time,when the wire is removed then connected it works why so?

oh okay, it will detect the status changed as perviousval=val;

code is made in that way
got it thanks
but there may be other way to detect and tell the present status
i will find out that