13.Arduino learning notebook--four-bit digital tube experiment

13.Arduino learning notebook--four-bit digital tube experiment

         
         Juvtmall ( a company supply PCB PrototypingPCBA service and sell kinds of components, modules and so on) ( Catduino has the same function of Arduino)


This experiment uses the arduino to drive a four-bit digital tube. It is necessary to drive the current-limiting resistance of the digital tube. There are two ways of current-limiting resistance, one is connecting to the d1-d4 anode. The benefit of this method is that the resistance demanded is less,  produces that different numbers will produce different brightness in each number. 1 is brightest and 8 is the darkest.The other method is connecting to the other eight pins, which shows uniform brightness, but needs more resistance.This experiment uses eight 220 Ω resistances 
The following figure is four-bit digital tube

The four-bit digital tube has 12 pins, and the decimal point is placed in front of them, and the lower left corner is 1, and the other pins are rotated counterclockwise.Upper left corner number 12 is the largest pin. 

The following chart shows the manual for the digital tube



Here is the hardware connection diagram


Arduino code
1. // set the cathode interface
2. int a = 1;
3. int b = 2;
4. int c = 3;
5. int d = 4;
6. int e = 5;
7. int f = 6;
8. int g = 7;
9. int p = 8;
10. // set the anode interface
11. int d4 = 9;
12. int d3 = 10;
13. int d2 = 11;
14. int d1 = 12;
15. // set the variable
16. int del = 100;
17. int buttoncount = 0;
18. int loopcount = 0;
19. 
20. void setup ()
21. {
22.   pinMode (d1, OUTPUT);
23.   pinMode (d2, OUTPUT);
24.   pinMode (d3, OUTPUT);
25.   pinMode (d4, OUTPUT);
26.   pinMode (a, OUTPUT);
27.   pinMode (b, OUTPUT);
28.   pinMode (c, OUTPUT);
29.   pinMode (d, OUTPUT);
30.   pinMode (e, OUTPUT);
31.   pinMode (f, OUTPUT);
32.   pinMode (g, OUTPUT);
33.   pinMode (p, OUTPUT);
34.   digitalWrite (a, HIGH);
35.   digitalWrite (b, HIGH);
36.   digitalWrite (c, HIGH);
37.   digitalWrite (d, HIGH);
38.   digitalWrite (e, HIGH);
39.   digitalWrite (f, HIGH);
40.   digitalWrite (g, HIGH);
41.   digitalWrite (p, HIGH);
42. }
43. 
44. void loop ()
45. {
46.   roulette (4); // rotation effect
47.   delay (100);
48.   zigzag (2); // Z font effect
49.   delay (100);
50.   circles (4); // circle effect
51.   delay (100);
52. }
53. 
54. void pickDigit (int x) // Define pickDigit (x), its function is to open the dx port
55. {
56.   digitalWrite (d1, LOW);
57.   digitalWrite (d2, LOW);
58.   digitalWrite (d3, LOW);
59.   digitalWrite (d4, LOW);
60. 
61.   switch (x)
62.   {
63.   case 1:
64.     digitalWrite (d1, HIGH);
65.     break
66.   case 2:
67.     digitalWrite (d2, HIGH);
68.     break
69.   case 3:
70.     digitalWrite (d3, HIGH);
71.     break
72.   default:
73.     digitalWrite (d4, HIGH);
74.     break
75.   }
76. }
77. 
78. void clearLEDs () // clear screen
79. {
80.   digitalWrite (a, HIGH);
81.   digitalWrite (b, HIGH);
82.   digitalWrite (c, HIGH);
83.   digitalWrite (d, HIGH);
84.   digitalWrite (e, HIGH);
85.   digitalWrite (f, HIGH);
86.   digitalWrite (g, HIGH);
87.   digitalWrite (p, HIGH);
88. }
89. 
90. void roulette (int x) // set the rotation effect
91. {
92.   loopcount = 0;
93.   while (loopcount <x)
94.   {
95.     digitalWrite (a, LOW);
96.     pickDigit (1);
97.     delay (del);
98.     pickDigit (2);
99.     delay (del);
100.     pickDigit (3);
101.     delay (del);
102.     pickDigit (4);
103.     delay (del);
104.     digitalWrite (a, HIGH);
105.     digitalWrite (b, LOW);
106.     delay (del);
107.     digitalWrite (b, HIGH);
108.     digitalWrite (c, LOW);
109.     delay (del);
110.     digitalWrite (c, HIGH);
111.     digitalWrite (d, LOW);
112.     delay (del);
113.     pickDigit (3);
114.     delay (del);
115.     pickDigit (2);
116.     delay (del);
117.     pickDigit (1);
118.     delay (del);
119.     digitalWrite (d, HIGH);
120.     digitalWrite (e, LOW);
121.     delay (del);
122.     digitalWrite (e, HIGH);
123.     digitalWrite (f, LOW);
124.     delay (del);
125.     clearLEDs ();
126.     loopcount ++;
127.   }
128. }
129. 
130. void zigzag (int x) // set the Z font effect
131. {
132.   loopcount = 0;
133.   while (loopcount <x)
134.   {
135.     digitalWrite (a, LOW);
136.     pickDigit (1);
137.     delay (del);
138.     pickDigit (2);
139.     delay (del);
140.     pickDigit (3);
141.     delay (del);
142.     pickDigit (4);
143.     delay (del);
144.     digitalWrite (a, HIGH);
145.     digitalWrite (b, LOW);
146.     delay (del);
147.     digitalWrite (b, HIGH);
148.     digitalWrite (g, LOW);
149.     delay (del);
150.     pickDigit (3);
151.     delay (del);
152.     pickDigit (2);
153.     delay (del);
154.     pickDigit (1);
155.     delay (del);
156.     digitalWrite (g, HIGH);
157.     digitalWrite (e, LOW);
158.     delay (del);
159.     digitalWrite (e, HIGH);
160.     digitalWrite (d, LOW);
161.     delay (del);
162.     pickDigit (2);
163.     delay (del);
164.     pickDigit (3);
165.     delay (del);
166.     pickDigit (4);
167.     delay (del);
168.     digitalWrite (d, HIGH);
169.     digitalWrite (c, LOW);
170.     delay (del);
171.     digitalWrite (c, HIGH);
172.     digitalWrite (g, LOW);
173.     delay (del);
174.     pickDigit (3);
175.     delay (del);
176.     pickDigit (2);
177.     delay (del);
178.     pickDigit (1);
179.     delay (del);
180.     digitalWrite (g, HIGH);
181.     digitalWrite (f, LOW);
182.     delay (del);
183.     clearLEDs ();
184.     loopcount ++;
185.   }
186. }
187. 
188. void circles (int x) // set the circle effect

