## Monday, March 17, 2014

### Arduino–Day 3: Adding an LCD (without Potentiometer) to display the message

Before I take on the project of taking the user input, of course, I had to learn few things first.  I started off with tackling adding an LCD.

I have a bad habit of not following tutorials on the web typically because I usually have my own twist to the problem that makes it complicated.  However, I found one that helped immensely.

Here’s how the pins were mapped:

 LCD Pin Connected to 1 (VSS) Ground (GRD)(Note the bridge.  The actual connection’s behind the LCD panel) 2 (VDD) +5v Arduino Pin 3 (Contrast) Resistor to GRD(Note Arduino GRD is connected by a small green wire.  It’s also connected to the other side of the breadboard by the GRD bridge) 4 (RS) Arduino Pin 2 5 (RW) Arduino Pin 3 6 (Enable) Arduino Pin 4 7, 8, 9, 10 No Connection 11 (D4) Arduino Pin 6(5 was skipped as I my leads in the wire didn’t have enough space) 12 (D5) Arduino Pin 7 13 (D6) Arduino Pin 8 14 (D7) Arduino Pin 9 15 Backlight Power (+) Arduino Pin 10 16 Backlight GRD Ground (GRD)

11 is my control pin for the Morse Messenger.

Code Snippet
1. #include <LiquidCrystal.h>
2.
3. // A dot is a '1',  A dash '0' -1 is a filler / compensator for the absence of jagged arrays
4. int letterMorse[][4] = {
5.   {1, 0, -1, -1},
6.   {0, 1, 1, 1},
7.   {0, 1, 0, 1},
8.   {0, 1, 1, -1},
9.   {1, -1, -1, -1},
10.   {1, 1, 0, 1},
11.   {0, 0, 1, -1},
12.   {1, 1, 1, 1},
13.   {1, 1, -1, -1},
14.   {1, 0, 0, 0},
15.   {0, 1, 0, -1},
16.   {1, 0, 1, 1},
17.   {0, 0, -1, -1},
18.   {0, 1, -1, -1},
19.   {0, 0, 0, -1},
20.   {1, 0, 0, 1},
21.   {0, 0, 1, 0},
22.   {1, 0, 1, -1},
23.   {1, 1, 1, -1},
24.   {0, -1, -1, -1},
25.   {1, 1, 0, -1},
26.   {1, 1, 1, 0},
27.   {1, 0, 0, -1},
28.   {0, 1, 1, 0},
29.   {0, 1, 0, 0},
30.   {0, 0, 1, 1}
31. };
32.
33.
34. int numberMorse[][5] = {
35.   { 0, 0, 0, 0, 0 },
36.   { 1, 0, 0, 0, 0 },
37.   { 1, 1, 0, 0, 0 },
38.   { 1, 1, 1, 0, 0 },
39.   { 1, 1, 1, 1, 0 },
40.   { 1, 1, 1, 1, 1 },
41.   { 0, 1, 1, 1, 1 },
42.   { 0, 0, 1, 1, 1 },
43.   { 0, 0, 0, 1, 1 },
44.   { 0, 0, 0, 0, 1 }
45. };
46.
47. //// A dot is a '1',  A dash '0'
48. //int symbolMorse[][6] = {
49. //    {0, -1, 0, -1, 0, -1},
50. //    {0, }
51. //}
52.
53. int controlPin = 11;
54. const int standardPauseInMilliseconds = 500;
55. LiquidCrystal lcd(2, 3, 4, 6, 7, 8, 9);
56. int backlight = 10;
57.
58. void setup() {
59.     pinMode(backlight, OUTPUT);
60.     digitalWrite(backlight, HIGH);
61.
62.   lcd.begin(16, 2);
63.   lcd.clear();
64.   lcd.setCursor(0, 0);
65.   lcd.display();
66.   // put your setup code here, to run once:
67.   doMorse("I LOVE ARDUINO");
68. }
69.
70. void loop() {
71. }
72.
73. void doMorse(String message)
74. {
75.   // put your main code here, to run repeatedly:
76.   for (int i = 0; i < message.length(); i++)
77.   {
78.     char letter = message.charAt(i);
79.
80.     lcd.print(letter);
81.
82.     Serial.println(letter);
83.
84.     if (letter == ' ')
85.     {
86.       //Words are separated by a space equal to 7 dots
87.       delay(standardPauseInMilliseconds * 7);
88.       continue;
89.     }
90.
91.     int *component = lookupComponents(letter);
92.
93.     for (int j = 0; j < 4; j++)
94.     {
95.
96.       int number = component[j];
97.       if (number < 0) continue;
98.
99.       bool isDot = (number);
100.
101.       Serial.print(isDot ? "Dot- " : "Dash- ");
102.
103.       //For the duration of a dash (0) is 3 times the duration of a dot (1)
104.       blink(isDot ? standardPauseInMilliseconds : standardPauseInMilliseconds * 3);
105.       beep(isDot ? standardPauseInMilliseconds : standardPauseInMilliseconds * 3);
106.
107.       //Each dot / dash is followed by a short silence, equal to the dot duration
108.       delay(standardPauseInMilliseconds);
109.     }
110.
111.     Serial.println(' ');
112.
113.     //The space between letters is three units
114.     delay(standardPauseInMilliseconds * 3);
115.
116.   }
117. }
118.
120. {
121.   pinMode(controlPin, OUTPUT);
122.   //Turn on the light
123.   digitalWrite(controlPin, HIGH);
124.   //For the duration of a dash (0) is 3 times the duration of a dot (1)
125.   delay(delayInMilliseconds);
126.   digitalWrite(controlPin, LOW);
127.
128. }
129.
130. void beep(int delayInMilliseconds)
131. {
132.   pinMode(controlPin, OUTPUT);
133.   analogWrite(controlPin, 20);
134.   delay(delayInMilliseconds);
135.   analogWrite(controlPin, 0);
136. }
137.
138.
139. int * lookupComponents(char letter)
140. {
141.     int value = ((int)letter) - 65;
142.     bool isNumber = value < 0;
143.
144.     Serial.println(value);
145.     //lcd.print(value);
146.
147.     return isNumber ? numberMorse[value] : letterMorse[value];
148.
149. }

The issues I had faced were actually numerous.  Didn’t get the backlight to light up for a long time. I found pins 12 & 13 wouldn’t work as control pins.   Why? They simply ignored analogWrite().  The piezo wouldn’t beep.

The biggest issue I faced was to ensure my programming is matching with the wiring.  This was especially complicated by the pin 12 & 13 refusing to work as control pins.

I still don’t understand many things (such as if the resistors were meant to use up energy so component upstream would receive less electricity why would it need to be connected to GRD & not power?  I found the 7 segment display brighten up considerably when I attached the ground directly than through the resistor).