IAN LANG ELECTRONICS

Here's the code for receiving the output from the PC interface in toto:

 

 

#include <LiquidCrystal.h>

  boolean resetdigit=true;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 

void setup(){

    // set up the LCD's number of columns and rows:

  lcd.begin(16, 1);

  // initialize the serial communications:

  Serial.begin(9600);

  reset();

 

}

 

void loop(){

if (digitalRead(7)==1){reset();}

  if (Serial.available()) {

    tone(6,242);delay(1000);noTone(6);

    lcd.clear();

    delay(100);

     while (Serial.available() > 0) {

     lcd.write(Serial.read());

    }

   resetdigit=false;delay(2000);}

if (resetdigit==false){

   delay (500);

lcd.scrollDisplayLeft();}

 

}

 

void reset(){

  resetdigit=true;

  lcd.clear();

  lcd.print(" Ian Lang Etncs ");}

 

 

 

What should happen is that the LCD should show on the top line the text  " Ian Lang Etncs " and if you don't want to pander to my vanity change the last line in the code to show whatever you like; bear in mind it shouldn't be longer than sixteen characters though. Now, start the interface in Visual Basic going, and type in the following (or copy and paste) to the textbox:

 

This message board is now capable of carrying eighty characters. Splendid job!

 

Hit the send button and if all goes well, you should hear the loudspeaker emit a tone. The beginning of the message will appear on the screen, and then it will scroll across. Let it scroll a couple of times, and then press the button on the breadboard firmly.  The message should now disappear, and the initial display on the screen will reappear instead.

 

It's easy to get this wrong, so here's some common problems:

 

If when you hit the send button, the speaker does not emit a tone-

 

1. Check the wiring. It should be a wire from pin 6 goes to a resistor of 100 ohms. The resistor goes to one wire attached to a terminal of the speaker. The other wire from the speaker goes to GND.

 

2.Check the portname settings on your serial port in VB. Does it match your settings on the Arduino? In the Arduino IDE click on tools and then serial port. You'll see in there which your Arduino is on. Mine tells me that com3 and com8 are active. I know that com3 is my USB mouse, and so com8 is my Arduino UNO. It's ticked too, and I've succesfully uploaded the sketch so that confirms it.

 

If there was a tone but no display:

 

1. Turn the potentiometer to alter the contrast. Turn it first one way and then the other.

 

2. Check your wiring VERY thouroughly. Nine times out of ten I put a wire in wrong too. Another advantage of building an LCD shield.

 

3. Check the Baud rate on your serial port.

 

Assuming all went well, that sentence we sent has seventy-eight characters. Try this one:

 

This message board is now capable of carrying eighty characters. Splendid job! Huzzah!

 

Doesn't go too well does it? You probably saw a blobby character at the beginning. It's a carriage return. The LCD display can only handle eighty characters at one go. Moreover it splits these over however many lines the LCD has. So only forty per line for a two-liner then. But if we lie and say to the Arduino that we have only a one-line display, it believes us and treats the display as a one-liner, and the display believes the Arduino and acts accordingly, allowing us a continual string of eighty characters.

 

You're probably thinking  "well, wouldn't it have been better to use a one-liner in the first place?" and you're right. Except I hadn't got one. So two-liner and an outrageous lie to the Arduino it is then.

 

Clear the text box on the interface, and type in any message you like. Hit the send button and the LCD will display it. It always, in scrolling, goes for a length of eighty characters whether or not there are eighty characters in the message, and so the shorter the message the longer the time it'll take to cycle back round. If you find a way round this, tell me, because I haven't managed to. Here's a couple more test pieces I tried it with; you'll notice a distinct flavour of HEX and the Bursar running through these:

 

Out of cheese error+++++Redo from Start+++++Mr Jelly!+++++++++

 

Whoops! Up goes the custard! Volume!

 

Millenium hand and shrimp!

 

And before anybody says that last one is Foul Ole Ron and not the Bursar, the Bursar did say it at the end of Lords and Ladies. So there. Let's test symbols:

 

!"£$%^&*()_+:;@'~#?><,.€

 

The only symbol in there it doesn't recognise is that last one, which is the sybol for the unit of currency the Euro. HAHAHAHA! How very ironic! I must e-mail Frau Merkl and tell her that! Let's see if she explodes!

 

So, let's have a canter through the code then, and see what's happening.

 

At the very beginning of the sketch we call the LCD library:

 

#include <LiquidCrystal.h>

 

Then we declare  a global variable:

 

 

  boolean resetdigit=true;

 

This is a checkdigit and is going to be used to determine whether or not a reset is active. Next thing is to state where our LCD is wired into, (RS, Enable and the four data busses):

 

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 

Now we get to the setup and tell the egregious lie as detailed above:

 

void setup(){

    // set up the LCD's number of columns and rows:

  lcd.begin(16, 1);

 

Then begin the serial communications

 

  // initialize the serial communications:

  Serial.begin(9600);

 

we then call the function reset:

 

  reset();

 

It looks like this:

 

void reset(){

  resetdigit=true;

  lcd.clear();

  lcd.print(" Ian Lang Etncs ");}

 

Obviously we clear the LCD and output to it Ian Lang Etncs or whatever you changed it to, but we also set resetdigit to true. This is not important in the setup but it is in the loop. Here's why:

 

void loop(){

if (digitalRead(7)==1){reset();}

 

The only way digitalRead(7) can be high is if we press the button. This is the part that dismisses the message and returns to the initial state. It does so by calling reset. As you know, resetdigit gets set to true when that happens. The next thing we do is look for incoming serial data:

 

  if (Serial.available()) {

 

if there is some, we do the alert tone, clear the LCD and wait for it all to come through:

 

    tone(6,242);delay(1000);noTone(6);

    lcd.clear();

    delay(100);

 

then we read it until the end,  and write each character to the LCD:

 

     while (Serial.available() > 0) {

     lcd.write(Serial.read());

    }

 

Once it's all down, we set resetdigit to false and wait two seconds:

 

   resetdigit=false;delay(2000);}

 

Now we are back out of that conditional, we continue the loop. If there's a message, we want it to scroll. If not, we want the text on the LCD to stay still. So:

 

if (resetdigit==false){

   delay (500);

lcd.scrollDisplayLeft();}

 

}

 

and that's all there is to it. This is a nice and simple way to get a practical message system going, and gives a sound basis for moving on to wireless communication systems. If you used a bigger LCD Module, it could also stand alone as an office sign. It could be used in window displays in shops, and varying programmable messages is going to be the subject of following chapters.

 

 

Ian Lang February 2012

A Simple PC to Arduino Message Board

Go Back Previous page