Advertisement
RuiViana

Contador_FOM

Jul 23rd, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. /*
  2.   Example to demonstrate how MULTIPLEX
  3.   and POV (Persistence of Vison) works,
  4.   using 3 Seven Segment Display on Arduino.
  5.   (No expansion ports used)
  6.   Licensed under MIT License.
  7.   02/03/2015 Gilmar Palega
  8. */
  9.  
  10.  
  11. int tpot;
  12. int pin_pot = 6;
  13. int Regre = 13;
  14. const int pin_a = 7;
  15. const int pin_b = 8;
  16. const int pin_c = 9;
  17. const int pin_d = 10;
  18. const int pin_e = 11;
  19. const int pin_f = 12;
  20. const int pin_g = 3;
  21. const int pin_com1 = 4;
  22. const int pin_com2 = 5;
  23. //--------------------------
  24. void setup()
  25. {
  26.   pinMode(pin_a, OUTPUT);
  27.   pinMode(pin_b, OUTPUT);
  28.   pinMode(pin_c, OUTPUT);
  29.   pinMode(pin_d, OUTPUT);
  30.   pinMode(pin_e, OUTPUT);
  31.   pinMode(pin_f, OUTPUT);
  32.   pinMode(pin_g, OUTPUT);
  33.   pinMode(pin_com1, OUTPUT);
  34.   pinMode(pin_com2, OUTPUT);
  35.   pinMode(pin_pot, INPUT_PULLUP);
  36.   pinMode(Regre, INPUT_PULLUP);
  37.   Serial.begin(9600);
  38. }
  39.  
  40. /*
  41.   Conversion table number to bit
  42.   If a segment is used to compose the digit, then sum the bit value
  43.   A B C D E F G
  44.   # 1 2 4 8 16  32  64
  45.   ------------------------------------------------------------------
  46.   0 1 1 1 1 1 1   63
  47.   1   1 1         6
  48.   ------------------------------------------------------------------
  49.   2 1 1   1 1   1 91
  50.   3 1 1 1 1     1 79
  51.   ------------------------------------------------------------------
  52.   4   1 1     1 1 102
  53.   5 1   1 1   1 1 109
  54.   ------------------------------------------------------------------
  55.   6 1   1 1 1 1 1 125
  56.   7 1 1 1         7
  57.   ------------------------------------------------------------------
  58.   8 1 1 1 1 1 1 1 127
  59.   9 1 1 1     1 1 103
  60. */
  61. //--------------------------
  62. int get_7SD_value(int n)
  63. {
  64.   if (n > 9) n = 9;
  65.   if (n < 0) n = 0;
  66.  
  67.   switch (n)
  68.   {
  69.     case 0: return 63; break;
  70.     case 1: return 6; break;
  71.     case 2: return 91; break;
  72.     case 3: return 79; break;
  73.     case 4: return 102; break;
  74.     case 5: return 109; break;
  75.     case 6: return 125; break;
  76.     case 7: return 7; break;
  77.     case 8: return 127; break;
  78.     case 9: return 103;
  79.   }
  80.   return 0;
  81. }
  82. /*
  83.   ATTENTION: I'm using Common Anodes 7 Segment Display.
  84.   For Commom Cathodes, you have to invert HIGH and LOW on code bellow.
  85. */
  86. //--------------------------
  87. void check_pin(int pin, int b)
  88. {
  89.   if (b == 1)
  90.   {
  91.     digitalWrite(pin, HIGH);
  92.     delay(1);
  93.     digitalWrite(pin, LOW);
  94.   }
  95.   else
  96.     digitalWrite(pin, LOW);
  97. }
  98. /*
  99.   ATTENTION: I'm using Common Anodes 7 Segment Display.
  100.   For Commom Cathodes, you have to invert HIGH and LOW on code bellow.
  101. */
  102. //--------------------------
  103. void display_7sd(int v, int pin_com)
  104. {
  105.   if (v > 9) v = 9;
  106.   if (v < 0) v = 0;
  107.   int n = get_7SD_value(v);
  108.   digitalWrite(pin_com, LOW);
  109.  
  110.   /* check all segments, and turn on the necessary ones to compose the number */
  111.   check_pin(pin_a, bitRead(n, 0));
  112.   check_pin(pin_b, bitRead(n, 1));
  113.   check_pin(pin_c, bitRead(n, 2));
  114.   check_pin(pin_d, bitRead(n, 3));
  115.   check_pin(pin_e, bitRead(n, 4));
  116.   check_pin(pin_f, bitRead(n, 5));
  117.   check_pin(pin_g, bitRead(n, 6));
  118.  
  119.   digitalWrite(pin_com, HIGH);
  120. }
  121. //--------------------------
  122. void display_value(int v)
  123. {
  124.   int n;
  125.   n = v / 10;
  126.   v = v - (n * 10);
  127.   display_7sd(v, pin_com1);
  128.   display_7sd(n, pin_com2);
  129. }
  130. //--------------------------
  131. void loop()
  132. {
  133.   if (digitalRead(pin_pot) == LOW)
  134.   {
  135.     tpot++;
  136.     if (tpot > 99)tpot = 0;
  137.     delay(100);
  138.     Serial.println(tpot);
  139.   }
  140.   display_value(tpot);
  141.   if (digitalRead(Regre) == LOW)
  142.   {
  143.     unsigned long Tempo = millis();                   // Marca tempo de inicio
  144.     unsigned long Contador = 1000;                    // 1 Seg
  145.     while (tpot > 0)
  146.     {
  147.       if ((millis() - Tempo) >= Contador)   // Se passou  o Tempo
  148.       {
  149.         Tempo = millis();                   // Recarrega o Tempo
  150.         tpot--;                             // Decrementa
  151.         Serial.println(tpot);
  152.       }
  153.       display_value(tpot);
  154.     }
  155.   }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement