I need help with send and receiving serial!

right now in my code i can only put in 1 digit lines and I want to know how to type in lines like “turn led on” in to the serial. Can someone help me with that?

int ledPin = 13;

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

void loop()
{
  while (Serial.available() == 0);
  int val = Serial.read();
  if (val == '1')
  {
    Serial.println("LED is ON");
    digitalWrite(ledPin, HIGH);
  }
  else if (val == '0')
  {
    Serial.println("LED is OFF");
    digitalWrite(ledPin, LOW);
  }
  else
  {
    Serial.println("Invalid");
  }
  Serial.readString();
}

mattmac553:
right now in my code i can only put in 1 digit lines and I want to know how to type in lines like “turn led on” in to the serial. Can someone help me with that?

int ledPin = 13;

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

void loop()
{
  while (Serial.available() == 0);
  int val = Serial.read();
  if (val == ‘1’)
  {
    Serial.println(“LED is ON”);
    digitalWrite(ledPin, HIGH);
  }
  else if (val == ‘0’)
  {
    Serial.println(“LED is OFF”);
    digitalWrite(ledPin, LOW);
  }
  else
  {
    Serial.println(“Invalid”);
  }
  Serial.readString();
}

Here’s a line input function that I wrote a long time ago and use it for everything. The code should be self explanatory.

char buffer [32];
char name [16];
int age;


void setup (void)
{
	Serial.begin (115200);

	sprintf (buffer, "\r\nPlease enter your name: ");
	Serial.print (buffer);

	readline (buffer, 16); // read user name (16 chars limit)
	strncpy (name, buffer, 16); // copy entered name into "name"

	sprintf (buffer, "\r\nPlease enter your age: ");
	Serial.print (buffer);

	readline (buffer, 16); // get user input
	age = atoi (buffer); // convert typed ascii to int

	sprintf (buffer, "\r\nHello %s, you are %d years old!\r\n", name, age);
	Serial.print (buffer);
}

void loop (void)
{
}

// read a line from user into buffer, return char count
int readline (char *buf, int limit)
{
	int ptr = 0;
	char c;

	*buf = 0; // clear first char in buffer

	while (1) {
		if (Serial.available()) {
			c = Serial.read();

			if (c == 0x0D) { // cr == end of line
				return ptr; // return char count
			}

			if (c == 0x08) { // backspace
				if (ptr > 0) {
					ptr--;
					buf[ptr] = 0;
					Serial.print ( (char) 0x08); // erase bs'd char
					Serial.print ( (char) 0x20);
					Serial.print ( (char) 0x08);

				} else {
					Serial.print ("\a");
				}

			} else {
				if (ptr < (limit - 1)) {
					Serial.print ( (char) c);
					buf[ptr] = c;
					ptr++;

				} else {
					Serial.print ("\a");
				}
			}
		}
	}
}

The “readline” function takes a pointer to a buffer, an int which says how many chars to accept (to avoid overrunning the buffer) and it returns a character count of how many characters were typed.

For your use, you would use atoi() to get the integer value of an ascii number in the buffer. You can use atof() to get a floating point number instead (for example, to get the proper value of “1234.567”).

Hope this helps.

I want to know how to type in lines like "turn led on"

Simple serial test 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

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);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}