Trying to get code to only run once

I'm trying to get a portion of a sketch to run only one time. Here's what I have written:

  //Handshake
  while (count == 0) {
    Serial.print(255, BYTE);
    Serial.print(1, BYTE);
    incoming = Serial.read();
    if (int(incoming) == 255) {
      connection = Serial.read();
      if (int(connection == 1)) {
        count = 10;
      }
    }
  }

  //Set pump start time
  if (Serial.read() == 252) {
    B_Sw = Serial.read();
    if ((B_Sw == HIGH) && (connection == 1)) {
      if (count == 10) {
        pumpStartTime = millis();
        lcd.setCursor(0,0);
        lcd.print("pump start time");
        count = 50;
      }
    }
  }

I initialized count as 0. However, I know that the loop checking it count == 10 is being entered multiple time because the LCD screen prints pump start time multiple times.

I'm trying to create a timer that starts when a switch is flipped. Any suggestions?

I initialized count as 0.

Where?

Any suggestions?

Post all of your code (or, at least, more of it).

int count=0;
int B_Sw;

void setup() {
  Serial.begin(19200);
}

void loop() {
  //Handshake
  while (count == 0) {
    Serial.print(255, BYTE);
    Serial.print(1, BYTE);
    incoming = Serial.read();
    if (int(incoming) == 255) {
      connection = Serial.read();
      if (int(connection == 1)) {
        count = 10;
      }
    }
  }

  //Set pump start time
  if (Serial.read() == 252) {
    B_Sw = Serial.read();
    if ((B_Sw == HIGH) && (connection == 1)) {
      if (count == 10) {
        pumpStartTime = millis();
        lcd.setCursor(0,0);
        lcd.print("pump start time");
        count = 50;
      }
    }
  } 
.
.
.
.

This is one of the units in a wireless communication setup. I want them to be connected before continuing, so I need them to run through a loop until they're connected and then never enter it again. That's what I was trying to do with the while() loop. As for the pumpStartTime, I just want that to be set at the time that the B_Sw is set high.

if (int[glow]([/glow]incoming[glow])[/glow] == 255) {
      connection = Serial.read();
      if (int[glow]([/glow]connection == 1[glow])[/glow]) {
        count = 10;
      }
    }

looks suspicious. You are also reading data without checking Serial.available(), and I see no declaration of 'incoming'

Are you sure you're not writing to 'count' ANYWHERE else in your code? Search for it....

http://www.ruggedcircuits.com

@drhex: I have the type-casting of incoming as an integer because it's originally a BYTE and I wasn't sure if I could do a comparison between a number and a BYTE. But you're right, the second thing you pointed out is wrong. I was trying to do another type-cast.

Thanks!

@RuggedCircuits: Thank you for pointing out the obvious answer. That is what was wrong...

Thanks for your help!