Stop Program looping

Hi

This is my first time at trying to program anything.

I have the Arduino Duemilanove Board.

I am trying to get the board to switch on or off 8 relays.

I have modified some code I have found on the internet and I am able to switch the 8 relays on and off ok. But the problem I am having is the command is being repeated so the relay is constantly switching on and off.

What I want is for a pulse to switch the relay on for 1 second then switch the relay off and wait for the next command.

here is the code I am using.

char val;         // variable to receive data from the serial port 
int ledpin = 2;  // LED connected to pin 2 to 9 (on-board LED) 

void setup() 
{ 
  pinMode(ledpin = 2, OUTPUT);  // pin 2 (on-board LED) as OUTPUT 
  pinMode(ledpin = 3, OUTPUT);  // pin 3 (on-board LED) as OUTPUT 
  pinMode(ledpin = 4, OUTPUT);  // pin 4 (on-board LED) as OUTPUT 
  pinMode(ledpin = 5, OUTPUT);  // pin 5 (on-board LED) as OUTPUT 
  pinMode(ledpin = 6, OUTPUT);  // pin 6 (on-board LED) as OUTPUT 
  pinMode(ledpin = 7, OUTPUT);  // pin 7 (on-board LED) as OUTPUT 
  pinMode(ledpin = 8, OUTPUT);  // pin 8 (on-board LED) as OUTPUT 
  pinMode(ledpin = 9, OUTPUT);  // pin 9 (on-board LED) as OUTPUT 
  Serial.begin(9600);       // start serial communication at 9600bps 
  Serial.println("Press 1 - 9 to toggle relay on/off");
} 
 
void loop() { 
  if( Serial.available() )       // if data is available to read 
  { 
    val = Serial.read();         // read it and store it in 'val' 
  } 
  if( val == '2' )               // if '2' was received 
  { 
    digitalWrite(ledpin = 2, HIGH);  // turn ON the LED 
    delay(1000);                  // waits for a second
    Serial.println("Relay 2 on");
    digitalWrite(ledpin, LOW);    // sets the LED off
    
  }
if( val == '3' )               // if '3' was received
 { 
    digitalWrite(ledpin = 3, HIGH);  // turn ON the LED 
    delay(1000);                  // waits for a second
    Serial.println("Relay 3 on");
    digitalWrite(ledpin, LOW);    // sets the LED off
  } 
  if( val == '4' )               // if '4' was received
  { 
    digitalWrite(ledpin = 4, HIGH);  // turn ON the LED 
    delay(1000);                  // waits for a second
    Serial.println("Relay 4 on");
    digitalWrite(ledpin, LOW);    // sets the LED off
  }
  if( val == '5' )               // if '5' was received
  { 
    digitalWrite(ledpin = 5, HIGH);  // turn ON the LED 
    delay(1000);                  // waits for a second
    Serial.println("Relay 5 on");
    digitalWrite(ledpin, LOW);    // sets the LED off
  }
  if( val == '6' )               // if '6' was received
  { 
    digitalWrite(ledpin = 6, HIGH);  // turn ON the LED 
    delay(1000);                  // waits for a second
    Serial.println("Relay 6 on");
    digitalWrite(ledpin, LOW);    // sets the LED off
  }
  if( val == '7' )               // if '7' was received
 { 
    digitalWrite(ledpin = 7, HIGH);  // turn ON the LED 
    delay(1000);                  // waits for a second
    Serial.println("Relay 7 on");
    digitalWrite(ledpin, LOW);    // sets the LED off
  }
  if( val == '8' )               // if '8' was received
  { 
    digitalWrite(ledpin = 8, HIGH);  // turn ON the LED 
    delay(1000);                  // waits for a second
    Serial.println("Relay 8 on");
    digitalWrite(ledpin, LOW);    // sets the LED off
  }
  if( val == '9' )               // if '9' was received
  { 
    digitalWrite(ledpin = 9, HIGH);  // turn ON the LED 
    delay(1000);                  // waits for a second
    Serial.println("Relay 9 on");
    digitalWrite(ledpin, LOW);    // sets the LED off
  }
}

Please can someone tell me where I'm going wrong.

Simon

Once you've processed a valid "command" using val, then you can set the val to a "do nothing" value until there's more input available.

Or you can move the "}" that you put right after the Serial.read() call. Move it to the end of the if statements. This will then only check the val immediately after a read, not on every loop.

Remember loop() loops constantly, forever. What is happening is tat every time loop() executes when there is no data, you still execute your if block and switch a relay. They way your program is structured it happens to be the last one you switched.

One way to stop that would be to wait until there is a character available before continuing the loop. This would cause loop() to block (stop execution) until it got a character to read.

Instead of the code

  if( Serial.available() )       // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'
  }

try this

  while (Serial.available() <1 )  // wait until there is data to read
  {;}
  val = Serial.read();         // read it and store it in 'val'

-j

Hi

Thanks for the quick response.

It's working perfectly now.

Thanks

Simon