Having some serious trouble

I'm still new to programming, so try to hang in there with me. I'm trying to learn how to use and call functions properly as well.

In my code, I'm trying to input a number using the getanwer() function and then print it to the serial moniter.

Here is my code:

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

void loop() { long length=getanswer(0); Serial.print(length); }

long getanswer(int totallength) { if(Serial.available()) { int number; char ch=Serial.read(); if(isDigit(ch)) { number=(number*10)+(ch-48); } else if(ch==10) { totallength=number; return totallength; } } }

Every time I run this program, it does not let me enter any value or anything and it gives me just a continous set of numbers that I did not enter.

I need help :astonished: :roll_eyes: :cold_sweat: :

Hi and welcome.

Your sketch is not waiting for any characters to be received. If no characters are available it just keeps printing a random number, because it gets to the end of your getanswer() function without executing a return command. Even if there is a character to receive, it does not wait for the remaining characters to complete the number typed. There are quite a few other errors also, but lets take them one at a time. Have a look at while().

Also forum etiquette is that when you post a sketch here, you should always use code tags (the # button). It prevents long sketches from filling the screen, and stops certain character sequences used in sketches from getting turned into smileys! Code tags do this:

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

void loop()
{
  long length=getanswer(0);
  Serial.print(length); 
}

long getanswer(int totallength)
{
  if(Serial.available())
  {
    int number;
    char ch=Serial.read();
    if(isDigit(ch))
    {     
      number=(number*10)+(ch-48);
    }
    else if(ch==10)
    {
      totallength=number;
      return totallength;
    }
  }
}

Paul

long getanswer(int totallength)
{
  if(Serial.available())
  {
    int number;
    char ch=Serial.read();
    if(isDigit(ch))
    {     
      number=(number*10)+(ch-48);
    }
    else if(ch==10)
    {
      totallength=number;
      return totallength;
    }
  }
}

You may have noticed that the return type of "getanswer" is "long", but if you actually return a value, it is only an "int".

Okay gotcha. Would I use a while function for it wait for the characters? Because I've used that getanswer() function to input numbers before, I just don't know why I'm having troubles with it now

Maybe you were using the serial monitor, and your sketch had some big delays that allowed the function to work as you thought it should. Remember, serial comms are slowwwwwww.

zwood14: Okay gotcha. Would I use a while function for it wait for the characters? Because I've used that getanswer() function to input numbers before, I just don't know why I'm having troubles with it now

Yes, a loop to wait for some characters to become available. Then another loop to read and deal with each character.

That version of getanswer () can never have worked, not like that anyway.

okay, so maybe set a boolean and in the while() loop, see if it true or false?

And do you have any suggestions on ways to read characters?

It is certainly a learning experience to have a go at using Horner's Method to parse integers, but you might want to look at Serial.parseInt().

Also note that the compiler should have given you a warning message about the getanswer() function. Something like "not all code paths return a value". The function should always return a value to the caller. In this case it should have a return statement when Serial.available() is 0 and when ch is not equal to 10.

You might also check out the Serial method named:

Serial.readBytesUntil(character, buffer, length)

A common use is to read serial data from the Serial object until the user presses Enter. When that happens, a newline character ('\n') is sent, signaling the end of input. If the longest message is, say, 15 bytes, then:

char myArray[16];   // Need an extra byte for the null character
int numberRead;
// More of your code...

   numberRead = Serial.readBytesUntil('\n', myArray, 15);
   myArray[numberRead] = '\0';
// more code??
   return atol(myArray);
// the rest of your code

This fragment would keep reading the input stream until the newline is read after the user pressed the Enter key. It will read no more than 15 characters. The method returns the number of bytes actually read, so the next statement places a null character at the end of the input stream so you can treat it as a string. You could then use the standard library atol() to convert the ASCII characters to a long.

That works GREAT!! butttt...it keeps printing numbers, what could I do to just print the number I want to input?

zwood14: That works GREAT!! butttt...it keeps printing numbers, what could I do to just print the number I want to input?

http://gammon.com.au/serial