-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.html
976 lines (900 loc) · 42.2 KB
/
final.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
<!DOCTYPE html>
<html lang="en" style="font-size: 16px">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, maximum-scale=1.0" />
<title>Final Project</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Forum&family=Josefin+Sans:ital,wght@0,100;0,400;1,100;1,400&family=Raleway:ital,wght@0,600;1,600&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Raleway:ital,wght@0,300;0,400;0,600;0,700;1,300;1,400;1,600;1,700&family=Roboto:ital,wght@0,400;0,500;0,700;0,900;1,400;1,500;1,700;1,900&family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css">
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<script src="script/index.js"></script>
<script src="script/lightbox.js"></script>
<link href="style/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<nav>
<h4 class="top-title">HCDE 439</h4>
<div class="nav-list">
<a class="nav-items" href="index.html">Home</a>
<div class="dropdown" style="padding-top: 2px;">
<a href="#">Assignments</a>
<!-- Assignments -->
<!-- <div style="padding-top: 2px;">Assignments</div> -->
<div class="dropdown-content">
<a class="nav-items" href="assignment1.html">Assignment 1</a>
<a class="nav-items" href="assignment2.html">Assignment 2</a>
<a class="nav-items" href="assignment3.html">Assignment 3</a>
<a class="nav-items" href="assignment4.html">Assignment 4</a>
<a class="nav-items" href="assignment5.html">Assignment 5</a>
<a class="nav-items" href="assignment6.html">Assignment 6</a>
</div>
</div>
<a class="nav-items" href="final.html">Final Project</a>
</div>
</nav>
<div class="title dark1" style="padding-bottom:0;">
<div class="flex">
<div class="info-wrap">
<div>
<h2>Final Project</h2>
<p style="padding-top: 6px;">
My Final Project, a smart knob that controls PC volume and media playback. Build on a PCB fully designed and built by me.
</p>
</div>
<p>
All files used in this project can be assessed here.
</p>
<p><em><a href="https://github.com/7HE-W0R1D/HCDE-439/tree/master/static/Final"
target="_blank">https://github.com/7HE-W0R1D/HCDE-439/tree/master/static/Final</a></em></p>
</div>
<img src="static/Final/device01_16_9.jpg" width=450 height=400 class="image" alt="arduino picture" style="object-fit: cover;">
</div>
</div>
<div class="content bright1" id="proposal">
<div class="title">
<h3>Proposal</h3>
<div class="subtitle"></div>
</div>
<h4>
Project Concept/Motivation
</h4>
<p>
I would like to make a smart knob that displays and controls my PC's volume.
</p>
<h4>
Anticipated Bill of Materials
</h4>
<ul>
<li>
Arduino
</li>
<li>
LCD display
</li>
<li>
Buttons
</li>
<li>
Resistors
</li>
<li>
Rotary Encoder
</li>
<li>
Wires
</li>
<li>
Materials for a housing (Optional)
</li>
</ul>
<!-- <h4>
Timeline and Contingency Plans
</h4>
<ul>
<li>
By March 1, finish the code
</li>
<li>
By March 3, finish the housing
</li>
<li>
By March 5, finish the installment
</li>
<li>
By March 8, finish the documentation
</li>
<li>
If anything goes wrong, delay it by 2 days max
</li>
</ul> -->
</div>
<div class="content dark1" id="concept">
<div class="title">
<h3>Concept</h3>
<div class="subtitle"></div>
</div>
<p>
A smart knob is designed to allow PC users to quickly check and adjust their PC's volumn. A rotary encoder is used to detect the user's scrolling actions. It is also equipped with an OLED screen that displays information about the current volume level or multimedia actions. The knob is also designed with a multimedia button that can be used to play, pause, mute, or skip through music or videos.
</p>
<p>
The smart knob is a convenient and efficient way to adjust the volume of your PC without having to use your mouse or keyboard. It provides a tactile and intuitive interface that allows for quick and precise adjustments, making it ideal for both casual users and competitive gamers who need to fully focus on their screens. With its geeky design, the smart knob is a stylish addition to any desk setup.
</p>
<h4>
Actual Bill of Materials
</h4>
<table>
<caption>Bill of Materials</caption>
<thead>
<tr>
<th>Amount</th>
<th>MN (manufacturer number)</th>
<th>MPN (manufacturer part number)</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Arduino Pro Micro</td>
<td class="props">type Arduino Pro Micro (Clone); variant type-c</td>
</tr>
<tr>
<td>1</td>
<td>Ceramic Capacitor</td>
<td class="props">package 100 mil [THT, multilayer]; voltage 10V; capacitance 22pF</td>
</tr>
<tr>
<td>2</td>
<td>Ceramic Capacitor</td>
<td class="props">package 100 mil [THT, multilayer]; voltage 35V; capacitance 100nF</td>
</tr>
<tr>
<td>1</td>
<td>Panasonic EVQ-V5B00215B</td>
<td class="props">package THT;</td>
</tr>
<tr>
<td>1</td>
<td>Generic female header - 2 pins</td>
<td class="props">package THT; pins 2; pin spacing 0.1in (2.54mm); form ♀ (female); hole size 1.0mm,0.508mm; row single</td>
</tr>
<tr>
<td>1</td>
<td>OLED 128x64 I2C Monochrome Display</td>
<td class="props">From Amazon; I2C</td>
</tr>
<tr>
<td>2</td>
<td>5.1k Ohm Resistor</td>
<td class="props">package 1206 [SMD]; tolerance ±5%; resistance 5.1k Ohm; power 0.25</td>
</tr>
<tr>
<td>1</td>
<td>1k Ohm Resistor</td>
<td class="props">package 1206 [SMD]; tolerance ±5%; resistance 1k Ohm; power 0.25</td>
</tr>
<tr>
<td>2</td>
<td>10k Ohm Resistor</td>
<td class="props">package 1206 [SMD]; tolerance ±5%; resistance 10k Ohm; power 0.25W</td>
</tr>
<tr>
<td>2</td>
<td>10k Ohm Resistor</td>
<td class="props">package THT; tolerance ±5%; pin spacing 400 mil; resistance 10k Ohm; bands 5; power 0.25</td>
</tr>
<tr>
<td>1</td>
<td>Pushbutton</td>
<td class="props">switch status Released; package [THT]</td>
</tr>
</tbody>
</table>
<h4>
Why Arduino Pro Micro
</h4>
<p>
I chose to use the Arduino pro micro as my microcontroller. The Arduino Pro Micro is a compact and versatile microcontroller board that is ideal for a wide range of projects. Its small size, roughly the size of a standard USB flash drive, makes it easy to integrate into my smart knob project where space is limited. The board features a USB-C port, which allows for fast and reliable data transfer and also provides power to the board. So I don't have to worry about making about USB module in my design.
</p>
<p>
One of the key benefits of the Arduino Pro Micro is its ability to act as a USB HID (Human Interface Device). This means that it can be used to emulate a keyboard, mouse, or another input device, allowing it to control other devices or software applications. This feature makes the board particularly useful for me as I need it to act as a keyboard to send media commands like "volume up", "volume down", "mute", and so on.
</p>
<p>
In addition to its USB HID capabilities, the Arduino Pro Micro also has a wide range of inputs and outputs, including digital and analog pins, PWM outputs, and serial communication ports. This makes it a versatile and adaptable platform that allows me to attach any peripherals to it without worrying about running out of pins.
</p>
<p>
Overall, the Arduino Pro Micro is a powerful and flexible microcontroller board that is ideal for my smart knob project where space is limited and USB HID functionality is required.
</p>
</div>
<div class="content dark2" id="schematic">
<div class="title">
<h3>Schematic</h3>
<div class="subtitle">Treasure Maps</div>
</div>
<figure class="lightbox-preview">
<img src="static/Final/final_schem.png" class="media" onclick='openLightbox("lightbox0");'>
</figure>
<div class="lightbox-expand" onclick='closeLightbox("lightbox0");' id="lightbox0" style="display: none;">
<img src="static/Final/final_schem.png" class="media">
</div>
<figure class="lightbox-preview">
<img src="static/Final/final_bb.svg" class="media" onclick='openLightbox("lightbox1");'>
</figure>
<div class="lightbox-expand" onclick='closeLightbox("lightbox1");' id="lightbox1" style="display: none;">
<img src="static/Final/final_bb.svg" class="media">
</div>
<p>
My schematic is similar to my assignment 4, which also features the Arduino, an OLED screen, a button, and a rotary encoder. However, there are also a couple of differences. The biggest change is the MCU, which I changed from an Arduino UNO to an Arduino Pro Micro Type-C. Since it’s a different board, the wiring changed. The IIC bus is on pin 2 and pin 3 for the Arduino Pro Micro. They are connected, instead of the A4 and A5 on the UNO, to the OLED screen, along with two 5.1K Ohm pull-up resistors. Another change is that a Panasonic EVQ-V5B00215B hollow rotary encoder replaced the rotary encoder module included in the kit. According to its datasheet, I included 4 * 10k Ohm resistors and 2 * 0.01uF capacitors to allow it to properly function. There's also a 22pF capacitor between the VCC and the GND pin to ease out spikes in currents.
</p>
<h4>Header Pin as Reset Button</h4>
<p>
Sometimes the Arduino becomes unresponsive when uploading code. The official solution is to hold reset button then uploading. However, to keep the size of my PCB, I cannot fit a button and a resistor. I replaced them with a simple 2 pin header connector.
When I need the reset button, I can connect the header pins to a button on a breadboard.
</p>
<h4>Better Power Supply</h4>
<p>
The IIC bus communicates rapidly with the arduino, if there are noise, sometimes the Arduino behaves weirdly or freezes. To compensate for that,
I put a 22pF capacitor between the power rails of the arduino, the capacitors can ease the fluctuation of the power on the breadboard.
</p>
<h4>Better I2C Bus Performance</h4>
<p>
According to the web, two 4.7k ~ 10k Ohm resistors are required to pull-up the SCK(SCL) and SDA lines in the I2C bus. These resistors can let to better performance and more resistance to noises. I chose two 5.1k Ohm resistors that are available in the kit.
</p>
</div>
<div class="content bright1" id="Hardware">
<div class="title">
<h3>Hardware Implementation</h3>
<div class="subtitle">Above the blueprint</div>
</div>
<figure class="lightbox-preview">
<img src="static/Final/6073342911031915558_121.jpg" class="media" onclick='openLightbox("lightbox2");'>
</figure>
<div class="lightbox-expand" onclick='closeLightbox("lightbox2");' id="lightbox2" style="display: none;">
<img src="static/Final/6073342911031915558_121.jpg" class="media">
</div>
<figure class="lightbox-preview">
<img src="static/Final/6073342911031915559_121.jpg" class="media" onclick='openLightbox("lightbox2");'>
</figure>
<div class="lightbox-expand" onclick='closeLightbox("lightbox2");' id="lightbox2" style="display: none;">
<img src="static/Final/6073342911031915558_121.jpg" class="media">
</div>
<p>
The above image shows the actual Arduino circuit build on a etched PCB. There are instructions on the top. Protections for the PCB are installed in the bottom.
</p>
</div>
<div class="content bright2" id="pcb">
<div class="title">
<h3>Making the PCB</h3>
<div class="subtitle">Engineering Fun</div>
</div>
<p>
To make the project more fun, I decided to home made the PCB from scratch. Some more detailed tutorials:
</p>
<ul>
<li>
<a href="https://www.youtube.com/watch?v=7wAer7a3tU4" target="_blank">DIY PCBs At Home (Single Sided Presensitized)</a>
</li>
<li>
<a href="https://www.youtube.com/watch?v=1bJoTFhJ5CY" target="_blank">How to Make a PCB at Home - DIY Printed Circuit Board</a>
</li>
<li>
<a href="https://www.youtube.com/watch?v=q6iu8Fe8UOY" target="_blank">How to make a Printed Circuit Board (PCB) at home</a>
</li>
<li>
<a href="https://www.instructables.com/How-to-make-PCB-at-Home/" target="_blank">How to Make PCB at Home</a>
</li>
</ul>
<p>
But here's my steps and some tips (a cheaper, more home-approachable version)
</p>
<p>
Materials Used
</p>
<ul>
<li>Single sided copper plate</li>
<li>Ferric Chloride Copper Etchant Solution</li>
<li>Hydrogen Peroxide Topical Solution</li>
<li>Photosensitive film</li>
<li>Washing soda</li>
<li>UV light</li>
<li>Small hand drill</li>
<li>1000# sand paper</li>
<li>Materials for the circuit</li>
</ul>
<h4>General Steps</h4>
<ul>
<li>Design the PCB</li>
<li>Protect the routes on copper Plate</li>
<li>Remove the copper</li>
<li>Remove the protective film</li>
<li>Prepare for Soldering</li>
</ul>
<h5>Design the PCB</h5>
<p>
I use fritzing to design the PCB, it is a once free electronic designing app for hobbyists. I've been using it for a few years and it has good compatibility with Arduino parts. I iterated my PCB with several versions and the final one is like this
</p>
<figure class="lightbox-preview">
<img src="static/Final/final_etch_copper_bottom.svg" class="media" onclick='openLightbox("lightbox3");'>
<figcaption>Fig. V0.15 PCB copper bottom</figcaption>
</figure>
<div class="lightbox-expand" onclick='closeLightbox("lightbox3");' id="lightbox3" style="display: none;">
<img src="static/Final/final_etch_copper_bottom.svg" class="media">
</div>
<h6>Print the Design</h6>
<ul>
<li>Print it mirrored, so it can cover the copper surface and eventually give out the correct result</li>
<li>Use high-quality exports, get the highest DPI / resolution possible</li>
<li>Use high-quality printers and print papers, check it the traces are straight</li>
</ul>
<figure class="lightbox-preview">
<img src="static/Final/PXL_20230303_040233151.MP.jpg" class="media" onclick='openLightbox("lightbox4");'>
<figcaption>Fig. Printed circuits</figcaption>
</figure>
<div class="lightbox-expand" onclick='closeLightbox("lightbox4");' id="lightbox4" style="display: none;">
<img src="static/Final/PXL_20230303_040233151.MP.jpg" class="media">
</div>
<h5>Protect the Routes on the Copper Plate</h5>
<p>
There are two ways to do this: <em>toner transfer method and the photosensitive method</em>. Due to my limitations to simple equipment and school printers, I have to choose photosensitive method, which is more complex but has higher resolution.
</p>
<h6>Prepare the Copper Plate</h6>
<ul>
<li>Sand the copper surface to remove the oxidized layer</li>
<li>First peel off the plastic film on the photosensitive film. Only peel off one side</li>
<li>Carefully stick the peeled off side onto the copper surface</li>
<li>Gently heat the copper surface to make the film stick tightly to it, I use my stove top and heat level 2-4 to achieve that</li>
<li>Carefully peel off the other protective film</li>
</ul>
<h6>Transfer the Routes</h6>
<ul>
<li>Spray some oil onto the film</li>
<li>Carefully cover the printed circuit onto the film, make sure the oil fully covered the surface and the print</li>
<li>Shine UV light on the surface, test beforehand to determine the best time frame (30 min for my setup)</li>
<li>Remove the print and wash the board with soapy water</li>
</ul>
<figure class="lightbox-preview">
<img src="static/Final/PXL_20230307_055622521.jpg" class="media" onclick='openLightbox("lightbox5");'>
<figcaption>Fig. Copper boards with routes transferred</figcaption>
</figure>
<div class="lightbox-expand" onclick='closeLightbox("lightbox5");' id="lightbox5" style="display: none;">
<img src="static/Final/PXL_20230307_055622521.jpg" class="media">
</div>
<h5>
Remove the Copper
</h5>
<ul>
<li>Add water, some washing soda, and a little bit of hydrogen peroxide into a bowl, put the copper plate into it. Area outside the route should start fading quickly. Shake to facilitate process.</li>
<li>After all light areas are removed, carefully inspect the route, if any of them is broken, use a permanent marker pen to restore it. The marker has the same function as the protective film.</li>
<li>Put the plate into ferric chloride solution, shake carefully, the copper should start fading.</li>
<li>Shake until all the copper outside the routes are dissolved.</li>
</ul>
<h5>
Remove Protective Film
</h5>
<ul>
<li>Use pure hydrogen peroxide solution, put the plate in.</li>
<li>Darkened routes should dissolve in solution</li>
</ul>
<h5>
Prepare for Soldering
</h5>
<ul>
<li>Sand the routes well, remove the oxidized layer.</li>
<li>Put rosin around the soldering points, use iron to melt it. Tin should stick to the points.</li>
<li>Ready for parts now</li>
</ul>
<figure class="lightbox-preview">
<img src="static/Final/PXL_20230308_060257484.jpg" class="media" onclick='openLightbox("lightbox6");'>
<figcaption>Fig. Finished copper board</figcaption>
</figure>
<div class="lightbox-expand" onclick='closeLightbox("lightbox6");' id="lightbox6" style="display: none;">
<img src="static/Final/PXL_20230308_060257484.jpg" class="media">
</div>
<figure class="lightbox-preview">
<img src="static/Final/workstation.jpg" class="media" onclick='openLightbox("lightbox7");'>
<figcaption>Fig. Workstation in my tiny, tiny studio</figcaption>
</figure>
<div class="lightbox-expand" onclick='closeLightbox("lightbox7");' id="lightbox7" style="display: none;">
<img src="static/Final/workstation.jpg" class="media">
</div>
<h5>
Tips
</h5>
<ul>
<li>Carefully check the printed PCB, is the size all correct? Are traces correctly connected?</li>
<li>Always use rubber gloves, proceed in well ventilated areas</li>
<li>Dispose solution with care</li>
<li>Use high quality solder iron and tin, tip tinner is a great helper</li>
</ul>
</div>
<div class="content dark1" id="Hardware">
<div class="title">
<h3>Software Implementation</h3>
<div class="subtitle">Inside the Chips</div>
</div>
<p>
There are two parts of the code, the Arduino side and the PC side.
</p>
<h6>Good News</h6>
<p>The Arduino can work on its own🎊</p>
<h6>Bad News</h6>
<p>It will have limited functionality</p>
<h4>Arduino Side</h4>
<p>The Arduino side works in three parts:</p>
<ul>
<li>
Reads rotary encoder and button status
</li>
<li>
Sends media command to the PC based on status
</li>
<li>
Sync volume and mute status from the PC (if available)
</li>
</ul>
<p>
And here's the actual code
</p>
<h5>Arduino Code</h5>
<div class="code-wrapper">
<pre><code class="arduino">#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Encoder.h>
#include <HID-Project.h>
#include <HID-Settings.h>
#include <ArduinoJson.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define SW_PIN 8 // Encoder Switch pin to 8
// Half of the total level the knob can adjust (-50 to +50)
#define KNOB_LEVEL_HALF 50
#define ENC_RATIO 1
#define CHAR_W 6
#define CHAR_H 8
#define MAIN_TAB_W 92
#define SIDE_TAB_W 32
#define ROUND_CORNER_R 16
#define MEDIA_TIMEOUT 3000
// 'volume_off_black_48dp', 24x24px
const unsigned char epd_bitmap_volume_off_black_48dp [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x1c, 0x03, 0x80, 0x0e,
0x01, 0xc0, 0x07, 0x10, 0xe0, 0x03, 0x80, 0x70, 0x01, 0xc2, 0x30, 0x1f, 0xe3, 0x38, 0x1f, 0xf1,
0x18, 0x1f, 0xf8, 0x18, 0x1f, 0xfc, 0x18, 0x1f, 0xfe, 0x18, 0x1f, 0xf7, 0x18, 0x00, 0xf3, 0x80,
0x00, 0x71, 0xc0, 0x00, 0x30, 0xe0, 0x00, 0x01, 0xf0, 0x00, 0x03, 0xb8, 0x00, 0x00, 0x10, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'play_pause_flaticon', 24x24px
const unsigned char epd_bitmap_play_pause_flaticon [] PROGMEM = {
0x00, 0x00, 0x00, 0x30, 0x00, 0x08, 0x38, 0x00, 0x18, 0x3c, 0x00, 0x30, 0x3f, 0x00, 0x60, 0x3f,
0x80, 0xc0, 0x3f, 0xe0, 0x80, 0x3f, 0xc1, 0x80, 0x3f, 0x83, 0x00, 0x3f, 0x06, 0x00, 0x3c, 0x0c,
0x00, 0x38, 0x18, 0x00, 0x20, 0x18, 0x00, 0x00, 0x30, 0x00, 0x00, 0x67, 0x9c, 0x00, 0xc7, 0x9c,
0x01, 0x87, 0x9c, 0x01, 0x07, 0x9c, 0x03, 0x07, 0x9c, 0x06, 0x07, 0x9c, 0x0c, 0x07, 0x9c, 0x18,
0x07, 0x9c, 0x10, 0x03, 0x9c, 0x00, 0x00, 0x00
};
// 'round_skip_next_black_48dp', 24x24px
const unsigned char epd_bitmap_round_skip_next_black_48dp [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xc0, 0x03, 0x00, 0xc0, 0x03, 0xc0, 0xc0, 0x03, 0xe0, 0xc0, 0x03, 0xf0,
0xc0, 0x03, 0xfc, 0xc0, 0x03, 0xfc, 0xc0, 0x03, 0xf0, 0xc0, 0x03, 0xe0, 0xc0, 0x03, 0xc0, 0xc0,
0x03, 0x00, 0xc0, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'round_skip_previous_black_48dp', 24x24px
const unsigned char epd_bitmap_round_skip_previous_black_48dp [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0xc0, 0x03, 0x03, 0xc0, 0x03, 0x07, 0xc0, 0x03, 0x0f,
0xc0, 0x03, 0x3f, 0xc0, 0x03, 0x3f, 0xc0, 0x03, 0x0f, 0xc0, 0x03, 0x07, 0xc0, 0x03, 0x03, 0xc0,
0x03, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'volume_up_black_48dp', 24x24px
const unsigned char epd_bitmap_volume_up_black_48dp [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00,
0x01, 0xc0, 0x00, 0x70, 0xe0, 0x00, 0xf0, 0x70, 0x01, 0xf2, 0x30, 0x1f, 0xf3, 0x38, 0x1f, 0xf3,
0x18, 0x1f, 0xf3, 0x18, 0x1f, 0xf3, 0x18, 0x1f, 0xf3, 0x18, 0x1f, 0xf3, 0x38, 0x00, 0xf2, 0x30,
0x00, 0x70, 0x70, 0x00, 0x30, 0xe0, 0x00, 0x01, 0xc0, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 384)
const int epd_bitmap_allArray_LEN = 5;
const unsigned char* epd_bitmap_allArray[5] = {
epd_bitmap_volume_off_black_48dp,
epd_bitmap_play_pause_flaticon,
epd_bitmap_round_skip_next_black_48dp,
epd_bitmap_round_skip_previous_black_48dp,
epd_bitmap_volume_up_black_48dp
};
int oldPos = 0;
int currPos = -KNOB_LEVEL_HALF;
int prevVal;
int currVal;
int knobExceed = 0;
int currExceed = 0;
unsigned long firstDownTime;
bool buttonState;
bool buttonPrevState;
bool startButton;
int counter;
unsigned long showMediaTime;
int mediaResult;
bool isMute;
// Connect to pin 2 and pin 3, only two pins with interrupt ability to ensure max performance
// // For Arduino UNO
// Encoder myEnc(2, 3);
// For Arduino Pro mini
Encoder myEnc(1, 0);
// This function composes a JSON string that includes both the raw encoder value and the processed value and send it to P5.js
void updateSerial() {
Serial.print("[");
Serial.print(currVal);
Serial.print(",");
Serial.print(mediaResult);
Serial.println("]");
}
void updateENC(bool forceUpdate = false, bool noUpdate = false) {
// Read from the rotary encoder
currPos = posConv(myEnc.read());
// Curr value is from 0 to 100
currVal = 1 * (currPos + KNOB_LEVEL_HALF);
// Enter if converted position changed or Exceeding status changed
if (currPos != oldPos || currExceed != knobExceed || forceUpdate) {
oldPos = currPos;
// Set currExceed same as knobExceed
currExceed = knobExceed;
// drawMainScreen();
// display.display();
if (currVal < prevVal && !noUpdate) {
// Turn down volume
Consumer.write(MEDIA_VOLUME_DOWN);
updateSerial();
}
if (currVal > prevVal && !noUpdate) {
// Turn up volume
Consumer.write(MEDIA_VOLUME_UP);
updateSerial();
}
prevVal = currVal;
}
}
void setup() {
Serial.begin(115200);
Consumer.begin();
// Pin 8 as the switch detect pin is set to INPUT, and when it's low it is depressed
pinMode(SW_PIN, INPUT);
myEnc.write(-KNOB_LEVEL_HALF * ENC_RATIO - (int)(ENC_RATIO / 2));
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the screen
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
currVal = 1 * (currPos + KNOB_LEVEL_HALF);
updateENC(true);
showMediaTime = millis();
}
void loop() {
// If there are data from P5.js
if (Serial.available() > 0) {
// Read data
String rawData = Serial.readStringUntil('\n');
rawData.trim(); // remove any \r \n whitespace at the end of the String
// Serial.println(rawData.substring(0, rawData.indexOf(",")));
int pcVolume = (rawData.substring(0, rawData.indexOf(","))).toInt();
String rawMute = rawData.substring(rawData.indexOf(",") + 1);
if (rawMute != "-1") {
isMute = rawMute == "1" ? true : false;
// Write the value to the encoder by reverting it
// Serial.print("Ismute" + isMute);
myEnc.write(((pcVolume) - KNOB_LEVEL_HALF) * ENC_RATIO);
// Force update the screen with new value
updateENC(true, true);
}
}
else {
updateENC();
}
int result = updateButton();
if (millis() - showMediaTime < MEDIA_TIMEOUT) {
// Still show media screen
if (result >= 0) {
//Button Pressed
mediaResult = result;
// Serial.println("Pressed " + String(mediaResult));
updateMediaCommand(mediaResult);
drawMultiScreen(mediaResult);
updateSerial();
showMediaTime = millis();
}
else {
drawMultiScreen(mediaResult);
}
}
else {
// Not showing media screen now
if (result >= 0) {
//Button Pressed
mediaResult = result;
// Serial.println("Pressed " + String(mediaResult));
updateMediaCommand(mediaResult);
transitionMultiScreen(true);
updateSerial();
drawMultiScreen(mediaResult);
showMediaTime = millis();
}
else {
if (mediaResult >= 0) {
transitionMultiScreen(false);
mediaResult = -1;
}
drawMainScreen();
}
}
delay(1);
}
int posConv(long inputPos) {
// Serial.println(inputPos);
int result;
// Result is position divided by four since the knob clicks every four turns
result = (int)(round((inputPos) / (float)ENC_RATIO));
// Serial.println(result);
if (result > KNOB_LEVEL_HALF) {
// Knob value exceeds max value
result = KNOB_LEVEL_HALF;
// Set knob value to max to prevent further increase
myEnc.write(KNOB_LEVEL_HALF * ENC_RATIO + (int)(ENC_RATIO / 2));
knobExceed = 1;
}
else if (result < -KNOB_LEVEL_HALF) {
// Knob value exceeds min value
result = -KNOB_LEVEL_HALF;
// Set knob value to min to prevent further decrease
myEnc.write(-KNOB_LEVEL_HALF * ENC_RATIO - (int)(ENC_RATIO / 2));
knobExceed = -1;
}
else {
knobExceed = 0;
}
return result;
}
int drawPartialRoundRect(int _x, int _y, int _w, int _h, int _r, bool isInvert, float percent) {
int boundX = round(_x + percent * _w);
display.fillRoundRect(_x, _y, _w, _h, _r, isInvert ? BLACK : WHITE);
display.fillRect(boundX, _y, round((1 - percent) * _w), _h, isInvert ? WHITE : BLACK);
display.drawRoundRect(_x, _y, _w, _h, _r, isInvert ? BLACK : WHITE);
return boundX;
}
void drawVolumeRect(int _w, int _h) {
// How many numbers to display "0" is 0.5, "12" is 1.0
float halfCharNum = 1.0;
int textSize = 2;
int cursorX;
int boundX = drawPartialRoundRect(0, 0, _w, _h, ROUND_CORNER_R, false, (currVal / 100.0));
display.setTextSize(textSize);
if (currVal < 10) {
halfCharNum = 0.5;
}
else if (currVal < 100) {
halfCharNum = 1.0;
}
else {
halfCharNum = 1.5;
}
cursorX = round((_w / 2) - halfCharNum * CHAR_W * textSize);
display.setCursor(cursorX, round((_h / 2) - CHAR_H));
display.setTextColor(WHITE, BLACK);
// If text merges with the white part, turn it into black text on white background
if (cursorX < boundX - 1) {
display.setTextColor(BLACK, WHITE);
}
display.print(currVal);
display.setTextColor(WHITE);
}
void drawPlayTab(int status) {
int fullStatus = status;
if (status == 0 && isMute) {
fullStatus = 4;
}
int iconSize = 24;
display.fillRoundRect(96, 0, 32, SCREEN_HEIGHT, ROUND_CORNER_R, WHITE);
display.drawBitmap(round(96 + (32 - iconSize) / 2), round((SCREEN_HEIGHT - iconSize) / 2), epd_bitmap_allArray[fullStatus], iconSize, iconSize, BLACK);
}
void drawMainScreen() {
drawVolumeRect(SCREEN_WIDTH, SCREEN_HEIGHT);
display.display();
}
void drawMultiScreen(int status) {
drawVolumeRect(MAIN_TAB_W, SCREEN_HEIGHT);
drawPlayTab(status);
display.display();
}
void transitionMultiScreen(bool direction) {
// Clear buffer
display.clearDisplay();
// Play 20 frames of the animation
int iteration = 20;
int mainW;
int mainH = SCREEN_HEIGHT;
int mediaW;
int mediaH = SCREEN_HEIGHT;
// Draw a enlarging rounded rectangle here
for (int i = 0; i < iteration; i++) {
// Allow user to scroll while animation is playing
updateENC();
display.clearDisplay();
// Calculate W, H, and radius of the rectangle
if (direction) {
mainW = round(easeInOutSine(i, 0, 20, SCREEN_WIDTH, MAIN_TAB_W));
mediaW = round(easeInOutSine(i, 0, 20, 0, SIDE_TAB_W));
}
else {
mainW = round(easeInOutSine(i, 0, 20, MAIN_TAB_W, SCREEN_WIDTH));
mediaW = round(easeInOutSine(i, 0, 20, SIDE_TAB_W, 0));
}
// Draw the rectangle
drawVolumeRect(mainW, mainH);
display.fillRoundRect(SCREEN_WIDTH - mediaW, 0, mediaW, mediaH, ROUND_CORNER_R, WHITE);
delay(5);
display.display();
delay(10);
}
}
void updateMediaCommand(int mediaStatus) {
if (mediaStatus == 0) {
// Mute
Consumer.write(MEDIA_VOLUME_MUTE);
}
else if (mediaStatus == 1) {
Consumer.write(MEDIA_PLAY_PAUSE);
}
else if (mediaStatus == 2) {
Consumer.write(MEDIA_NEXT);
}
else if (mediaStatus == 3) {
Consumer.write(MEDIA_PREVIOUS);
}
}
//Reads and returns how many clicks are there for the button
int updateButton() {
if (millis() - firstDownTime > 500 && startButton) {
// Serial.println("Pressed " + String(counter));
int result = counter;
counter = 0;
startButton = false;
// Constrain the clicks from none to 3
return constrain(result, -1, 3);
}
buttonState = digitalRead(SW_PIN);
if (buttonState != buttonPrevState) {
delay(10);
if(digitalRead(SW_PIN) == buttonState) {
if(buttonState == LOW) {
// Shifted LOW
startButton = true;
firstDownTime = millis();
}
else {
// Shifted HIGH
if (startButton) {
counter++;
}
}
}
}
buttonPrevState = buttonState;
return -1;
}
// This function is similar to the built-in map() function, but with a eased curve at the start and the end of the range.
// Similar to the ease-in-out function in CSS
float easeInOutSine(int _x, int inputLow, int inputHigh, int lowerBound, int higherBound) {
int inputRange = inputHigh - inputLow - 1;
float convX = ((float)_x - (float)inputLow) / (float)inputRange;
// Serial.print(String(convX) + "; ");
int range = higherBound - lowerBound;
float result = (-(cos(PI * convX) - 1.0) / 2.0) * range + lowerBound;
// Serial.println(result);
return result;
}</code></pre>
</div>
<h4>Python Code</h4>
<p>
The Python code is simple, I utilized the PYCAW package to read the volume and mute status of a Windows machine.
Pyserial is also used to communicate with the arduino through serial port. The code reads volume and mute information from PC and also reads arduino's stored volume and mute status.
It then determines if arduino's volume and mute status need update, if it needs, the code sends the updated status through serial.
</p>
<h5>Working without Python</h5>
<p>
As mentioned before, the project can work without the Python script. But without the ability to sync volume with the PC, it cannot show the correct volume and control the PC's volume when Arduino reaches the limit.
For example, if the PC's volume is at 35, but Arduino shows 0, then Arduino cannot make the PC's volume lower since on the Arduino's side, the volume is already 0, the lowest one.
</p>
<h5>Wrapping by EXE</h5>
<p>
To make the code more convenient to use, I wrapped my code into a .exe file using <a href="https://pypi.org/project/auto-py-to-exe/" target="_blank">auto-py-to-exe</a>.
</p>
<div class="code-wrapper">
<pre><code class="python">import serial
import time
import json
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
DEFAULT_PORT = "COM8"
def port_prompt():
target_port = input("Please enter a port: ")
if target_port == '' or target_port == None:
return DEFAULT_PORT
return target_port
arduino_port = port_prompt()
print("Starting Serial at " + arduino_port + "...")
arduino = serial.Serial(port = arduino_port)
time.sleep(0.5)
print("Starting audio services...")
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
def read_arduino():
line = arduino.readline() # read a byte
if line:
string = line.decode() # convert the byte string to a unicode string
data = json.loads(string)
time.sleep(0.01)
return data[0], data[1]
return -1, -1
def send_arduino(pc_volume, is_mute):
send_str = ""
send_str += str(pc_volume) + "," + str(is_mute) + "\n"
print("Sending to serial: " + send_str)
arduino.write(send_str.encode("utf-8"))
def main():
arduino_vol = -1
arduino_mute = -1
while True:
time.sleep(0.001)
curr_vol = round(volume.GetMasterVolumeLevelScalar() * 100)
curr_mute = volume.GetMute()
# print("Curr_vol" + str(curr_vol))
while arduino.in_waiting:
send_arduino(curr_vol, -1)
arduino_vol, arduino_mute = read_arduino()
curr_mute = volume.GetMute()
if arduino_vol != curr_vol or arduino_mute != curr_mute:
send_arduino(curr_vol, 1 - curr_mute)
arduino_vol = curr_vol
arduino_mute = curr_mute
main()</code></pre>
</div>
</div>
<div class="content bright1" id="actual-peration">
<div class="title">
<h3>Actual Operation</h3>
<div class="subtitle">Coming alive</div>
</div>
<video controls>
<source src="static/Final/6073342910575676156.mp4" type="video/mp4">
Your browser does not support HTML video.
</video>
<p>
The above video shows how the device operates, turning the knob can change the volume of the music, single click of button can play / pause audio, double click to skip track, triple click to reverse track, and long press to mute / unmute.
</p>
</div>
<button onclick="scrollToTop()" id="topBtn" title="Go to top">
<span class="material-icons">
arrow_upward
</span>
</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
</body>
</html>