Won't detect Serial.read most of the time.

Hi.

I'm trying to create something which requires me to type some simple commands into the serial monitor.

But since I was having trouble I tried to create a very simple code to see if the problem persisted. And it did.

Since I am still waiting for my arduino board to be shipped to me I am doing my preliminary work on an arduino emulator. I am using both http://123d.circuits.io/, and a paid version of the software found at www.virtualbreadboard.com. Both emulator's are showing the same problem.

The simple code is:

import muvium.compatibility.arduino.*; 

public class testtest extends Arduino{ 

    public void setup()
    {   
        Serial.begin(9600); 
    }

    public void loop()
    {
        while (Serial.available() == 0);

        if (Serial.read() == '1')
        {
            Serial.println("Nr. 1 works");
        }   
        else if (Serial.read() == '2')
        {
            Serial.println("Nr. 2 works");
        }
        else if (Serial.read() == '3')
        {
            Serial.println("Nr. 3 works");
        }   
        else if (Serial.read() == '4')
        {
            Serial.println("Nr. 4 works");
        }
        else if (Serial.read() == '5')
        {
            Serial.println("Nr. 5 works");
        }
        else if (Serial.read() == '6')
        {
            Serial.println("Nr. 6 works");
        }
        else if (Serial.read() == '7')
        {
            Serial.println("Nr. 7 works");
        }
        else if (Serial.read() == '8')
        {
            Serial.println("Nr. 8 works");
        }
        else if (Serial.read() == '8')
        {
            Serial.println("Nr. 8 works");
        }
        else if (Serial.read() == '9')
        {
            Serial.println("Nr. 9 works");
        }

    }
}

Now here I should be able to simply type a number and having the same number returned to me in a sentence.

But what happens is that more than half the time that I type in a number, there is no response in the serial monitor. To get a response I mostly have to type in a number several times like: 1111111 or 222222. But stil only some of those numbers triggers the reaction I want.

I am extremely new to arduino so there might be some simple thing that I have done wrong, but it is really frustrating.

Please advice.

Thank you

        if (Serial.read() == '1')
        {
            Serial.println("Nr. 1 works");
        }

You read the character off the serial input. You can't read it again. read() takes it out of the buffer.

You need to keep your own copy. Then you can do several tests on that copy.

Look at the simple reliable examples in serial input basics. They store the incoming data in a char array which you can read as often as you like.

...R

MorganS: You read the character off the serial input. You can't read it again. read() takes it out of the buffer.

You need to keep your own copy. Then you can do several tests on that copy.

How do I keep a copy?

In a variable.

Robin2: Look at the simple reliable examples in serial input basics. They store the incoming data in a char array which you can read as often as you like.

...R

Hi Robin.

I have gone through your tutorial and understand very little of it. It feels as though it is targeted towards someone with more basic knowledge than me.

Anyways.. I can understand the first part about receiving and sending one character and then having the "This is just in..." message displayed. But What if I want the program to do something different with each command... like:

if I enter "1" it lights up an LED on pin 2. if I enter "b" it lights up LED's on pin 2 and 4.

Then i't's not enough to have a static function that simply reads the character that's been entered and then send that character back to the serial monitor...

Like this:
[copy]
if (Serial.available()>0){
incomingByte = Serial.read();
switch (incomingByte){
case '1':
// code - lights up an LED on pin 2, turns off others (like 4)
break;
case 'b':
// code - lights up LED's on pin 2 and 4
break;
:
:
case 'z':
// code - lights up LED's on pin, turns off others (like 2)
break;
} // end switch
} // end serial available test

mcbogel: But What if I want the program to do something different with each command.

Think of the problem as two almost separate parts.

One part reads in the data. The other part examines the data and decides what to do with it.

You could modify the code in the function showNewData() to examine the data and get other things to happen.

...R

CrossRoads:

Like this:

[copy]
if (Serial.available()>0){
incomingByte = Serial.read();
switch (incomingByte){
case ‘1’:
// code - lights up an LED on pin 2, turns off others (like 4)
break;
case ‘b’:
// code - lights up LED’s on pin 2 and 4
break;
:
:
case ‘z’:
// code - lights up LED’s on pin, turns off others (like 2)
break;
} // end switch
} // end serial available test

Thank you… SO MUCH!!!

Your code worked and I have already implemented it into my real code.
It works like a charm and the characters I enter is registered and the commands executed perfectly.

So again… thank you very much.
I really appreciate it.

Some basic serial command code.

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

//A very simple example of sending a string of characters 
//from the serial monitor, capturing the individual 
//characters into a String, then evaluating the contents 
//of the String to possibly perform an action (on/off board LED).

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.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED ON");
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
      Serial.println("LED OFF");
    }

    readString="";
  } 
}