Problems Controlling relays via Serial

I have an eight-relay board I have established I can control via my Arduino Mega 2560. When I tried controlling it with a sketch I found here http://bildr.org/2011/01/arduino-serial/, however, my LEDs and relays start on and open, respectively, with the final sketch offered there - that is :

void setup(){
  Serial.begin(9600);

  //Set all the pins we need to output pins
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
 }

void loop (){
  if (Serial.available()) {

    //read serial as a character
    char ser = Serial.read();

    //NOTE because the serial is read as "char" and not "int", the read value must be compared to character numbers
    //hence the quotes around the numbers in the case statement
    switch (ser) {
      case '0':
        triggerPin(2);
        break;
      case '1':
        triggerPin(3);
        break;
      case '2':
        triggerPin(4);
        break;
      case '3':
        triggerPin(5);
        break;
      case '4':
        triggerPin(6);
        break;
      case '5':
        triggerPin(7);
        break;
      case '6':
        triggerPin(8);
        break;
      case '7':
        triggerPin(9);
        break;
    } 
  }
}

void triggerPin(int pin){
  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
  delay(250);
}

and the devices don't stay in the state more than the delay (250ms). I tried an IF/ELSE statement, but it breaks functionality altogether.

void triggerPin(int pin){
 if (pin == HIGH)
  {
   digitalWrite(pin, LOW);
   delay(250);
  }
  else if (pin == LOW)
  {
   digitalWrite(pin, HIGH);
   delay(250);
}
}

Am I missing something? My goal is to be able to turn each of the relays on and for them to stay on until I send a command to turn off. And, why is it starting in an on state and not off?

Serial.read() produces an int, doesn't it ? Not sure if that is actually your problem.

Well your second version of the function definitely won’t work.

‘pin’ is the number of the pin. What do you think you are doing by checking if it is HIGH or LOW ?

Trying to alternate. If it's high and it gets a command, I want to to go low and vice versa.

you have 2 choces:

1st: if you want to send on off to each relais you must tell relaisnumer and state so you need to send to your arduino board for example sendserial(relaisnumer,state) like sendserial(5,1) oder (5,0) to switsch relais 5,

ok ? 2nd: i do it in another way, it works fine

i send a byte (8 bit) and so i set the relais in binary text it is like:

sendserial(0x00001111)

in my arduino i read this byte

and now all you need is to logic AND

relais-state of r1 is like r1 = value && 0x00000001; r2 = value && 0x00000010; and so on.....

you can do this in a switch case like you did

i hope it works, i had a project like this some weeks ago, if you need more help let me know.....

I'm sorry, I found that hard to follow. Can you post your working sketch?

As Michinyon notes, you're testing the pin number, not its state. This:

 if (pin == HIGH)

Should be this:

 if (digitalRead(pin) == HIGH)