I am considering some projects that require user input to establish some parameters to be used in the program execution. Serial.read seems to offer a single byte but I would like to accept words and multi-digit numbers via Serial Monitor, Putty, etc. Will the CmdMessenger library handle this? If so, are there other sources which can further explain CmdMessenger? I'm having a hard time following the documentation/explanations at Dreamcat4's Github page so maybe it's not even my solution.
Thanks for the link but I guess I didn't correctly explain my question. The link goes into the user input during the loop function while I'm just looking for ways the Uno would query and record user ASCII-based entries used to define constants in the setup function.
hoboat33:
I'm just looking for ways the Uno would query and record user ASCII-based entries used to define constants in the setup function.
Don't understand that, sorry.
You can't have user input define something that can't be changed like constants.
I'll try to pose my problem differently. I'm wanting the Uno to run different programs based on the users entry, in one case the entry will be in degrees Fahrenheit. That entry, temperature, will be used to define a constant that will run in the program. Different temperatures make the program do different things.
I'm new to the Arduino and my programming experience it limited to Fortran and Basic (and that was many years ago). I started by doing the many examples on this site, at Adafruit and a couple of books as well as consulting with a couple of on-line C++ sites. It seems every thing I run across uses the Serial Monitor to input single character inputs, "1" for On, "2" for Off, etc.
what are you using for an input device? is this a pc with a serial connection (putty)? i have a similar issue in that i have to toggle between different data sets and would like the user to select those. if you only want integer values you could put a display and have the user twist a pot.
if you want to get fancy i just noticed that radioshack has a touch shield. http://www.radioshack.com/product/index.jsp?productId=12688460&utm_source=Google&utm_medium=PPC&utm_term=12410220&utm_content=Exact&utm_campaign=PLA&cagpspn=pla&gclid=CNDczoySxLUCFYMyMAodSVgAAA&gclsrc=ds and there are others out there. i've had good luck with these add-ons. easy!
technically you can't change the value of a constant - (#define) but i know what you mean. you've declared an int that is global and will update that value depending on the user input. in this case any old analog pot will do - but you still need a display.
Daveclark - Thanks for the suggestion. The pot might be a solution for part of it but I'm also needing whole words captured. I'm trying to find a solution that just uses the computer keyboard (via Serial Monitor, Putty, etc.) for the input. I don't want to write an intermediate MMI just to convert keyboard input to something usable in the sketches and am not familiar enough at this point with C++ to know the procedures for having the MMI within the sketch coding.
hoboat33:
Thanks for the link but I guess I didn't correctly explain my question.
Did you read it? That explains how to take numbers (eg. "12345" terminated by a newline) and turn them into something you can use in the sketch.
On my web page here I have an example of a programmer I wrote for Atmega chips. It asks questions, gets responses, and acts accordingly. Sample session:
Enter action:
F
LFuse = 0xFF
HFuse = 0xDE
EFuse = 0xFD
Lock byte = 0xCF
Choose fuse (LOW/HIGH/EXT/LOCK) ...
LOW
Current value of low fuse = 0xFF
Enter new value for low fuse (2 hex digits) ...
C2
WARNING: Fuse changes may make the processor unresponsive.
Confirm change low fuse from 0xFF to 0xC2 . Type 'YES' to confirm ...
YES
Changing low fuse ...
Fuse written.
LFuse = 0xC2
HFuse = 0xDE
EFuse = 0xFD
Lock byte = 0xCF
Lines in blue are user input. I am entering whole words there, and hex strings.
The code is on GitHub if you want to see how it was done.
Nick - Yes I read the link; however, I didn't think it was applicable (I assume you are meaning the Buffering Input section) when I read "...being able to do other things while the text is arriving." and "... inside the main "loop" function.." parts. I still went through that as well as the Machine State portion but did not see quite what I was looking for; or if I did, I didn't recognize it.
I was just hoping there was and ability, for example, to do a Serial.Read to capture user entered string "On", "Off", "###", etc. then do a Boolean test to see what was entered and then kick off the pertinent sections of the program. I was looking for these user entered messages to be via a dialog box like Serial Monitor or Putty. The examples I've been cranking through have all just dealt with a single byte to send commands to the Arduino.
If there is no way to do this without doing the work at byte level I'll write an intermediate MMI to feed the Arduino but I was hoping to avoid separating I/O from data acquisition and control.
There is no inbuilt Serial.read functionality that returns a string of characters but Nicks routine does it for you as in this example
char temperature[10];
void setup()
{
Serial.begin(9600);
Serial.print("Enter a temperature ");
getline( temperature, 20);
}
void loop()
{
}
void getline (char * buf, size_t bufsize)
{
byte i;
// discard any old junk
while (Serial.available ())
Serial.read ();
for (i = 0; i < bufsize - 1; )
{
if (Serial.available ())
{
int c = Serial.read ();
if (c == '\n') // newline terminates
break;
if (!isspace (c)) // ignore spaces, carriage-return etc.
buf [i++] = toupper (c);
} // end if available
} // end of for
buf [i] = 0; // terminator
Serial.println (buf); // echo what they typed
} // end of getline
UKHelibob - Thanks for your input in pointing out the portion applicable to my question. I'm just getting familiar with this and finding solutions that have already been made is part of my discovery process. It surprises me that there is no standard "getline" command nor Boolean handling of text strings, but it is what it is. Thanks again.
It surprises me that there is no standard "getline" command
Have you looked at the documentation for the Serial class?
Perhaps the readBytesUntil() method bears investigating.
Pauls - That just might work. Many thanks.
hoboat33:
It surprises me that there is no standard "getline" command nor Boolean handling of text strings, but it is what it is.
I should point out that in the link I posted above was a version of getline I wrote for the Arduino:
// get a line from serial (file name)
// ignore spaces, tabs etc.
// forces to upper case
void getline (char * buf, size_t bufsize)
{
byte i;
// discard any old junk
while (Serial.available ())
Serial.read ();
for (i = 0; i < bufsize - 1; )
{
if (Serial.available ())
{
int c = Serial.read ();
if (c == '\n') // newline terminates
break;
if (!isspace (c)) // ignore spaces, carriage-return etc.
buf [i++] = toupper (c);
} // end if available
} // end of for
buf [i] = 0; // terminator
Serial.println (buf); // echo what they typed
} // end of getline
That particular one ignores space, but you can change that.
The thing is, the Arduino isn't really intended for large-scale user input/output. And there are lots of ways data can arrive (different serial ports, software serial, I2C, Ethernet, etc.).
It isn't necessarily easy to write a library that supports all that, and even if you do, people won't necessarily find it.