Delay question

Hi I am very new in programming for the arduino and I’ve come across something wired. my code explains it more but here is an explanation any way :}.

Well my program turns a led on and off with the pc. So while programming I put text in certain spots to see what the program was doing and when I got everything working I removed it but when I run the program it does nothing. So after some debugging I found out that a delay on 2 different spots lets the program work. But why do I need to put them in there ?

Here is code:


int command[20];
int number = 0;
int ledPin = 13;

void setup() {
      Serial.begin(9600);
        pinMode(ledPin, OUTPUT);     
}

void loop() {
      if (Serial.available() > 0) {
            command[number] = Serial.read();
                delay(1);                      // why does it lock up if i dont put this in here?
                number++;
        }
        else
        {
           int t = ArraySize(command);
           if (t > 0)
           {
               int count = 0;
               for (int i = 0; i < t; i++)
               {
                 count = count + command[i];
                 command[i] = NULL;
               }
               delay(2);                        // why does it lock up if i dont put this in here?
               if (count == 221) // on
               {
                 On();
               } 
               if (count == 315) // off
               {
                 Off();
               }
           }
           number = 0;
        }
}

void On()
{
        Serial.println("The led is on.");
        digitalWrite(ledPin, HIGH);
}

void Off()
{
        Serial.println("The led is off.");
        digitalWrite(ledPin, LOW);
}

int ArraySize(int array[])
{
        int i = 0;
        while(array[i] != NULL) i++;
        return i;
}

Well not sure I understand what your sketch is doing but I loaded it as is and if I send a single character from the serial monitor the LED13 blinks momentarily. I then commented out your two delays and the program seemed to work the same, so I guess I don't see your problem.

Lefty

I had the same issue as precious roy in some code I was working on last week. No delay, and it was like the Serial.read statement didn’t work. A short delay, and Serial.read worked just fine.

Unless the delay is in an interrupt handler, though, a 1 microsecond delay is not likely to be noticeable.

My theory was that the Serial.read returns as soon as the transfer starts. If the next instruction is started before the transfer completed, the transfer seemed to be aborted. The delay allowed the transfer to complete, so the read was successful. Could be all wet, though.

just like PaulS said that what I mean. I have a arduino the a atmaga328 don't know if that matters.

what my code does is when the serial monitor is open:

and write on it turns the led connected to pin 13 and ground on

and when you write off it turns it off that's all

I just tried your code on an Arduino Mega; works the same with and without the delay(x) statements.
"on" and "off" control the onboard LED.

hmm that's weird. oh well works ether way. thanks for the help.