TerusTheBird

arduino digit display

Sep 8th, 2021 (edited)
1,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. // HIGH and LOW are aliases for 1 and 0 respectively
  2. // each of these uses only 1 byte of memory
  3.  
  4. const unsigned char digit_bits[] = {
  5.     0b11111100  // 0
  6.     0b01100000, // 1
  7.     0b11011010, // 2
  8.     0b11110010, // 3
  9.     0b01100110, // 4
  10.     0b10110110, // 5
  11.     0b10111110, // 6
  12.     0b11100000, // 7
  13.     0b11111110, // 8
  14.     0b11110110, // 9
  15. // you could have bit patterns for other digits after this, such as hexadecimal
  16. };
  17.  
  18. void display_digit( int which_digit, int number ) {
  19.     unsigned char bits;
  20.     digitalWrite( which_digit, HIGH );
  21.    
  22.     bits = ~digit_bits[ number ];
  23.     // flip all the bits, since 0 means the segment is on apparently
  24.     // this could also overrun the array and show garbage, but that's the fun part
  25.    
  26.     // write the bits out backwards, so that the definitions are more readable
  27.     digitalWrite( SEG_DOT, bits & 1 ); bits >>= 1;
  28.     digitalWrite( SEG_G,   bits & 1 ); bits >>= 1;
  29.     digitalWrite( SEG_F,   bits & 1 ); bits >>= 1;
  30.     digitalWrite( SEG_E,   bits & 1 ); bits >>= 1;
  31.     digitalWrite( SEG_D,   bits & 1 ); bits >>= 1;
  32.     digitalWrite( SEG_C,   bits & 1 ); bits >>= 1;
  33.     digitalWrite( SEG_B,   bits & 1 ); bits >>= 1;
  34.     digitalWrite( SEG_A,   bits );
  35.    
  36.     delay( GHOST_DELAY );
  37.     digitalWrite( which_digit, LOW );
  38. }
Add Comment
Please, Sign In to add comment