Rev Author Line No. Line
4218 kakl 1 // MLAB LABduino LCD module example
2 // www.mlab.cz
3 //---------------------------------------------------------------------
4  
5 #include <LiquidCrystal.h>
6  
7 #define S1 9 // PB1
8 #define S2 10 // PB2
9 #define S3 6 // PD6
10 #define S4 7 // PD7
11  
12 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
13  
14 // Create a set of new characters
15 byte smiley[8] = {
16 0b00000,
17 0b00000,
18 0b01010,
19 0b00000,
20 0b00000,
21 0b10001,
22 0b01110,
23 0b00000
24 };
25  
26 byte frownie[8] = {
27 0b00000,
28 0b00000,
29 0b01010,
30 0b00000,
31 0b00000,
32 0b00000,
33 0b01110,
34 0b10001
35 };
36  
37  
38  
39 void play()
40 {
41 // notes in the melody:
42 int melody[] = {
43 262, 196, 196, 220, 196,0, 247, 262};
44  
45 // note durations: 4 = quarter note, 8 = eighth note, etc.:
46 int noteDurations[] = {
47 4, 8, 8, 4,4,4,4,4 };
48  
49 // iterate over the notes of the melody:
50 for (int thisNote = 0; thisNote < 8; thisNote++)
51 {
52 // to calculate the note duration, take one second
53 // divided by the note type.
54 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
55 int noteDuration = 1000/noteDurations[thisNote];
56 tone(8, melody[thisNote],noteDuration);
57  
58 // to distinguish the notes, set a minimum time between them.
59 // the note's duration + 30% seems to work well:
60 int pauseBetweenNotes = noteDuration * 1.30;
61 delay(pauseBetweenNotes);
62 // stop the tone playing:
63 noTone(8);
64 }
65 }
66  
67 int button;
68  
69 void setup()
70 {
71 lcd.begin(16,2); // initialize the lcd
72  
73 lcd.createChar (0, smiley); // load character to the LCD
74 lcd.createChar (1, frownie); // load character to the LCD
75  
76 lcd.home (); // go home
77 lcd.print("www.mlab.cz www.mlab.cz www.mlab.cz");
78 lcd.setCursor ( 0, 1 );
79 lcd.print("wiki.mlab.cz/doku.php?id=cs:labduino");
80  
81  
82 pinMode(S1, INPUT); // initialize buttons
83 pinMode(S2, INPUT);
84 pinMode(S3, INPUT);
85 pinMode(S4, INPUT);
86  
87 button = 6;
88 lcd.setCursor ( 0, 1 );
89 }
90  
91 unsigned int n=0;
92  
93 void loop()
94 {
95  
96 if (!digitalRead(S1)) // read buttons
97 {
98 button=1;
99 }
100 if (!digitalRead(S2))
101 {
102 button=2;
103 }
104 if (!digitalRead(S3))
105 {
106 button=3;
107 }
108 if (!digitalRead(S4))
109 {
110 button=4;
111 }
112  
113 if ((button<=4)&&(button>=1))
114 {
115 lcd.clear(); // clear display
116 lcd.home (); // go home
117 lcd.print("www.mlab.cz");
118 lcd.setCursor ( 0, 1 );
119 lcd.print("Cvak...S");
120 }
121  
122 switch (button) // do some specific action
123 {
124 case 1:
125 lcd.print("1");
126 button=0;
127 break;
128 case 2:
129 lcd.print("2");
130 button=0;
131 break;
132 case 3:
133 lcd.print("3");
134 button=0;
135 break;
136 case 4:
137 lcd.print("4");
138 button=0;
139 break;
140 }
141  
142 if (0==button) // do rest of action
143 {
144 lcd.setCursor ( 10, 1 );
145 lcd.print ( char(0));
146 play();
147 lcd.setCursor ( 0, 1 );
148 lcd.print("Hmmm... ");
149 lcd.print ( char(1));
150 button=5;
151 lcd.setCursor ( 0, 1 );
152 }
153  
154  
155 if ((n++ > 25000) && (button==6)) // time delay
156 {
157 lcd.scrollDisplayLeft();
158 n=0;
159 }
160 }