Forum Coding Corner A little digital display fun

Viewing 1 reply thread
  • Author
    Posts
    • #6860
      vince

        I was curious about how the digital segments were mapped so I wrote a little sketch to figure it out. Since I’m new to the language, I started with the digital clock, stripped out what I wouldn’t need and started from there. Hope you enjoy.

        // Quick sketch to show use of digital segments.

        //RTC Stuff
        #include “RTClib.h” // Include RTClib library
        RTC_DS1307 rtc; // Create an instance of the DS1307

        //DIGITISER Stuff

        const int OE = 10;
        const int SER = 3;
        const int CLK = 4;
        const int LATCH = 5;

        const int sw1 = A0;
        const int sw2 = A2;
        const int Pot = A1;

        int potRead = 0;
        int potDimVal = 0;

        int num = 0;

        const int digit[4] =
        {
        B00001110,B00001101,B00001011,B00000111
        };

        int digit0 = digit[0];
        int digit1 = digit[1];
        int digit2 = digit[2];
        int digit3 = digit[3];

        // Segment comments
        // The segments in each Digit display are addressed using a simple
        // switch matrix. Using 1 byte – each bit represents or addresses a single
        // segment of the display. A one is off and a zero is on.
        // Likewise each digit is selected using the switch matrix above these comments
        // with using binary one through 4.

        // The list below shows which bit turns on which segment.
        // Below the list is the content of the seg constant which places them in
        // the order of All off then rotating starting with the dot, lower left, upper left
        // etc. ending with the center segment then all on.

        const int seg[10]=
        {

        // B01111111 is Center Segment
        // B10111111 is Upper Left Segment
        // B11011111 is Top Segment
        // B11101111 is Upper Right Segment
        // B11110111 is Dot in Lower Right
        // B11111011 is Lower Right Segment
        // B11111101 is Bottom Segment
        // B11111110 is Lower Left Segment
        // B11111111 is All Segments Off
        // B00000000 is All Segments On

        B11111111,B11110111,B11111101,B11111110,B10111111,B11011111,B11101111,B11111011,B01111111,B00000000

        };
        int seg0 = seg[4];

        void setup() {

        // Start Serial Comminication

        Serial.begin(9600);

        //RTC Stuff

        rtc.begin();

        // Uncomment next line to set clock – then re-comment it out to prevent every time set

        rtc.adjust(DateTime(__DATE__,__TIME__));; // Set the RTC to Compile Date and Time

        //DIGITISER Stuff

        pinMode(SER, OUTPUT); // SER (data) pin as output
        pinMode(CLK, OUTPUT); // CLK (clock) pin as output
        pinMode(LATCH, OUTPUT); // LATCH pin as output
        pinMode(OE, OUTPUT); // Output Enable

        analogWrite(OE, 150); // Set brightness

        pinMode(sw1, INPUT); // Switch 1
        pinMode(sw2, INPUT); // Switch 2
        pinMode(Pot, INPUT); // Potentiometer

        }

        void loop() {

        //DIM DISPLAY USING POTENTIOMETER after each sequence of Digits

        potRead = analogRead(Pot); // Reads the pot and assigns potRead the same value
        potDimVal = map(potRead,0,1023,255,0); // maps potRead (0-1023) to potDimVal (255-0)
        analogWrite(OE,potDimVal); // Sets a PWM value (potDimVal) to the OE pin to set brightness

        int nm = 0;
        nm = 0;

        while (nm < 10)
        {

        // delay quarter second between segments
        int d1 = 250;

        // Push which segment to light
        Serial.println(nm);
        digitalWrite(LATCH, LOW);
        shiftOut(SER,CLK,LSBFIRST,seg[nm]);
        shiftOut(SER,CLK,LSBFIRST,digit0);
        digitalWrite(LATCH,HIGH);

        delay(d1);

        nm++;
        }

        }

      • #6865
        PBH

          It’s nice to see I am not the only one. This is the way we learn I guess. Well done.
          Peter

      Viewing 1 reply thread
      • You must be logged in to reply to this topic.