Tuesday, March 18, 2014

Arduino–Day 5: 7 Segment Numeral Display

 

A quick & dirty library for displaying numbers on a 7 Segment Display.  I’m storing the segment patterns in an array at the same position – ordinal - as that of the number.  Thus, a pattern for ‘0’ (Pin 10 & Pin 4 off – Dot & Central bar) would be found at numbers[0].

Countdown on a 7 Seg Display
  1. //Pin patterns for 0-9
  2. int numbers[][8] =  {
  3.     { 1, 1, 1, 0, 1, 1, 1, 0 },
  4.     { 0, 0, 1, 0, 1, 0, 0, 0 },
  5.     { 1, 1, 0, 0, 1, 1, 0, 1 },
  6.     { 0, 1, 1, 0, 1, 1, 0, 1 },
  7.     { 0, 0, 1, 0, 1, 0, 1, 1 },
  8.     { 0, 1, 1, 0, 0, 1, 1, 1 },
  9.     { 1, 1, 1, 0, 0, 1, 1, 1 },
  10.     { 0, 0, 1, 0, 1, 1, 0, 0 },
  11.     { 1, 1, 1, 0, 1, 1, 1, 1 },
  12.     { 0, 1, 1, 0, 1, 1, 1, 1 },
  13. };
  14.  
  15. // Pin 1, 2, 4, 5, 6, 7, 9, 10 anti clockwise starting from lower left corner
  16. int pins[] = {3, 4, 6, 7, 8, 9, 11, 12};
  17.  
  18.  
  19.  
  20. void setup()
  21. {
  22.  
  23.   /* add setup code here */
  24.  
  25. }
  26.  
  27. void loop()
  28. {
  29.  
  30.   /* add main program code here */
  31.  
  32.     for (int number = 9; number <= 0; number++)
  33.     {
  34.         displayNumber(number);
  35.         //Wait 1 second
  36.         delay(1000);
  37.     }
  38.  
  39.     //Display four random number
  40.     for (int i = 1; i < 5; i++)
  41.     {
  42.         int next = random(0, 9);
  43.         displayNumber(next);
  44.         clear();
  45.         delay(1000);
  46.     }
  47.  
  48.     delay(1000);
  49. }
  50.  
  51.  
  52. void displayNumber(int number)
  53. {
  54.     for (int i = 0; i < 8; i++)
  55.     {
  56.         //determine pin for the segment 0-7
  57.         int pin = pins[i];
  58.         bool pinOffOn = numbers[number][i];
  59.  
  60.         pinMode(pin, OUTPUT);
  61.         //Turn on the segment
  62.         digitalWrite(pin, (pinOffOn) ? HIGH : LOW);
  63.     }
  64. }
  65.  
  66.  
  67. void clear()
  68. {
  69.     for (int i = 0; i < 8; i++)
  70.     {
  71.         int pin = pins[i];
  72.         pinMode(pin, OUTPUT);
  73.         digitalWrite(pin, LOW);
  74.     }
  75. }

The above approach is cost prohibitive (number of pins needed to drive a 4 digit number is staggering).

Not knowing Arduino’s capabilities, I’m hypothesizing (speculating) thus:

  1. Either:
    • Arduino should be able to drive multiple outputs through a single pin
    • One pin per digit... Possibly 2 pins for n-number of digits
  2. Or:
    • We can send multiple values through a single Arduino digital pin punctuated by a specific delay (say, 10 millisecond increments). 
    • A timer would measure the difference, and route power to appropriate segment
    • Pros: One pin for each digit
  3. Or:
    • Identify the digit by a specific delay upfront (100 millisecond increments)
    • Route to the right digit
    • All subsequent power would be sent to the same digit (till it's reset by a specific delay)
    • We can send multiple values through a single Arduino digital pin punctuated by a specific delay (say, 10 millisecond increments).
    • A timer would measure the difference, and route power to appropriate segment
      • Pros: One pin for the whole display

1 comment:

for IT the said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.