cookchar

14 Segment Display Revolving Message Sketch

Jul 31st, 2019
125
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     14 Segment Display Revolving Message Sketch
  3.  
  4.     By Charlie Cook a.k.a. C-Squared
  5.     Last Edited July 31st, 2019
  6.    
  7.     This sketch allows for the storage and presentation of alphanumeric messages
  8.     across four digits of 14 segments each. The hardware needed besides the
  9.     displays, which themselves are LPT-3784 common-cathode modules from Lite On,
  10.     is just one (1) CD4051 or CD4052 analog multiplexer, and two (2) CD4094
  11.     SIPO shift registers.
  12.    
  13.     The shift registers should be hooked up head-to-tail (i.e. Q_s of the first chip
  14.     should connect to DATA of the second chip; also tie OUT.EN. high), and the first
  15.     14 bits should be connected to the 14 seg. displays on pins A thru P
  16.     (the letters I and O are not used to avoid ambiguity). The last 2 bits
  17.     of the shift registers should be fed to the A & B addressing pins of the
  18.     multiplexer. The lower four connection pins of the mux should connect to the
  19.     cathodes of the individual digits of the display.
  20.  
  21.     Details on which cathodes are which and how the pins map from their letter names
  22.     to numbers can be found in the datasheet for the LPT-3784. It should be noted
  23.     that this sheet neglects to inform that the numbering of the pins is like that
  24.     of DIP logic chips, where pin 1 is in the lower left corner, then the number
  25.     increases counter-clockwise to pin 18 in the upper left corner.
  26.  
  27.     Once all connections are made, The DATA, CLOCK, and STROBE inputs of the 4094s
  28.     should connect to digital pins 2, 3, and 4 respectively. After that, simply use
  29.     the Arduino IDE's serial monitor or something like moserial to set the message.
  30.     Don't worry if when you boot it up the first time and see garbage, it won't
  31.     impede writes from a serial link.
  32.  
  33.     One last note, my fontface, contained in the unsigned int arrays, only handles
  34.     alphanumeric characters at this time. All non-A.N. characters will be rendered
  35.     blank on the display. Also, some of the lowercase letters can look weird, so I
  36.     recommend sticking to capitals.
  37. */
  38.  
  39. #include <EEPROM.h>
  40. #define DATAPIN 2
  41. #define CLOCKPIN 3
  42. #define STROBEPIN 4
  43. #define NEXTDIGIT 0x40
  44. #define DIGITSHIFT 6
  45. #define HIGHSHIFT 8
  46. #define LOWMASK 0x00FF
  47. #define DIGITDIFF 0x30
  48. #define LOWERCASEDIFF 0x61
  49. #define UPPERCASEDIFF 0x41
  50.  
  51. const unsigned int segTransNum[10] = {
  52.     0x08BF,
  53.     0x0086,
  54.     0x1099,
  55.     0x010F,
  56.     0x1126,
  57.     0x210D,
  58.     0x1239,
  59.     0x0881,
  60.     0x113F,
  61.     0x210F
  62. };
  63. const unsigned int segTransUpp[26] = {
  64.     0x0586,
  65.     0x054F,
  66.     0x0039,
  67.     0x044F,
  68.     0x1039,
  69.     0x1131,
  70.     0x013D,
  71.     0x1136,
  72.     0x0449,
  73.     0x0851,
  74.     0x12B0,
  75.     0x0038,
  76.     0x20B6,
  77.     0x2236,
  78.     0x003F,
  79.     0x10B1,
  80.     0x023F,
  81.     0x12B1,
  82.     0x112D,
  83.     0x0441,
  84.     0x003E,
  85.     0x2206,
  86.     0x0A36,
  87.     0x2A80,
  88.     0x2480,
  89.     0x0889
  90. };
  91. const unsigned int segTransLow[26] = {
  92.     0x1A08,
  93.     0x1238,
  94.     0x1118,
  95.     0x090E,
  96.     0x0382,
  97.     0x1580,
  98.     0x210F,
  99.     0x1230,
  100.     0x0104,
  101.     0x0850,
  102.     0x06C0,
  103.     0x0440,
  104.     0x1514,
  105.     0x1210,
  106.     0x111C,
  107.     0x3030,
  108.     0x2107,
  109.     0x1110,
  110.     0x0308,
  111.     0x1540,
  112.     0x001C,
  113.     0x0204,
  114.     0x0A14,
  115.     0x2A80,
  116.     0x2880,
  117.     0x1808
  118. };
  119.  
  120. //const char message[] = "0123456789  ABCDEFGHIJKLMNOPQRSTUVWXYZ  abcdefghijklmnopqrstuvwxyz   ";
  121. //const byte mesgLen = sizeof(message) - 1;
  122.  
  123. byte getDigit(byte d, unsigned int n) {
  124.     unsigned int powTen = 1;
  125.     byte k;
  126.     for (k = 1; k < d; k++) {
  127.         powTen *= 10;
  128.     }
  129.     return (byte) ((n / powTen) % 10);
  130. }
  131. unsigned int translate(char c) {
  132.     if (isDigit(c)) {
  133.         return segTransNum[c - DIGITDIFF];
  134.     } else if (isLowerCase(c)) {
  135.         return segTransLow[c - LOWERCASEDIFF];
  136.     } else if (isUpperCase(c)) {
  137.         return segTransUpp[c - UPPERCASEDIFF];
  138.     } else {
  139.         return 0;
  140.     }
  141. }
  142.  
  143. byte i, j, high, low, loops, temp;
  144. char c;
  145. unsigned int t;
  146. String message;
  147. byte mesgLen;
  148. void setup() {
  149.     pinMode(DATAPIN, OUTPUT);
  150.     pinMode(CLOCKPIN, OUTPUT);
  151.     pinMode(STROBEPIN, OUTPUT);
  152.  
  153.     digitalWrite(DATAPIN, LOW);
  154.     digitalWrite(CLOCKPIN, LOW);
  155.     digitalWrite(STROBEPIN, HIGH);
  156.  
  157.     loops = 0;
  158.     j = 0;
  159.     message = "";
  160.  
  161.     temp = EEPROM.read(0);
  162.     if (temp <= 128) {
  163.         i = 1;
  164.         do {
  165.             c = EEPROM.read(i);
  166.             message.concat(c);
  167.             i++;
  168.         } while (i <= temp && c != 0 && c != 0xFF);
  169.     } else {
  170.         message = "PLEASE SET THIS MESSAGE VIA A SERIAL LINK   ";
  171.     }
  172.     mesgLen = message.length();
  173.  
  174.     Serial.begin(9600);
  175.     Serial.println("14 Segment Display with Programmable Message");
  176.     Serial.println("Stored message length: " + String(mesgLen));
  177.     Serial.println("Send a new message if you like (limit of 128 chars)");
  178.     Serial.print("$ ");
  179. }
  180.  
  181. void loop() {
  182.     for (i = 0; i < 4; i++) {
  183.         t = translate(message[(j + 3 - i) % mesgLen]);
  184.         high = (byte) (t >> HIGHSHIFT);
  185.         low = (byte) (t & LOWMASK);
  186.         high += (i << DIGITSHIFT);
  187.         digitalWrite(STROBEPIN, LOW);
  188.         shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, high);
  189.         shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, low);
  190.         digitalWrite(STROBEPIN, HIGH);
  191.         delay(5);
  192.     }
  193.  
  194.     loops++;
  195.     if (loops == 10) {
  196.         loops = 0;
  197.         j++;
  198.         j %= mesgLen;
  199.     }
  200.    
  201.     if (Serial.available() > 0) {
  202.         message = "";
  203.         while (Serial.available() > 0) {
  204.             c = Serial.read();
  205.             message.concat(c);
  206.             delay(1);
  207.         }
  208.         if (message.length() > 0) {
  209.             mesgLen = message.length();
  210.             Serial.println("Message recieved! Length: " + String(mesgLen));
  211.             Serial.println("Message: " + message);
  212.             EEPROM.write(0, mesgLen);
  213.             for (i = 0; i < mesgLen; i++) {
  214.                 EEPROM.write(i + 1, message.charAt(i));
  215.             }
  216.             EEPROM.write(i + 1, 0);
  217.             Serial.println("Length Confirmation (Read from EEPROM): " + String(EEPROM.read(0)));
  218.             Serial.print("$ ");
  219.             j = 0;
  220.             loops = 0;
  221.         }
  222.     }
  223. }
RAW Paste Data