HOME - - - - - - - - - Other material for programmers - - - - - - - - - Arduino Tutorial Table of Contents

Arduino Programming: The question of output.


This is a special Arduino tutorial with no parallel in my related Pascal tutorials.

Having spent most of the day on it, I am, at the end of that day, a little discouraged by the conclusion that maybe it was "A good idea at the time, but...." I probably invented an "answer" for a "problem" that doesn't (usually) exist.

Having said that, what is here DOES work! You don't NEED to do the tutorial before going on to others, but it might amuse you, and, while it unlikely to be directly useful, struggling through it would sharpen your Arduino programming teeth.

In the previous tutorials my Arduino course, the fanciest output we have used is one or more LEDs. The time has come to consider doing more.

I would really, really commend to you a simple LCD panel. Good, cheap ($30), ones are available, and they interface with the Arduino really nicely, taking almost no overhead.

But I realize that not everyone wants to, or has yet gone that route. Nor, I discover (late in the day!) do you need either an LCD panel, or the ideas in this tutorial to have access to alphanumeric output during program development. The free, comes with the system, alternative is explained in a short tutorial addressing just that topic. Oh well.... I had fun creating the program, and the text below to explain it!

Therefore, in this tutorial I am going to build something that will be used heavily in future tutorials. It won't make complete sense yet, but I hope you'll find that you can use it, anyway. You don't NEED to understand all of the details. Don't let them worry you!

Don't be daunted by the length of the tutorial. There's a lot of repetition of code as we build towards a final answer. And you can use copy & paste to move the code from here to your Arduino.

As presented below, the code is right for an Arduino with a serial LCD panel connected (more in a moment), or an Arduino with at least two LEDs connected (more in a moment), or an Arduino with BOTH connected!

Serial LCD panel connected: I've written what follows with an LCD panel connected with just 3 wires: One for 5v, one for ground (0v), and one for data from the Arduino to the panel, connected to the "Digital Line 1" connection, pin 2 of J1. Aka "Tx" and "TXD". Yes, inside the Arduino that line is also used when uploading programs to the Arduino. That's okay. The LCD panel I have incorporates an LCD controller from ModernDevice.com. There are acceptable alternatives, the most certain to work with my program being the others based on Peter Anderson's #117 PIC LCD controller.

At least two LEDs connected: These LEDs can be on any otherwise unused digital lines. In the example, I'm using lines 13, 12 and 11. Each resistor should be connected, via a resistor, to ground (0v). The lines used can be changed quite easily in the program. I've specified "13, 12, 11" because, given that the Arduino has an LED on line 13 on the Diecimila board, that's likely to be used for LEDs by many people. Line 12 is next door. If you have to cut the lines used for LEDs down, I'd drop the one on 11 first, hence it being last in the list.

For the sake of the little demo program that follows, but NOT as part of my general answer to allowing all student's Arduinos to "talk", we also need switches on lines 8 and 9. Push button switches: Ideal. Wire them up so that when pushed it connects an Arduino pin to ground, and they are pulled high when the switch is open.

So... set up your Arduino as above. Note that it merely builds on what you had at the end of the previous tutorial. Be sure that the LEDs work and the inputs are "seen". (Test them as you tested the first of each, in the previous tutorial.) Then enter the following....

/*plt1c2a_1
First program for plt1c2a.htm */


/*From here down to "end of first block for "tkbOutput"
is code needed whenever using "tkbOutput". ("tkb being
the author's initials, and an attempt to keep THIS
set of output options separate from other "output" routines!

tkbOutput is some code to allow students to follow the
examples in the Arduino programming course at...
http//:sheepdogsoftware.co.uk/pltut.htm
... whether they have a LCD panel or not.*/

#define tkbOutLEDPin0 13 /* (No ; after #define) Configures program to
    run on hardware with an LED on digital line 13, which
    makes sense as there's already one on that line, on the
    Diecimila itself. You can also have a "bigger, better"
    LED attached. Just be sure to connect it through a suitable
    resistor to the ground line.
 You can move the resistors, have them driven by other lines.
    If you do, JUST change these DEFINEs to reflect your
    hardware choices */
#define tkbOutLEDPin1 12
#define tkbOutLEDPin2 11

#define tkbOutUseLED 1 /*Make 1 if you want LED output, 0 if you don't*/
#define tkbOutUseLCD 1 /*Make 1 if you want LCD output, 0 if you don't*/

byte tkbOutMsg;
/*End of first block for tkbOutput*/


/*Start of a block of things only needed for the demo...*/
#define Sw0 8 /* Configure the program to look at digital
  line 8 when asked to read "Sw0"... "SWitch zero"*/
