buttonState HIGH Serial.write repeatedly

hi all please help about my problem

im sending string to serial when switch is HIGH but how to send onetime?

here is my code

int emergency_switch = 8;
int limit_switch_a = 9;
int limit_switch_b = 10;

String message1 = "Emergency Switch is closed";
String message2 = "Limit Switch A is closed";
String message3 = "Limit Switch B is closed";
String message4 = "Emergency Switch and Limit Switch A is closed";
String message5 = "Emergency Switch and Limit Switch B is closed";

int buttonstate;
int lastbuttonstate;

void setup(){
Serial.begin(9600);
pinMode(emergency_switch, INPUT);
pinMode(limit_switch_a, INPUT);
pinMode(limit_switch_b, INPUT);

}

void loop(){
emergency_switch = digitalRead(emergency_switch);
limit_switch_a = digitalRead(limit_switch_a);
limit_switch_b = digitalRead(limit_switch_b);

while (emergency_switch == HIGH){
Serial.println(message1); //this would be repeatedly because of emergency switch still high or press

/* my question is how to send one time only to the serial

   this is my first try to send one time only but we cannot proceed to other while loop
   [b]how to call another while loop?[/b]*/

  //while (true) {};  

}

while ( limit_switch_a == HIGH){
Serial.println(message2); //this would be repeatedly because of limit_switch_a still high or press

}

while ( limit_switch_b == HIGH){
Serial.println(message3); //this would be repeatedly because of limit_switch_b still high or press

}

while (emergency_switch == HIGH &&  limit_switch_a == HIGH){
Serial.println(message4); //this would be repeatedly because of emergency emergency switch and limit_switchswitch_a still high or press

}

while (emergency_switch == HIGH &&  limit_switch_b == HIGH){
Serial.println(message5); //this would be repeatedly because of emergency switch and limit_switch_b still high or press

}

}

Have a look at the statechange example that comes with the IDE. The idea is that you only send something when you detect a change from low-to-high or high-to-low (or both).

It is not a good idea to use the String (capital S) class as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

…R