189. {
190.    loopcount = 0;
191.    while (loopcount <x)
192.    {
193.      digitalWrite (a, LOW);
194.      digitalWrite (b, LOW);
195.      digitalWrite (f, LOW);
196.      digitalWrite (g, LOW);
197.      pickDigit (1);
198.      delay (250);
199.      digitalWrite (a, HIGH);
200.      digitalWrite (b, HIGH);
201.      digitalWrite (f, HIGH);
202.      digitalWrite (c, LOW);
203.      digitalWrite (d, LOW);
204.      digitalWrite (e, LOW);
205.      pickDigit (2);
206.      delay (250);
207.      digitalWrite (a, LOW);
208.      digitalWrite (b, LOW);
209.      digitalWrite (f, LOW);
210.      digitalWrite (c, HIGH);
211.      digitalWrite (d, HIGH);
212.      digitalWrite (e, HIGH);
213.      pickDigit (3);
214.      delay (250);
215.      digitalWrite (a, HIGH);
216.      digitalWrite (b, HIGH);
217.      digitalWrite (f, HIGH);
218.      digitalWrite (c, LOW);
219.      digitalWrite (d, LOW);
220.      digitalWrite (e, LOW);
221.      pickDigit (4);
222.      delay (250);
223.      clearLEDs ();
224.      loopcount ++;
225.    }
226. }

