1+ // Light a cathode/anode combo full time, no multiplexing
2+
3+ const byte mainAdjUp = A3; // press to change cathode
4+ const byte mainAdjDn = A2; // press to change anode
5+
6+ // This clock is 2x3 multiplexed: two tubes powered at a time.
7+ // The anode channel determines which two tubes are powered,
8+ // and the two SN74141 cathode driver chips determine which digits are lit.
9+ // 4 pins out to each SN74141, representing a binary number with values [1,2,4,8]
10+ byte binOutA[4 ] = {2 ,3 ,4 ,5 };
11+ byte binOutB[4 ] = {6 ,7 ,8 ,9 };
12+ // 3 pins out to anode channel switches
13+ byte anodes[3 ] = {11 ,12 ,13 };
14+
15+ byte btnCur = 0 ; // Momentary button currently in use - only one allowed at a time
16+ byte curCathode = 0 ;
17+ byte curAnode = 0 ;
18+
19+ void setup (){
20+ Serial.begin (9600 );
21+ initOutputs ();
22+ initInputs ();
23+ updateDisplay ();
24+ }
25+
26+ void loop (){
27+ checkInputs ();
28+ delay (10 ); // in case of switch bounce?
29+ }
30+
31+ void initInputs (){
32+ pinMode (mainAdjUp, INPUT_PULLUP);
33+ pinMode (mainAdjDn, INPUT_PULLUP);
34+ }
35+ void checkInputs (){
36+ checkBtn (mainAdjUp);
37+ checkBtn (mainAdjDn);
38+ }
39+ bool readInput (byte pin){
40+ if (pin==A6 || pin==A7) return analogRead (pin)<100 ?0 :1 ; // analog-only pins
41+ else return digitalRead (pin);
42+ }
43+ void checkBtn (byte btn){
44+ // Changes in momentary buttons, LOW = pressed.
45+ // When a button event has occurred, will call ctrlEvt
46+ bool bnow = readInput (btn);
47+ // If the button has just been pressed, and no other buttons are in use...
48+ if (btnCur==0 && bnow==LOW) {
49+ btnCur = btn;
50+ if (btn==mainAdjUp) { curCathode++; if (curCathode==10 ) curCathode = 0 ; updateDisplay (); }
51+ else if (btn==mainAdjDn) { curAnode++; if (curAnode==sizeof (anodes)) curAnode = 0 ; updateDisplay (); }
52+ }
53+ // If the button has just been released...
54+ if (btnCur==btn && bnow==HIGH) {
55+ btnCur = 0 ;
56+ }
57+ }
58+
59+ void initOutputs () {
60+ for (byte i=0 ; i<4 ; i++) { pinMode (binOutA[i],OUTPUT); pinMode (binOutB[i],OUTPUT); }
61+ for (byte i=0 ; i<3 ; i++) { pinMode (anodes[i],OUTPUT); }
62+ }
63+
64+ void updateDisplay () {
65+ for (byte i=0 ; i<sizeof (anodes); i++) { digitalWrite (anodes[i],LOW); } // all anodes off
66+ setCathodes (curCathode,curCathode);
67+ digitalWrite (anodes[curAnode],HIGH);
68+ }
69+
70+ void setCathodes (byte decValA, byte decValB){
71+ bool binVal[4 ]; // 4-bit binary number with values [1,2,4,8]
72+ decToBin (binVal,decValA); // have binary value of decVal set into binVal
73+ for (byte i=0 ; i<4 ; i++) digitalWrite (binOutA[i],binVal[i]); // set bin inputs of SN74141
74+ decToBin (binVal,decValB);
75+ for (byte i=0 ; i<4 ; i++) digitalWrite (binOutB[i],binVal[i]); // set bin inputs of SN74141
76+ } // end setCathodes()
77+
78+ void decToBin (bool binVal[], byte i){
79+ // binVal is a reference (modify in place) of a binary number bool[4] with values [1,2,4,8]
80+ if (i<0 || i>15 ) i=15 ; // default value, turns tubes off
81+ binVal[3 ] = int (i/8 )%2 ;
82+ binVal[2 ] = int (i/4 )%2 ;
83+ binVal[1 ] = int (i/2 )%2 ;
84+ binVal[0 ] = i%2 ;
85+ } // end decToBin()
0 commit comments