This is one of a collection of pages which, together, attempt to show you "everything" about the Arduino's programming language.
There is a page for you with more information about the project in general, and the way these pages are organized, if you want that.
Please visit my page about power browsing notes sometime.
This page, and the software it references, ©TK Boyd, 1/2010.
We've seen how things can be displayed in the serial monitor window which can be opened from the IDE.
At the top of that window, there is an edit box and a "send" button, for sending things TO the Arduino.
We'll use it for a little calculator. Our calculator won't be very clever: It will merely tell us what twice anything we send it is. You should be able to guess that we are drawing on earlier programs to create this.
GOOD LORD! I had NO idea how big a project I was taking on with that "little joy"!
The following code does do what I suggested I would accomplish... but I'm not going to give you the usual detailed guide to every element.
I will discuss aspects of the code in a moment.
/*FEAa1OvertakingTallyStage2 29 Dec 09 Accepts input from the IDE's serial monitor, and sends the number doubled back to the serial monitor. */ int intFailedTries=0; void setup() { Serial.begin(9600);//Prepare serial port for use } void loop() { int intInput=0; intFailedTries=0; //do nothing (except continue to check) until something has been sent. while (Serial.available()==0) {;}; while ((Serial.available()>0) || (intFailedTries<1000)) {intInput=FetchNextCharacterAndAddToAccumulator(intInput);}; Serial.print("Two times "); Serial.print(intInput); Serial.print(" is "); intInput=intInput*2; Serial.println(intInput); } int FetchNextCharacterAndAddToAccumulator(int intAccumulator) { int intIncomingByte; intIncomingByte=intAsciiToBinary(Serial.read()); if (intIncomingByte!=-1) {intAccumulator=(intAccumulator*10)+intIncomingByte;}; intFailedTries++; return intAccumulator; } int intAsciiToBinary(int intIn) /*this function is for use while reading a number from the serial stream. It will convert the Ascii for digit to the binary for the same, and return -1 for all other input.*/ { int intTmp=-1; if ((intIn>47) && (intIn<58)) {intTmp=intIn-48;}; return intTmp; }
There's nothing special in the "setup" function.
All the problems with writing this program stem from the following three issues....
When we enter something in the serial monitor's edit box, and then click "send", HOW it is sent, and WHEN it is sent, and WHERE it is sent is not simple.
HOW it is sent: If we've entered, say, 47 in the edit box, then when we click send, the Ascii code for "4" is sent, and then the Ascii code for "7" is sent. The Ascii code for zero is 48, for "1" is 49, and so on up to 57 for "9".
WHEN it is sent: Sometime Real Soon Now... but there may be significant (to the computer) delays between sending individual characters.
WHERE it is sent: The characters from the edit box are sent to something called the serial buffer.
Happily, the Arduino language provides us with two essential functions....
Serial.available, and Serial.read
Serial.available is a function which returns a number. It will return zero if there is nothing in the serial buffer.
Serial.read will remove something from the serial buffer for us.
So, until I put something into the serial monitor's edit box, and click "send", Serial.available returns zero. That's why "loop" begins....
while (Serial.available()==0) {;};
... I am waiting for the user to have put something in the serial monitor's edit box and to have clicked "send".
As soon as the first character reaches the serial buffer, Serial.available will become more than zero. This will take us out of that first "do nothing" loop, and pass us to....
while ((Serial.available()>0) || (intFailedTries<1000)) {intInput=FetchNextCharacterAndAddToAccumulator(intInput);};
... where we will stay for a while. While we are in that loop, we will pick up and use digits from the serial buffer, as they arrive. If a non-digit appears, it will just be thrown away. EVERY time we go through the "while" loop, we will increment intFailedTries. When that reaches 1000, which happens very quickly, we will assume that no more characters are working their way through the system. (This is very crude... but works!)
On each interaction of the "while" loop, we go off to FetchNextCharacterAndAddToAccumulator. In that, if a character is available in the serial buffer, we fetch it from the buffer with Serial.read. We see if it is the Ascii for a digit. If it is, we convert it from Ascii to the number the Ascii stands for (that's the "-48"). We then take the old running accumulation, multiply it by 10, and add on the latest digit.
So, in overview, if "47" was entered in the edit box, first we fetch the 4. A moment later, we multiply that by 10, making 40, and add the 7, making a grand total of "47".
Who would have thought it could all be so difficult???? The problems stem from the extremely low level that the serial stream is working at. You don't send "a number" from the serial monitor, you send a stream of characters which, in our minds, represent a number. Converting those characters to a number, something we do without thinking about it, is heavy lifting for the Arduino.
But... through the joys of functions... you SHOULD be able to see most of what is going on in this program.
Look again at the "loop" function, which we have mostly gone through....
So far, we've had the "wait until there's something to process" loop.
Then there was the "fetch the characters from the serial buffer and put the equivalent number in intInput" part.
Next, we send some text to the serial monitor, telling users what they are looking at.
Then we double the number held in the variable intInput, and display the result.
Done!
An aside: Another feature of Good Programs is that their output is helpful.
This program would "do its job" if when you put in 47 it output 94... but it does its job WELL if when you put in 47 the program reports "Two times 47 is 94".
That's it! A shorter than usual tutorial, for once.
The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.
SPELL your search term properly. When I review search logs, it is amazing how many people ask the engine to search for something meaningless.
Why does this site cause a script to run? I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try it, check out their site. And if there are Google ads on the page, they are run with scripts, too.
Here is how you can contact this page's editor. This page, and the software it references, ©TK Boyd, 1/2010.
Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org