SOLVED can I use the monitor to send a word?

I can send one character from the monitor with

void setup() {

  Serial.begin(9600);
}
void loop() {

  while (Serial.available()>0){
 int   message = Serial.read();
    Serial.println (message);
  }

but is it possible to send a word or a sentence that could then be assigned to a variable or array?

I think you need to add a short delay after serial read

void setup() {

  Serial.begin(9600);
}
void loop() {

  while (Serial.available()>0){
 int   message = Serial.read();
   delay(100);
    Serial.println (message);
  }

but I googled that so I could be wrong., at the very least there might be a better way. From what I gather, delays are bad...

DO NOT USE A DELAY!

Drop the point less while.

 int message;
void setup() {

  Serial.begin(9600);
}
void loop() {

  if (Serial.available()>0){
   message = Serial.read();
    Serial.println (message);
  }

Now add the array. Watch your scope! (look it up).

Mark

no, all serial.read gives me is the ascii code for each letter I type, one at a time. I want to type a word or sentence and assign it to a variable so I can get an LCD to print it out.

matelot:
no, all serial.read gives me is the ascii code for each letter I type, one at a time. I want to type a word or sentence and assign it to a variable so I can get an LCD to print it out.

Very simple character capture code that captures the individual characters into the String readString, then sends the contents of readString back to the serial monitor.

//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    readString="";
  } 
}

thank you zoomkat that is exactly what I wanted. I have produced the finished section of the sketch in case anyone else wants to use it. It is just part of the total sketch but works quite well on its own. again thank you for your input.

/*
  LiquidCrystal Library - scrollDisplayLeft() 
for single line lcd screens.
  prints out anything you print in the monitor

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 6
 * LCD D5 pin to digital pin 5
 * LCD D6 pin to digital pin 4
 * LCD D7 pin to digital pin 3
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 This example code is in the public domain.
 with monitor read function from zoomkat in arduino forum.
 
 */
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);// I used 6,5,4,3 so the screen could be wired 
//with pins that go straight into the UNO
int i=0;
String readString;
String printout;
int a;

void setup() {
  Serial.begin(9600);
/*  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer   
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
    }  
  if (readString.length() >0) {
    a =(readString.length());
    Serial.println(a);
    printout = readString;
    Serial.println(readString);  //so you can see the captured String 
    readString="";
  } */
// set up the LCD's number of columns and rows: 
  lcd.begin(8,2);//a single line sees the lines as 40 long but only uses 1-8 and 40-48
}                // so you have to assign it as two columns of 8 and 2 rows

void loop() {    
    while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
    }
  
    if (readString.length() >0) {//if something has been entered in the monitor 
    a =(readString.length());               //measure its length
    Serial.println(a);                      //print its length
    printout = readString;//copy the input string to the variable 'printout'
    Serial.println(readString);  //so you can see the captured String 
    readString="";//reset to 0 to stop it repeating
    a = (a+15);//add a number of spaces to the length of the string
  } 
  Serial.println (i);    
  if (i>a){     //if a = length of string
    lcd.clear();//start string again
    i=0;        //start scroll again
  }
    lcd.setCursor(0,0);//set cursor to start of left half of screen
    lcd.print("                ");//let screen do full scroll from right
    lcd.print(printout);//print message
    lcd.setCursor(0,1);//set cursor to start of right half of screen
    lcd.print("        ");//let screen do full scroll from right
    lcd.print(printout);//print message
    lcd.print("        ");//allow blank screen to fill right half
    
  lcd.scrollDisplayLeft();//move message left on both halves of screen 
  i=i+1;
  delay(150);
}

Why are you reading from Serial in setup() and then repeating the code in loop() ? If you must do it in two places then using a function would be a better solution.

There are many here that suggest that using Strings is wasteful of resources on an Arduino and that C style strings (an array of chars) should be used instead.

Why are you reading from Serial in setup() and then repeating the code in loop() ?

I'm not :grin: I forgot to comment it out of the setup where I put it originally. I have done it now so in fact it could be removed completely. It works just the same with it in or out.

zoomkat:

...

delay(2);  //slow looping to allow buffer to fill with next character

Just FYI this is not the correct way of doing this. Delaying may or may not allow enough time for the buffer to fill. That is like putting the washing machine on, seeing that it says it will be done in 30 minutes, and after 30 minutes opening it up and hanging the washing out, without checking if it is actually done. For example, changing the baud rate changes the delay time.

http://www.gammon.com.au/serial

The above link shows how you can read a line of data, without using delay, and without “blocking”.

The easiest thing is to set the serial monitor to send a “newline” and then check for receiving that (as in the link above).

thank you Nick I will use that if you don't mind and thank you for your website, I often use it for hints about how to solve problems I have.
I also appreciate the bits on connecting an atmega chip on a bread board and bootloading an amega chip using an arduino and a bread board.
They have been used by me and I suspect many other people so I am grateful for them.
Please continue to add programming tips, they are all very welcome.

Here’s a lighter weight approach, avoiding String class (which isn’t the most
lightweight of things, and doesn’t keep strict guard on memory usage:

#define MAX_MSG_LEN 40
char buffer [MAX_MSG_LEN+1] ;
byte buf_ptr = 0 ;

void loop ()
{
  bool newline = false ;
  if (buf_ptr < MAX_MSG_LEN && Serial.available () > 0)
  {
    int ch = Serial.read () ;
    if (ch == '\n')
      newline = true ;
    else
      buffer [buf_ptr++] = ch ;
  }

  if (newline || buf_ptr >= MAX_MSG_LEN)  // whole line or buffer full
  {
    buffer [buf_ptr] = 0 ;  // NULL byte to terminate string
    buf_ptr = 0 ; // for next message
    Serial.println (buffer) ;
  }
}

Note the test for buffer-overflow - essential to always do this, the compiler
doesn’t generate array-bounds checks at all.

char * style strings need a zero (null) byte to terminate them,
note the +1 in the declaration of buffer to allow for this.

indexes start from 0, not 1 in C, BTW.

Because buffer is fixed size and we check the bounds the sketch cannot exhaust
memory (a risk with String if you aren’t careful - String used to have bugs
which made it a real pitfall, but now I think you can use it with an eye on
memory use).

  bool newline = false ;

The fatal flaw in that code is that it assumes that all the serial data will be available at once. That is not a valid assumption.

Make this variable global or static, and that assumption goes away.

If you have an idea of the max length of the message, say 25 characters, and you are using the Serial monitor to send the data, which means there's always a newline character ('\n') that terminates it, use the Serial object's readBytesUntil() method...makes things really easy.

#define MESSAGELENGTH    25

char message[MESSAGELENGTH + 1];    // Big enough for 25 chars + null
int bytesRead;

void setup() {

  Serial.begin(9600);
}
void loop() {
  if (Serial.available() > 0){
    bytesRead = Serial.readBytesUntil('\n', message, MESSAGELENGTH);
    message[bytesRead] = '\0';     // Make it into a string
    Serial.println (message);      // Show it
  }  
}

PaulS:

  bool newline = false ;

The fatal flaw in that code is that it assumes that all the serial data will be available at once. That is not a valid assumption.

Make this variable global or static, and that assumption goes away.

Nope, read the code carefully. There is a bug, which is that setup() isn't
defined, but that variable is local and correctly so.

but that variable is local and correctly so.

I still think it would be better to have newline be static and to reset it after the data in the buffer is used.

But, I guess that method works.