About Me

My photo
They never fall who never climb.

007 TEST SERIAL INPUT-OUTPUT FEATURE


007 TEST SERIAL INPUT-OUTPUT FEATURE

January 24, 2011    (See also Companion Blog )
[Go BACK to Table of Contents] [ Next Experiment ] [ Last Experiment ]

007 TEST SERIAL INPUT OUTPUT FEATURE

           There is no brainboard wiring in this experiment, other than an LED in pin 13 to show that the circuit is working. The entire experiment is controlled by the code below and the purpose is to familiarize myself with the Serial read and write commands as interpreted by the Arduino code once it is uploaded. Shown here is a composite photograph that never actually appears on your computer monitor, as will be explained.

           Code matches sketch A100445_SerialReadWrite.pde.

Theory:
           The code is designed to read any keypress from your computer keyboard. It then adds one to that value and displays the results. An LED on pin 13 blinks to show that the circuit is operating.

Practice:
           Worked almost perfectly the first trial. The LED did not blink and if you’ll read the code, you’ll see the commands to make it work are considerably more complicated than the simple “Hello World” blink of experiment 005 described above. The modular box shown above was never mentioned in any of the text examples, so this may be a new feature of the Arduino Uno board.
           The box is certainly useful and a considerable improvement over the examples which show the output as scrolling past in that tiny black rectangle below the IDE code box. As shown here, I typed the phrase [Now is the time for all good men] and the lower text box displayed it back as a series of the next consecutive characters. I also tested it on punctuation and numbers with the corresponding same result.
           In real life, whatever you type in the upper box disappears when you click on the Send button, hence the photo shown here is contrived to demonstrate the effect. The results cannot be cleared and cannot be copied. There is no carriage return and long strings flow past the right margin. There is a newline control displayed but it either has no effect or converts the [/n] command to the meaningless [0o].

Conclusion:
           Much is left to think about. I learned the abbreviation for the microprocessor board is MCU. I see that all the serial data is interpreted as ASCII bytes, which is why the code can display the next consecutive value from the ASCII table (also called the collating sequence). If it sees an A, it echoes back B and so on.

Questions:
           What is not known? First of all, the directions said use one character at a time, but I typed in strings of characters. How did the Arduino know to read the next consecutive unread character? Put another way, how did it know the last character had already been read. When I typed in two batches, how did it know to append the second batch? Can it only read ASCII code? How did it ignore non-printing characters I typed which were also ASCII code (like the down arrow and control-D)? It is clear all these instructions are built into the unit, but should they not explain this a little better?

January 26, 2011
           Examining the program module concerning the LED leads me to believe there is at least one line of code missing. There is no condition that sets the LED to high. I’m now suspecting the original code listing which is split between page 41 and 42 in the manual. It is a poor-man’s e-reader, the booklet scanned as a pdf file.
           Moments later, although this did not turn out to be the problem, it sent me in the right direction. A typo in my original code had set the blink interval to 10, too fast to be seen. The code now works perfectly as described, although I don’t quite grasp why the LED flash instructions are so complex. I think it is so the LED flashes regularly despite the interrupts caused by the Serial commands.

March 27, 2011
           Examining the code after several more trials, I've concluded that it really does work as terribly as I thought, and I thought it was a mistake. The Arduino display is proof that computers are so simple, retarded people who can neither spell nor type can program them.

Here is the code:

/* January 25, 2011.
* First attempt to use Arduino serial monitor.
* This example found in "Making Arduino Toys Talk", page 41.
*/

/* This program takes incoming serial data, adds one to that
* data and sends the result back. There is a blinking LED pin
* to show the operation is working.
*/

// Assign variables
int LEDpin = 13; // use the built in LED
int inByte = 0; // storage location for incoming data
long vBlinkTimer; // keeps track of last LED off state
int vBlinkInterval = 1000; // LED timer
// End of assign variables

// Begin Main Program

void setup()
{
pinMode(LEDpin, OUTPUT); // set pin 13 to output
Serial.begin(9600); // configure serial port
}

void loop()
// check if there are any serial bytes to read
{
if (Serial.available() >0)
{
inByte = Serial.read(); // if so, read the first byte
Serial.print(inByte+1, BYTE);
}

if (millis() - vBlinkTimer >= vBlinkInterval / 2)
{
digitalWrite(LEDpin, HIGH); // light LED pin 13
}

if (millis() - vBlinkTimer >= vBlinkInterval)
{
digitalWrite(LEDpin, LOW);
vBlinkTimer = millis(); // reset the timer
}
}

// End Main Program

//////////////////////////////////////////////////////////////
// I found the timer code more interesting than the serial. //
// although I never could get it to work right. //
// Keyentered January 25, 2011 by da Bassguy. //
// Uploaded and tested same date. //
//////////////////////////////////////////////////////////////
// Note my convention that variables assigned initial values//
// are given cryptic names, but variable which are to //
// contain input or output values always begin with a "v". //
// That's a Deltacomm™ standard. //
//////////////////////////////////////////////////////////////

Go BACK to Table of Contents