#define Sw1 9
/*End of things for demo*/

void setup()
{
/*Start, second block of things for tkbOutput*/
  pinMode(tkbOutLEDPin0, OUTPUT);
  pinMode(tkbOutLEDPin1, OUTPUT);
  pinMode(tkbOutLEDPin2, OUTPUT);
  digitalWrite(tkbOutLEDPin0,LOW); //Establish initial state
      /*N.B.:  LOW turns LED OFF!*/
  digitalWrite(tkbOutLEDPin1,LOW); //Establish initial state
  digitalWrite(tkbOutLEDPin2,LOW); //Establish initial state
/*End, second block of things for tkbOutput*/
}

void loop()
{
  if (digitalRead(Sw0)==0) {digitalWrite(tkbOutLEDPin0,HIGH);}; //If switch pressed, turn LED on
  if (digitalRead(Sw1)==0) {digitalWrite(tkbOutLEDPin1,HIGH);}; //If switch pressed, turn LED on
  /*Run it like this, and then change the second line to....
  if (digitalRead(Sw1)==0) {digitalWrite(tkbOutLEDPin2,HIGH);};
  ... to make sure that the third LED can be turned on.*/
}

Once you get that into your Arduino, you should find that pressing the first button turns on the first LED, and pressing the second turns on the second. It doesn't (yet) attempt to do anything with your LCD, if you have one. It doesn't matter to the program how many LEDs (0-3) you have, or whether you have an LCD attached... or not.

The second phase is....

/*plt1c2a_2
Second program for plt1c2a.htm */


/*From here down to "end of first block for "tkbOutput"
is code needed whenever using "tkbOutput". ("tkb being
the author's initials, and an attempt to keep THIS
set of output options separate from other "output" routines!

tkbOutput is some code to allow students to follow the
examples in the Arduino programming course at...
http//:sheepdogsoftware.co.uk/pltut.htm
... whether they have a LCD panel or not.*/

#define tkbOutLEDPin0 13 /* (No ; after #define) Configures program to
    run on hardware with an LED on digital line 13, which
    makes sense as there's already one on that line, on the
    Diecimila itself. You can also have a "bigger, better"
    LED attached. Just be sure to connect it through a suitable
    resistor to the ground line.
 You can move the resistors, have them driven by other lines.
    If you do, JUST change these DEFINEs to reflect your
    hardware choices */
#define tkbOutLEDPin1 12
#define tkbOutLEDPin2 11

#define tkbOutUseLED 1 /*Make 1 if you want LED output, 0 if you don't*/
#define tkbOutUseLCD 1 /*Make 1 if you want LCD output, 0 if you don't*/

/*End of first block for tkbOutput*/


/*Start of a block of things only needed for the demo...*/
#define Sw0 8 /* Configure the program to look at digital
  line 8 when asked to read "Sw0"... "SWitch zero"*/
#define Sw1 9
/*End of things for demo*/

void setup()
{
/*Start, second block of things for tkbOutput*/
  pinMode(tkbOutLEDPin0, OUTPUT);
  pinMode(tkbOutLEDPin1, OUTPUT);
  pinMode(tkbOutLEDPin2, OUTPUT);
  digitalWrite(tkbOutLEDPin0,LOW); //Establish initial state
      /*N.B.:  LOW turns LED OFF!*/
  digitalWrite(tkbOutLEDPin1,LOW); //Establish initial state
  digitalWrite(tkbOutLEDPin2,LOW); //Establish initial state
/*End, second block of things for tkbOutput*/
}

void loop()
{
  if (digitalRead(Sw0)==0) {tkbOut(0);}; //If switch pressed, send message "0"
  if (digitalRead(Sw1)==0) {tkbOut(1);}; //If switch pressed, send message "1"
  if ((digitalRead(Sw0)==0)and(digitalRead(Sw1)==0))
         {tkbOut(2);}; //If both switches pressed, send message "2"

/*N.B.: In this simple little demo, it will not be possible to send
the third message without sending at least one of the other messages.
You will not be able to press both switches down at exactly the same
moment. Along the way to both down, you will have just one down.
A similar problem occurs during the release of the switches.
But we're not always going to generate our messages with mere
button presses.*/
}

void tkbOut(byte msg)
{
if (msg==0) {digitalWrite(tkbOutLEDPin0,HIGH);};
if (msg==1) {digitalWrite(tkbOutLEDPin1,HIGH);};
if (msg==2) {digitalWrite(tkbOutLEDPin2,HIGH);};

}

In particular, don't worry about the new function, created by the stuff at the end starting "void tkbOut..." You can "just use it". We'll talk more about how later.