And then copy the following code to the control board, to see the result.
1. // set the cathode interface
2. int a = 1;
3. int b = 2;
4. int c = 3;
5. int d = 4;
6. int e = 5;
7. int f = 6;
8. int g = 7;
9. int p = 8;
10. // set the anode interface
11. int d4 = 9;
12. int d3 = 10;
13. int d2 = 11;
14. int d1 = 12;
15. // set the variable
16. long n = 0;
17. int x = 100;
18. int del = 55; // the value adjust the clock here
19. 
20. void setup ()
21. {
22.   pinMode (d1, OUTPUT);
23.   pinMode (d2, OUTPUT);
24.   pinMode (d3, OUTPUT);
25.   pinMode (d4, OUTPUT);
26.   pinMode (a, OUTPUT);
27.   pinMode (b, OUTPUT);
28.   pinMode (c, OUTPUT);
29.   pinMode (d, OUTPUT);
30.   pinMode (e, OUTPUT);
31.   pinMode (f, OUTPUT);
32.   pinMode (g, OUTPUT);
33.   pinMode (p, OUTPUT);
34. }
35. 
36. void loop ()
37. {
38.   clearLEDs ();
39.   pickDigit (1);
40.   pickNumber ((n / x / 1000)% 10);
41.   delayMicroseconds (del);
42. 
43.   clearLEDs ();
44.   pickDigit (2);
45.   pickNumber ((n / x / 100)% 10);
46.   delayMicroseconds (del);
47. 
48.   clearLEDs ();
49.   pickDigit (3);
50.   dispDec (3);
51.   pickNumber ((n / x / 10)% 10);
52.   delayMicroseconds (del);
53. 
54.   clearLEDs ();
55.   pickDigit (4);
56.   pickNumber (n / x% 10);
57.   delayMicroseconds (del);
58. 
59.   n ++
60. 
61.   if (digitalRead (13) == HIGH)
62.   {
63.     n = 0;
64.   }
65. }
66. 
67. void pickDigit (int x) // Define pickDigit (x), its function is to open the dx port
68. {
69.   digitalWrite (d1, LOW);
70.   digitalWrite (d2, LOW);
71.   digitalWrite (d3, LOW);
72.   digitalWrite (d4, LOW);
73. 
74.   switch (x)
75.   {
76.   case 1:
77.     digitalWrite (d1, HIGH);
78.     break
79.   case 2:
80.     digitalWrite (d2, HIGH);
81.     break
82.   case 3:
83.     digitalWrite (d3, HIGH);
84.     break
85.   default:
86.     digitalWrite (d4, HIGH);
87.     break
88.   }
89. }
90. 
91. void pickNumber (int x) // Define pickNumber (x), its function is to display the number x
92. {
93.   switch (x)
94.   {
95.   default:
96.     zero ();
97.     break
98.   case 1:
99.     one ();
100.     break
101.   case 2:
102.     two ();
103.     break
104.   case 3:
105.     three
106.     break
107.   case 4:
108.     four ();
109.     break
110.   case 5:
111.     five
112.     break
113.   case 6:
114.     six ();
115.     break
116.   case 7:
117.     seven ();
118.     break
119.   case 8:
120.     eight ();
121.     break
122.   case 9:
123.     nine ();
124.     break
125.   }
126. }
127. 
128. void dispDec (int x) // set the decimal point opened
129. {
130.   digitalWrite (p, LOW);
131. }
132. 
133. void clearLEDs () // clear screen
134. {
135.   digitalWrite (a, HIGH);
136.   digitalWrite (b, HIGH);
137.   digitalWrite (c, HIGH);
138.   digitalWrite (d, HIGH);
139.   digitalWrite (e, HIGH);
140.   digitalWrite (f, HIGH);
141.   digitalWrite (g, HIGH);
142.   digitalWrite (p, HIGH);
143. }
144. 
145. void zero () // when defining the number 0, the tube pins switch is cathode.
146. {
147.   digitalWrite (a, LOW);
148.   digitalWrite (b, LOW);
149.   digitalWrite (c, LOW);
150.   digitalWrite (d, LOW);
151.   digitalWrite (e, LOW);
152.   digitalWrite (f, LOW);
153.   digitalWrite (g, HIGH);
154. }
155. 
156. void one () // when defining the number1, the tube pins switch is cathode.
157. {
158.   digitalWrite (a, HIGH);
159.   digitalWrite (b, LOW);
160.   digitalWrite (c, LOW);
161.   digitalWrite (d, HIGH);
162.   digitalWrite (e, HIGH);
163.   digitalWrite (f, HIGH);
164.   digitalWrite (g, HIGH);
165. }
166. 
167. void two () //when defining the number 2, the tube pins switch is cathode.
168. {
169.   digitalWrite (a, LOW);
170.   digitalWrite (b, LOW);
171.   digitalWrite (c, HIGH);
172.   digitalWrite (d, LOW);
173.   digitalWrite (e, LOW);
174.   digitalWrite (f, HIGH);
175.   digitalWrite (g, LOW);
176. }
177. 
178. void three () // when defining the number 3, the tube pins switch is cathode.
179. {
180.   digitalWrite (a, LOW);
181.   digitalWrite (b, LOW);
182.   digitalWrite (c, LOW);
183.   digitalWrite (d, LOW);
184.   digitalWrite (e, HIGH);
185.   digitalWrite (f, HIGH);
186.   digitalWrite (g, LOW);
187. }
188. 
189. void four () // when defining the number 4, the tube pins switch is cathode.
190. {
191.   digitalWrite (a, HIGH);
192.   digitalWrite (b, LOW);
193.   digitalWrite (c, LOW);
194.   digitalWrite (d, HIGH);
195.   digitalWrite (e, HIGH);
196.   digitalWrite (f, LOW);
197.   digitalWrite (g, LOW);
198. }
199. 
200. void five () // when defining the number5, the tube pins switch is cathode.
201. {
202.   digitalWrite (a, LOW);
203.   digitalWrite (b, HIGH);
204.   digitalWrite (c, LOW);
205.   digitalWrite (d, LOW);
206.   digitalWrite (e, HIGH);
207.   digitalWrite (f, LOW);
208.   digitalWrite (g, LOW);
209. }
210. 
211. void six () // when defining the number 6, the tube pins switch is cathode.
212. {
213.   digitalWrite (a, LOW);
214.   digitalWrite (b, HIGH);
215.   digitalWrite (c, LOW);
216.   digitalWrite (d, LOW);
217.   digitalWrite (e, LOW);
218.   digitalWrite (f, LOW);
219.   digitalWrite (g, LOW);
220. }
221. 
222. void seven () // when defining the number 7, the tube pins switch is cathode.
223. {
224.   digitalWrite (a, LOW);
225.   digitalWrite (b, LOW);
226.   digitalWrite (c, LOW);
227.   digitalWrite (d, HIGH);
228.   digitalWrite (e, HIGH);
229.   digitalWrite (f, HIGH);
230.   digitalWrite (g, HIGH);
231. }
232. 
233. void eight () // when defining the number8, the tube pins switch is cathode.
234. {
235.   digitalWrite (a, LOW);
236.   digitalWrite (b, LOW);
237.   digitalWrite (c, LOW);
238.   digitalWrite (d, LOW);
239.   digitalWrite (e, LOW);
240.   digitalWrite (f, LOW);
241.   digitalWrite (g, LOW);
242. }
243. 
244. void nine () // when defining the number 9, the tube pins switch is cathode.
245. {
246.   digitalWrite (a, LOW);
247.   digitalWrite (b, LOW);
248.   digitalWrite (c, LOW);
249.   digitalWrite (d, LOW);
250.   digitalWrite (e, HIGH);
251.   digitalWrite (f, LOW);
252.   digitalWrite (g, LOW);
253. }

