// Arduino UNO based encoder that converts 4-bit number on (A0..A3) pins to 7 segment display via (D2..D8) pins
// INPUT: A0..A3 --> 4-bit hex number
// OUTPUT: D2..D8 <-- 7 segment display
const int WAIT_AFTER_UPDATE = 500; // in milliseconds
const int INPUT_MODE = INPUT;
const bool IS_SEVEN_SEGMENT_COMMON_MODE_SET_TO_ANODE = true; // "anode" mode by default, LOW signal lights up the LED
// in "cathode" mode, HIGH signal lights up the LED
// see https://docs.wokwi.com/parts/wokwi-7segment for details
const int INPUT_FIRST_PIN = A0;
const int OUTPUT_FIRST_PIN = 2;
// 7 segment display, see https://docs.wokwi.com/parts/wokwi-7segment
// AAA
// F B
// F B
// GGG
// E C
// E C
// DDD
// A B C D E F G
const int SEGMENT_COUNT = 7;
const int SEGMENTS[16][SEGMENT_COUNT] = {
{ 1, 1, 1, 1, 1, 1, 0 }, // 0
{ 0, 1, 1, 0, 0, 0, 0 }, // 1
{ 1, 1, 0, 1, 1, 0, 1 }, // 2
{ 1, 1, 1, 1, 0, 0, 1 }, // 3
{ 0, 1, 1, 0, 0, 1, 1 }, // 4
{ 1, 0, 1, 1, 0, 1, 1 }, // 5
{ 1, 0, 1, 1, 1, 1, 1 }, // 6
{ 1, 1, 1, 0, 0, 0, 0 }, // 7
{ 1, 1, 1, 1, 1, 1, 1 }, // 8
{ 1, 1, 1, 1, 0, 1, 1 }, // 9
{ 1, 1, 1, 0, 1, 1, 1 }, // A
{ 0, 0, 1, 1, 1, 1, 1 }, // B
{ 1, 0, 0, 1, 1, 1, 0 }, // C
{ 0, 1, 1, 1, 1, 0, 1 }, // D
{ 1, 0, 0, 1, 1, 1, 1 }, // E
{ 1, 0, 0, 0, 1, 1, 1 }, // F
};
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
for (int q = 0; q < 4; ++q)
pinMode(INPUT_FIRST_PIN + q, INPUT_MODE);
for (int q = 0; q < SEGMENT_COUNT; ++q)
pinMode(OUTPUT_FIRST_PIN + q, OUTPUT);
}
int blinky = LOW;
void loop() {
int hex =
analogAsDigitalRead(INPUT_FIRST_PIN + 0) * 1 +
analogAsDigitalRead(INPUT_FIRST_PIN + 1) * 2 +
analogAsDigitalRead(INPUT_FIRST_PIN + 2) * 4 +
analogAsDigitalRead(INPUT_FIRST_PIN + 3) * 8;
for (int q = 0; q < SEGMENT_COUNT; ++q)
digitalWrite(OUTPUT_FIRST_PIN + q, (SEGMENTS[hex][q] ^ IS_SEVEN_SEGMENT_COMMON_MODE_SET_TO_ANODE) ? HIGH : LOW);
blinky = ~blinky;
digitalWrite(LED_BUILTIN, blinky);
delay(WAIT_AFTER_UPDATE);
}
int analogAsDigitalRead(int pin)
{
return analogRead(pin) >= 512 ? 1: 0;
}