At this point, the LEDs should be switch- on- able as before, AND if you press both switches down, the third LED should light.

The third phase is....

/*plt1c2a_2
Second program for plt1c2a.htm */


/*From here down to "end of first block for "tkbOutput"
is code needed whenever using "tkbOutput". ("tkb being
the author's initials, and an attempt to keep THIS
set of output options separate from other "output" routines!

tkbOutput is some code to allow students to follow the
examples in the Arduino programming course at...
http//:sheepdogsoftware.co.uk/pltut.htm
... whether they have a LCD panel or not.*/

#define tkbOutLEDPin0 13 /* (No ; after #define) Configures program to
    run on hardware with an LED on digital line 13, which
    makes sense as there's already one on that line, on the
    Diecimila itself. You can also have a "bigger, better"
    LED attached. Just be sure to connect it through a suitable
    resistor to the ground line.
 You can move the resistors, have them driven by other lines.
    If you do, JUST change these DEFINEs to reflect your
    hardware choices */
#define tkbOutLEDPin1 12
#define tkbOutLEDPin2 11

#define tkbOutUseLED 1 /*Make 1 if you want LED output, 0 if you don't*/
#define tkbOutUseLCD 1 /*Make 1 if you want LCD output, 0 if you don't*/

/*End of first block for tkbOutput*/


/*Start of a block of things only needed for the demo...*/
#define Sw0 8 /* Configure the program to look at digital
  line 8 when asked to read "Sw0"... "SWitch zero"*/
#define Sw1 9
/*End of things for demo*/

void setup()
{
/*Start, second block of things for tkbOutput*/
  pinMode(tkbOutLEDPin0, OUTPUT);
  pinMode(tkbOutLEDPin1, OUTPUT);
  pinMode(tkbOutLEDPin2, OUTPUT);
  digitalWrite(tkbOutLEDPin0,LOW); //Establish initial state
      /*N.B.:  LOW turns LED OFF!*/
  digitalWrite(tkbOutLEDPin1,LOW); //Establish initial state
  digitalWrite(tkbOutLEDPin2,LOW); //Establish initial state

  Serial.begin(9600);
/*End, second block of things for tkbOutput*/
}

void loop()
{
  if (digitalRead(Sw0)==0) {tkbOut(0);}; //If switch pressed, send message "0"
  if (digitalRead(Sw1)==0) {tkbOut(1);}; //If switch pressed, send message "1"
  if ((digitalRead(Sw0)==0)and(digitalRead(Sw1)==0))
         {tkbOut(2);}; //If both switches pressed, send message "2"

/*N.B.: In this simple little demo, it will not be possible to send
the third message without sending at least one of the other messages.
You will not be able to press both switches down at exactly the same
moment. Along the way to both down, you will have just one down.
A similar problem occurs during the release of the switches.
But we're not always going to generate our messages with mere
button presses.*/
}

void tkbOut(byte msg)
{
if (msg==0) {
      digitalWrite(tkbOutLEDPin0,HIGH);
      Serial.print("Hello World?n");
      //With the #117 displays, the ?n will cause
      //the controller to start a new line.
      //If yours doesn't do this, replace the
      //?n with a space.
    };
if (msg==1) {
      digitalWrite(tkbOutLEDPin1,HIGH);
      Serial.print("Second msg?n");
    };
if (msg==2) {
      digitalWrite(tkbOutLEDPin2,HIGH);
      Serial.print("Third msg?n");
    };

delay(500); //This is needed while sending to the
  //LCD if there's a way for multiple, rapid
  //calls of tkbOutput to occur... as they
  //will in the basic version of the demo, even
  //if the user tries to press the button
  //"briefly". "Bounce" is at the heart of
  //the problem which will arise.
}

Now when you press a button, you should see some text on your LCD panel, if you have one. Don't worry about gibberish you may see on the LCD screen while your Arduino program is uploading.

The fourth and final phase is....

/*plt1c2a_4
fourth program for plt1c2a.htm */


/*From here down to "end of first block for "tkbOutput"
is code needed whenever using "tkbOutput". ("tkb being
the author's initials, and an attempt to keep THIS
set of output options separate from other "output" routines!

tkbOutput is some code to allow students to follow the
examples in the Arduino programming course at...
http//:sheepdogsoftware.co.uk/pltut.htm
... whether they have a LCD panel or not.*/

#define tkbOutLEDPin0 13 /* (No ; after #define) Configures program to
    run on hardware with an LED on digital line 13, which
    makes sense as there's already one on that line, on the
    Diecimila itself. You can also have a "bigger, better"
    LED attached. Just be sure to connect it through a suitable
    resistor to the ground line.
 You can move the resistors, have them driven by other lines.
    If you do, JUST change these DEFINEs to reflect your
    hardware choices */
