Arduino response time

when writing any type of program including just a simple program that outputs "read" when i send serial 1, however if for example I send 1 through the serial monitor say 15 consecutive times the serial monitor will return only 7 "read" same thing for any type of other hardware I tried to use. For example I have a relay switch and I have it set to where sending serial 3 will activate the relay 6 times. When sending serial 3 it works sometimes and other times it doesn't. Any ideas? I have tried using the resistor between 5v and and the reset pin to keep the arduino from resetting and I have also tried it without and I get the same problem. Any ideas?

thanks,

Can you post some code that exhibits your problem?

What do you mean when you say ‘I send serial 1’ ?

/* Blink Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain. */

void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode(13, OUTPUT); Serial.begin(9600); }

void loop() { if (Serial.read() == '3'){

int x = 3;

while (x!=0){ digitalWrite(13,HIGH); delay(500); digitalWrite(13,LOW); delay(500); x-=1; } digitalWrite(13,LOW); } if (Serial.read() == '1'){

Serial.print("Read"); }

}

Here is the code. what I mean by sending serial 1 is opening the serial monitor while my arduino is connected and typing in 1 and pressing enter.

OK, I'll preface this by saying that I'm still learning C on the Arduino, but I do have many many years of programming in other languages.

Your sample code is 'consuming' 2 bytes from the serial interface each time Loop() runs. Don't forget that serial.Read() removes the byte from the serial buffer, so unless you store it it's gone forever.

Say you send a bunch of characters down the serial line - "1313" for example. Loop() will read the first character and compare it against '3' - which fails. Then it will read the next character, and compare it against '1', and fail. Rinse lather, repeat. End result is that nothing will be output, and no LEDs flash.

You need to use a SWITCH CASE statement http://arduino.cc/en/Reference/SwitchCase, and possibly also a serial.available http://arduino.cc/en/Serial/Available test first.

Hope that makes sense and helps.

possibly also a serial.available

Read() returns -1 when there is nothing to read so one can include that in the switch statement. However Serial.available() makes the code better understandable imho.

As Slugsie suggests, each time you call Serial.read(), one byte is removed from the serial buffer. So instead of the multiple calls you want to do what the reference suggests: declare a variable to place your incoming byte into and only read it is Serial.Available is greater than 0.

    int incomingByte = 0; 
   if (Serial.Available > 0)  {
        incomingByte = Serial.Read();

        ..... your code

   }

Worked perfectly thanks everyone for the response.

:)