Alternative Arduino code


/*
B (digital pin 8 to 13)
C (analog input pins)
D (digital pins 0 to 7)

DDRD - The Port D Data Direction Register - r/w
PORTD - The Port D Data Register - r/w
PIND - The Port D Input Pins Register - r

use D1 to D7 as output to 7-segment display
and B1 to B3 as 4-bit input ranging from 0 to 15 (displaying 0-F)
*/
#include <Arduino.h>


int main(void) {
  // array of 16 rows, each 7 columns, e.g. leds[0] a=1,b=1,c=1,d=1,e=1,f=1,g=0
    int leds[16][7]= { 
      {1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1}, // 0 1 2 3 4 5
      {1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1},{1,1,1,0,1,1,1},{0,0,1,1,1,1,1}, // 6 7 8 9 A b 
      {1,0,0,1,1,1,0},{0,1,1,1,1,0,1},{1,0,0,1,1,1,1},{1,0,0,0,1,1,1}  // C d E F
    };
    // easier but not intended would be: int leds[16]  = { 0x7E, 0x30, 0x6D, ...... 0x47};
    byte led_pins;
    int j;
    byte old=16;
    byte number=0;
     
    init(); //Initializes timers for millis(), delay(), etc.

    // Your "setup" code here
    DDRD= DDRD | B11111110;  // 1=output 
    DDRB = DDRB & B11110000; // 0=input 
    PORTB = PORTB | B00001111;   // 1=input_pullup  like digitalWrite(pin,high) to activate internal pullup                       
    number = (PINB & 0x0F) ^ 0x0F; // make upper 4-bits zero, next XOR: make 0->1 1->0, because of PCB design 
    

         
    while (1) {
     while (number == old) number=(PINB & 0x0F) ^ 0x0F;  // wait for input to change
      
      led_pins = 0;
      j=1;
      for (int i=0; i < 7; i++) {   // d0=x, d1=a d2=b d3=c d4=d d5=e d6=f d7=g
        led_pins += leds[number][i] << j;  // push each of 7 bits onto led_pins 
        j++; 
      }
      PORTD=led_pins;     
      old=number;
    }        
    
    return 0;
}