The effect of the following code is the same as the above, but the below uses the array method to shorthand. Lets have a look at the difference between them.
ARDUINO code

1. // set the cathode interface
2. int a = 1;
3. int b = 2;
4. int c = 3;
5. int d = 4;
6. int e = 5;
7. int f = 6;
8. int g = 7;
9. int p = 8;
10. // set the anode interface
11. int d4 = 9;
12. int d3 = 10;
13. int d2 = 11;
14. int d1 = 12;
15. // set the variable
16. long n = 0;
17. int x = 100;
18. int del = 55; // the value adjust the clock here
19. 
20. byte segs [7 = {a, b, c, d, e, f, g};
21. 
22. byte seven_seg_digits [10 [7 = {{0,0,0,0,0,1}, // = 0
23.                                  {1,0,0,1,1,1,1}, // = 1
24.                                  {0,0,1,0,0,1,0}, // = 2
25.                                  {0,0,0,0,1,1,0}, // = 3
26.                                  {1,0,0,1,1,0,0}, // = 4
27.                                  {0,1,0,0,1,0,0}, // = 5
28.                                  {0,1,0,0,0,0,0}, // = 6
29.                                  {0,0,0,1,1,1,1}, // = 7
30.                                  {0,0,0,0,0,0}, // = 8
31.                                  {0,0,0,0,1,0,0} // = 9
32.                              };
33. 
34. void setup ()
35. {
36.   pinMode (d1, OUTPUT);
37.   pinMode (d2, OUTPUT);
38.   pinMode (d3, OUTPUT);
39.   pinMode (d4, OUTPUT);
40.   pinMode (a, OUTPUT);
41.   pinMode (b, OUTPUT);
42.   pinMode (c, OUTPUT);
43.   pinMode (d, OUTPUT);
44.   pinMode (e, OUTPUT);
45.   pinMode (f, OUTPUT);
46.   pinMode (g, OUTPUT);
47.   pinMode (p, OUTPUT);
48. }
49. 
50. void loop ()
51. {
52.   clearLEDs ();
53.   pickDigit (1);
54.   lightSegments ((n / x / 1000)% 10);
55.   delayMicroseconds (del);
56. 
57.   clearLEDs ();
58.   pickDigit (2);
59.   lightSegments ((n / x / 100)% 10);
60.   delayMicroseconds (del);
61. 
62.   clearLEDs ();
63.   pickDigit (3);
64.   dispDec (3);
65.   lightSegments ((n / x / 10)% 10);
66.   delayMicroseconds (del);
67. 
68.   clearLEDs ();
69.   pickDigit (4);
70.   lightSegments (n / x% 10);
71.   delayMicroseconds (del);
72. 
73.   n ++
74. 
75.   if (digitalRead (13) == HIGH)
76.   {
77.     n = 0;
78.   }
79. }
80. 
81. void pickDigit (int x) // Define pickDigit (x), its function is to open the dx port
82. {
83.   digitalWrite (d1, LOW);
84.   digitalWrite (d2, LOW);
85.   digitalWrite (d3, LOW);
86.   digitalWrite (d4, LOW);
87. 
88.   switch (x)
89.   {
90.   case 1:
91.     digitalWrite (d1, HIGH);
92.     break
93.   case 2:
94.     digitalWrite (d2, HIGH);
95.     break
96.   case 3:
97.     digitalWrite (d3, HIGH);
98.     break
99.   default:
100.     digitalWrite (d4, HIGH);
101.     break
102.   }
103. }
104. 
105. void dispDec (int x) // set the decimal point opened
106. {
107.   digitalWrite (p, LOW);
108. }
109. 
110. void clearLEDs () // clear screen
111. {
112.   digitalWrite (a, HIGH);
113.   digitalWrite (b, HIGH);
114.   digitalWrite (c, HIGH);
115.   digitalWrite (d, HIGH);
116.   digitalWrite (e, HIGH);
117.   digitalWrite (f, HIGH);
118.   digitalWrite (g, HIGH);
119.   digitalWrite (p, HIGH);
120. }
121. 
122. 
123. // light the digital tube of the corresponding number
124. void lightSegments (int x) {
125.   for (int i = 0; i <7; i ++) {
126.     digitalWrite (segs [i, seven_seg_digits [x [i);
127.   }
128. }


The other blog

评论

此博客中的热门博文

9.Arduino learning notebook--Photocontrolled LED experiment

19.Arduino learning notebook--Atmega8 makes the minimum arduino system

17.Arduino learning notebook - stepper motors experiment