Hi,
I'm writing a program where you have to enter numbers between 0 - 1000. While you're doing this and you think you have enough data, you should be able to cancel this process and continue with the next step.
I came up with two simple if-Statements but it seems like that, as soon as Serial.parseInt exists in the 2nd Statement, Serial.read cannot read any characters anymore.
As soon as I delete the parseInt Statement, it workes just fine. Why is this so? Do you have any ideas how I could realize this?
The following code is an example Sketch I have created to mess around with my idea...
More importantly, .parseInt() will throw away any characters it reads before it finds something that looks like the beginning of a number ('0'-'9', '-', maybe '+'). You can use Serial.peek() to see what character is coming next and only call '.parseInt()' if the next character is part of a number.
if (isDigit(Serial.peek()) || Serial.peek() == '+' || Serial.peek() == '-')
{
int nInput = Serial.parseInt();
if (nInput > 0){
Serial.println("Data recieved");
}
}
Below is a simple general purpose program to read commands and numbers using the SafeString library
Some sample output is
Input either stop or start cmds or an integer
stop
Found stop cmd
kdfj !!Invalid input!!
33.3
!!Invalid input!!
33a
!!Invalid input!!
334
New Int:334
It uses a non-blocking SafeStringReader to collect and tokenize the input and the SafeString.toInt() method to convert the ints
The SafeString.toInt() has a number of advantages over the Arduino toInt()
SafeString.toInt() rejects numbers like 33a
When SafeString.toInt() finds a invalid number it returns false and the int argument is left unchanged, the Arduino toInt() returns 0 as the converted number with no other indication of failure. So you cannot tell if the number was actually 0 or just invalid
// ReadCmdsAndNumbers.ino
//
// Example of NON-Blocking read commmands from the Arduino Monitor input and acts on them
// the available commands are start stop
// Commands are delimited by space dot comma NL or CR
// If you set the Arduino Monitor to No line ending then the last command will be ignored until it is terminated by a space or ,
// Use the settings Newline or Carrage Return or Both NL & CR
//
// These commands can be picked out of a line of user input
// start stop
// The input line can be as long as you like 100's of Kb long, but only two small buffers need to parse the commands
//
// download and install the SafeString library from
// www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
#include "SafeStringReader.h"
// create an sfReader instance of SafeStringReader class
// that will handle commands upto 20 chars long , for numbers
// delimited by space, comma or CarrageReturn or NewLine
// the createSafeStringReader( ) macro creates both the SafeStringReader (sfReader) and the necessary SafeString that holds input chars until a delimiter is found
// args are (ReaderInstanceName, expectedMaxCmdLength, delimiters)
createSafeStringReader(sfReader, 20, " ,\r\n");
int intValue = 0;
void setup() {
Serial.begin(9600);
for (int i = 10; i > 0; i--) { // pause a little to give you time to open the Arduino monitor
Serial.print(i); Serial.print(' '); delay(500);
}
Serial.println();
Serial.println(" Input either stop or start cmds or an integer");
SafeString::setOutput(Serial); // enable error messages and SafeString.debug() output to be sent to Serial
sfReader.connect(Serial); // where SafeStringReader will read from
sfReader.echoOn(); // echo back all input, by default echo is off
}
void handleStartCmd() {
Serial.println(F(" Found start cmd"));
}
void handleStopCmd() {
Serial.println(F(" Found stop cmd"));
}
void loop() {
if (sfReader.read()) { // non blocking read of input
if (sfReader == "start") {
handleStartCmd();
} else if (sfReader == "stop") {
handleStopCmd();
} else if (sfReader.toInt(intValue)) {
// got a new valid int
Serial.println(); Serial.print("New Int:"); Serial.println(intValue);
} else { // dont recognize this
Serial.println(" !!Invalid input!! ");
}
} // else no delimited command yet
// rest of code here is executed while the user typing in commands
}