#define tkbOutLEDPin1 12
#define tkbOutLEDPin2 11

#define tkbOutUseLED 1 /*Make 1 if you want LED output, 0 if you don't*/
#define tkbOutUseLCD 1 /*Make 1 if you want LCD output, 0 if you don't*/
#define tkbOutClearLEDs 1 /*Make 1 if you want message on LEDs to go away
     a moment after it appears.*/

/*End of first block for tkbOutput*/


/*Start of a block of things only needed for the demo...*/
#define Sw0 8 /* Configure the program to look at digital
  line 8 when asked to read "Sw0"... "SWitch zero"*/
#define Sw1 9
/*End of things for demo*/

void setup()
{
/*Start, second block of things for tkbOutput*/
  pinMode(tkbOutLEDPin0, OUTPUT);
  pinMode(tkbOutLEDPin1, OUTPUT);
  pinMode(tkbOutLEDPin2, OUTPUT);
  digitalWrite(tkbOutLEDPin0,LOW); //Establish initial state
      /*N.B.:  LOW turns LED OFF!*/
  digitalWrite(tkbOutLEDPin1,LOW); //Establish initial state
  digitalWrite(tkbOutLEDPin2,LOW); //Establish initial state

  Serial.begin(9600);
/*End, second block of things for tkbOutput*/
}

void loop()
{
  if (digitalRead(Sw0)==0) {tkbOut(0);}; //If switch pressed, send message "0"
  if (digitalRead(Sw1)==0) {tkbOut(1);}; //If switch pressed, send message "1"
  if ((digitalRead(Sw0)==0)and(digitalRead(Sw1)==0))
         {tkbOut(2);}; //If both switches pressed, send message "2"

/*N.B.: In this simple little demo, it will not be possible to send
the third message without sending at least one of the other messages.
You will not be able to press both switches down at exactly the same
moment. Along the way to both down, you will have just one down.
A similar problem occurs during the release of the switches.
But we're not always going to generate our messages with mere
button presses.*/
}

void tkbOut(byte msg)
{
if (msg==0) {
      if (tkbOutUseLED==1) {digitalWrite(tkbOutLEDPin0,HIGH);};
      if (tkbOutUseLCD==1) {Serial.print("Hello World?n");};
      //With the #117 displays, the ?n will cause
      //the controller to start a new line.
      //If yours doesn't do this, replace the
      //?n with a space.
    };
if (msg==1) {
      if (tkbOutUseLED==1) {digitalWrite(tkbOutLEDPin1,HIGH);};
      if (tkbOutUseLCD==1) {Serial.print("Second msg?n");};
    };
if (msg==2) {
      if (tkbOutUseLED==1) {digitalWrite(tkbOutLEDPin2,HIGH);};
      if (tkbOutUseLCD==1) {Serial.print("Third msg?n");};
    };

delay(500); /*This determines how long the "message"
  remains visible on the LEDs if tkbOutClearLEDs
  is set to 1. It is also needed while sending to the
  the LCD if there's a way for multiple, rapid
  calls of tkbOutput to occur... as they
  will in the basic version of the demo, even
  if the user tries to press the button
  "briefly". "Bounce" is at the heart of
  the problem which will arise. */

if (tkbOutClearLEDs==1)
  {
  digitalWrite(tkbOutLEDPin0,LOW); //Turn LED off again
  digitalWrite(tkbOutLEDPin1,LOW);
  digitalWrite(tkbOutLEDPin2,LOW);
  };
}

It adds a frill... when, near the start of the program, the line starting "#define tkbOutClearLEDs" ends with a 1, then the LEDs get turned off again shortly after being turned on.

The final phase also makes the program pay attention to the "#define tkbOutUseLED 1" (or 0) and "#define tkbOutUseLCD 1" (or 0) which have been in the program all along... but didn't previously do anything!

Note the line near the end of the program that says "delay(500)". If you make this longer, messages will remain on the LEDs for longer. But remember that your program will "hang up" in the tkbOut part until that delay is complete.

There is nothing to say that you can't change the messages that arise, nor the number of messages available. As this becomes available, instructions will be given in the tutorials concerned.

+++++++++DO CONCLUDING REMARKS++++++++++++
Please also note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

Go to the sites above, and use their search buttons if you want to search them.
To search this site....

Search this site or the web powered by FreeFind

Site search Web search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

You can also search this site without using forms.
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.


Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers, but yes, they do pay me if you use this link! As do the Google advertisers, about whom I know nothing, of course.)



Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have some of my pages' traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.
Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....