Serial available returns 0

not sure if this topic goes here, but my Serial.available() always return 0; and I am not sure why. I even copied the code on arduino site and still 0. What am I missing?

I also read somewhere to do delay(); I did that too

I also read to close out arduino IDE and restart the board, I did that too.

Are you sending your code (which you forgot to post) anything?

well even the simplest thing won't do anything... say this one on arduino site, it doesn't work. gives me nope

int incomingByte = 0; // for incoming serial data

void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps }

void loop() {

// send data only when you receive data: if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read();

// say what you got: Serial.print("I received: "); Serial.println(incomingByte, DEC); } else Serial.println("nope"); }

And is the serial monitor or whichever terminal emulator you're using set to 9600 bits per second too? If using the serial monitor, are you clicking on the send control?

Please remember to use code tags when posting code.

Oh so sorry about the codes.

Yes, I am hitting send and yes it's at 9600.

Very simple code for turning the arduino LED on/off.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED ON");
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
      Serial.println("LED OFF");
    }
    readString="";
  } 
}

Your loop() function runs about 100,000 times a second.

So drop this

  else Serial.println("nope");

and you may see your echoed char insteand of a 100,000 "nope"'s

Mark

holmes4: Your loop() function runs about 100,000 times a second.

Not at 960 characters per second it doesn't ;)

Not at 960 characters per second it doesn't

Sure it does, because most of the iterations, there will be nothing to do.

To prove @PaulS's point temporarily add delay(1000); as the last line in loop() in the code in Reply #2

The examples in serial input basics are simple and reliable.

...R

PaulS:
Sure it does, because most of the iterations, there will be nothing to do.

Except print “nope”.

pinktoadette3: well even the simplest thing won't do anything... say this one on arduino site, it doesn't work. gives me nope

Working as intended then. What code did you copy and paste?

well even the simplest thing won't do anything.

Besides which, I don't call printing "nope" not doing anything.

It is printing "nope", right?

Yea it is printing "nope". It really baffles me.

void loop()

...gets called over and over. Very fast.

        // send data only when you receive data:
        if (Serial.available() > 0) {

...most of the time, there is no Serial data available. Which makes the condition false. Because the condition is false, the else-clause...

        else Serial.println("nope");

...runs.

Make sense?

so it's not called because it goes really fast? What if there's delay();? I tried that that didn't seem to work either.

No, it'll be called as often as needed - the delay is just so that you can see what's going on.

pinktoadette3: Yea it is printing "nope". It really baffles me.

I wonder if you are looking at the piece of code as a "block" or "chunk" and wondering why it is not working.

You need to think about each line of code one at a time. When I get stuck with a problem I find it very useful to take a paper and pencil and go through the code line by line writing down the values as they arise and following the path of the code through the different IF and ELSE statements.

In this case work through the code once pretending that no data has arrived in the serial buffer and then work through it again pretending that a character has arrived in the buffer.

Keep in mind that at 9600 baud characters cannot arive faster than about 1 per millisecond. loop() repeats many times per millisecond.

...R

loop() repeats many times per millisecond.

It would, if "Serial.print" were not being kept busy waiting for a slot in the outgoing buffer to come free, because it is printing "nope" a lot.