// this program takes a combination of four different buttons as an input, and outputs the corresponding hex digit as an output.
// pins for the four buttons
int buttons[4] = {A3,A2,A1,A0};
// pins for the seven segments.
// you might have to adjust this line
// if you are using something other
// than the adafruit trinket.
int light[7] = {0,1,3,4,5,6,8};
// configurations for the sixteen digits.
// see the hand-drawn charts above for details.
int digits[16] = {0x3F, 0x9, 0x5E, 0x5B, 0x69, 0x73, 0x77, 0x19, 0x7F, 0x7B, 0x7D, 0x67, 0x36, 0x4F, 0x76, 0x74};
// variable for button state
int state = 0;
void setup() {
// initialize button pins
for (int i=0;i<4;i++) {
pinMode(buttons[i], INPUT);
}
// initialize display pins
for (int i=0;i<7;i++) {
pinMode(light[i], OUTPUT);
}
}
void loop() {
//initialize number to zero
int number = 0;
//read buttons
for (int i=0;i<4;i++) { // for each button...
state = digitalRead(buttons[i]); // find out if it's pressed or not.
if (state == HIGH) { // if it's pressed...
number += 1<<i; // add 2^i to the number.
}
}
//display number
for (int i=0;i<7;i++) { // for each segment...
if (digits[number] & 1<<i) { // if the number requires that segment...
digitalWrite(light[i], HIGH); // light up the segment!
}
else { // otherwise...
digitalWrite(light[i], LOW); // don't light up that segment.
}
}
}
Comments