Deixo aqui a Classe e um exemplo de como usá-la.
Configuração de pinos (Digital) utilizada no exemplo:
Obs.: Está configuração pode ser modificada no momento em que é criada a instância da classe “Simple7Segments” (Simple7Segments display(2, 3, 4, 5, 6, 7, 8, 9);).
Simple7Segments.h (biblioteca)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /** * Simple seven segments LED display * */ #ifndef Simple7Segments_h #define Simple7Segments_h #include <inttypes.h> class Simple7Segments { private: uint8_t *_pins; uint8_t _count; public: Simple7Segments(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t); void showNumber(char); void showAll(); void clearAll(); }; #endif |
Simple7Segments.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | /** * Simple seven segments LED display * */ #include "Simple7Segments.h" #include "WProgram.h" #include <inttypes.h> #define QUANTITY_PINS 8 Simple7Segments::Simple7Segments(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f, uint8_t g, uint8_t pd) { // Alocate memory for Array _pins _pins = (uint8_t*)calloc(QUANTITY_PINS, 4); // Transfer parameters to array pins _pins[0] = a; _pins[1] = b; _pins[2] = c; _pins[3] = d; _pins[4] = e; _pins[5] = f; _pins[6] = g; _pins[7] = pd; // Set pin mode to all for (_count = 0; _count < QUANTITY_PINS; _count++) { pinMode(_pins[_count], OUTPUT); } } void Simple7Segments::showNumber(char letter) { clearAll(); switch (letter) { case '0': digitalWrite(_pins[0], HIGH); digitalWrite(_pins[1], HIGH); digitalWrite(_pins[2], HIGH); digitalWrite(_pins[3], HIGH); digitalWrite(_pins[4], HIGH); digitalWrite(_pins[5], HIGH); break; case '1': digitalWrite(_pins[1], HIGH); digitalWrite(_pins[2], HIGH); break; case '2': digitalWrite(_pins[0], HIGH); digitalWrite(_pins[1], HIGH); digitalWrite(_pins[6], HIGH); digitalWrite(_pins[4], HIGH); digitalWrite(_pins[3], HIGH); break; case '3': digitalWrite(_pins[0], HIGH); digitalWrite(_pins[1], HIGH); digitalWrite(_pins[6], HIGH); digitalWrite(_pins[2], HIGH); digitalWrite(_pins[3], HIGH); break; case '4': digitalWrite(_pins[5], HIGH); digitalWrite(_pins[6], HIGH); digitalWrite(_pins[1], HIGH); digitalWrite(_pins[2], HIGH); break; case '5': digitalWrite(_pins[0], HIGH); digitalWrite(_pins[5], HIGH); digitalWrite(_pins[6], HIGH); digitalWrite(_pins[2], HIGH); digitalWrite(_pins[3], HIGH); break; case '6': digitalWrite(_pins[0], HIGH); digitalWrite(_pins[2], HIGH); digitalWrite(_pins[3], HIGH); digitalWrite(_pins[4], HIGH); digitalWrite(_pins[5], HIGH); digitalWrite(_pins[6], HIGH); break; case '7': digitalWrite(_pins[0], HIGH); digitalWrite(_pins[1], HIGH); digitalWrite(_pins[2], HIGH); break; case '8': digitalWrite(_pins[0], HIGH); digitalWrite(_pins[1], HIGH); digitalWrite(_pins[2], HIGH); digitalWrite(_pins[3], HIGH); digitalWrite(_pins[4], HIGH); digitalWrite(_pins[5], HIGH); digitalWrite(_pins[6], HIGH); break; case '9': digitalWrite(_pins[0], HIGH); digitalWrite(_pins[1], HIGH); digitalWrite(_pins[2], HIGH); digitalWrite(_pins[5], HIGH); digitalWrite(_pins[6], HIGH); break; } } void Simple7Segments::clearAll() { for (_count = 0; _count < QUANTITY_PINS; _count++) { digitalWrite(_pins[_count], LOW); } } void Simple7Segments::showAll() { for (_count = 0; _count < QUANTITY_PINS; _count++) { digitalWrite(_pins[_count], HIGH); } } |
SevenSegmentsExample.pde
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /** * Simple seven segments LED display * * */ #include "Simple7Segments.h" #define LED_FINISH 12 #define DELAY 500 unsigned int count = 0; // Define variable display. // The numbers are the pins of the display Simple7Segments display(2, 3, 4, 5, 6, 7, 8, 9); void setup() { pinMode(LED_FINISH, OUTPUT); } void loop() { char* buffer; itoa(count, buffer, 10); display.showNumber(buffer[0]); delay(DELAY); if (++count == 10) { count = 0; finish(); } } void finish() { digitalWrite(LED_FINISH, HIGH); delay(50); digitalWrite(LED_FINISH, LOW); delay(50); digitalWrite(LED_FINISH, HIGH); delay(50); digitalWrite(LED_FINISH, LOW); } |
Análise:
Linha 17) Cria uma instância da classe Simple7Segments passando no construtor os pinos correspondentes as posições no display.
Linha 27) Converte o valor da variável contadora (count) para char* (“String”).
Linha 28) Chama o método showNumber da instância display passando o número a exibir.
Linha 37) Método chamado quando uma contagem de 0 à 9 chega ao fim (pisca o LED).
Faça o download do código fonte completo aqui.
Nenhum comentário:
Postar um comentário