Something Wrong?

int buz = 3;
void setup() {
Serial.begin(9600);

pinMode(buz, OUTPUT);

}

void loop() {
//wait until the serial connection is open
while (Serial.available() == 0);

//read from the serial connection; the - '0' is to cast the values as the int and not the ASCII code
int COM_value = Serial.read() - '0';

//print to the console for testing
Serial.println(COM_value);
for (int x = 0; x < COM_value; x++) {
digitalWrite(buz, HIGH);
delay(1000);
digitalWrite(buz, LOW);
}
}

i have put a led on Digital Port 3 and this is my code. I want it to run like... Enter value... LED on... wait for 1 sec... LED off... repeat as many rimes as the value of COM_value... exit loop... wait for a new value to be entered. Now what is happening is that the LED just stays on on the first value .. and Serial wont accept new values... The led just stays on.

Not sure if I understand the problem correctly. Maybe this is what you mean?

Assume Com_value equals 3

LED on
delay
LED off
no delay
LED on
delay
LED off
no delay
LED on
delay
LED off

Because there is no delay between off and on, it looks like the LED is permanently on.

Please edit your post and use code tags
type
** **[code]** **

paste your code after that
type
** **[/code]** **

It will then look like this :wink:

int buz = 3;
void setup() {
  Serial.begin(9600);

  pinMode(buz, OUTPUT);

}

void loop() {
  //wait until the serial connection is open
  while (Serial.available() == 0);

  //read from the serial connection; the - '0' is to cast the values as the int and not the ASCII code
  int COM_value = Serial.read() - '0';

  //print to the console for testing
  Serial.println(COM_value);
  for (int x = 0; x < COM_value; x++) {
    digitalWrite(buz, HIGH);
    delay(1000);
    digitalWrite(buz, LOW);
  }
}

If what you want to do is,

if COM_value=3, blink the LED three times then there is a problem.

You missed a 'delay(1000);'

int buz = 13;
void setup() {
  Serial.begin(9600);

  pinMode(buz, OUTPUT);

}

void loop() {
  //wait until the serial connection is open
  while (Serial.available() == 0);

  //read from the serial connection; the - '0' is to cast the values as the int and not the ASCII code
  int COM_value = Serial.read() - '0';

  //print to the console for testing
  Serial.println(COM_value);
  for (int x = 0; x < COM_value; x++)
  {
    digitalWrite(buz, HIGH);
    delay(1000);
    digitalWrite(buz, LOW);
    delay(1000);
  }
}

By using long delay()s for your blinking you are prevented from getting more serial data.

Use millis() to manage timing without blocking as illustrated in Several Things at a Time

Also use one the examples in Serial Input Basics to receive your data. They are simple, reliable and non-blocking.

...R

ichangimath thank you so much.. i feel so silly i missed the delay. Thanking the others too. Now what the problem is that SOMETIMES instead of running the number of times as COM_value it just keeps repeating the loop forever.... how do i prevent this prom happening
P.S.
I have interfaced this with PHP

<?php // SPECIFY USB PORT TO USE $usb_comPort = "COM3"; switch($_POST) { case isset($_POST['b1']): exec("ECHO 1 > $usb_comPort"); // Ring Bell once break; case isset($_POST['b2']): exec("ECHO 2 > $usb_comPort "); // Ring Bell twice break; case isset($_POST['b3']): exec("ECHO 3 > $usb_comPort"); // Ring Bell thrice break; case isset($_POST['b4']): exec("ECHO 4 > $usb_comPort"); // Ring Bell 4 times break; case isset($_POST['b5']): exec("ECHO 5 > $usb_comPort"); // Ring Bell 5 times break; case isset($_POST['b6']): exec("ECHO 6 > $usb_comPort"); // Ring Bell 6 times break; case isset($_POST['b7']): exec("ECHO 7 > $usb_comPort"); // Ring Bell 7 times break; case isset($_POST['b8']): exec("ECHO 8 > $usb_comPort"); // Ring Bell 8 times break; } ?>

Control Panel

"> this is my php side code...I tried to enter through serial monitor and it worked but when i used php to send data to com3 it does the forever loop(sometimes it works too)