Skip to content

Instantly share code, notes, and snippets.

@yoniLavi
Created October 16, 2024 19:37
Show Gist options
  • Save yoniLavi/0590a1770ce87fc973ac0a26b185d580 to your computer and use it in GitHub Desktop.
Save yoniLavi/0590a1770ce87fc973ac0a26b185d580 to your computer and use it in GitHub Desktop.
A basic Arduino example of providing a digital reading of a potentiometer via a 7-segment display
// Potentiometer
int potPin = A0;
// 7-Segment Display
const int sA = 1;
const int sB = 7;
const int sC = 5;
const int sD = 4;
const int sE = 3;
const int sF = 2;
const int sG = 6;
const int allSegments[] = {sA, sB, sC, sD, sE, sF, sG};
const int digitPatterns[10][7] = {
// sA, sB, sC, sD, sE, sF, sG
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW}, // 0
{LOW, HIGH, HIGH, LOW, LOW, LOW, LOW}, // 1
{HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH}, // 2
{HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH}, // 3
{LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH}, // 4
{HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH}, // 5
{HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH}, // 6
{HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW}, // 7
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}, // 8
{HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH} // 9
};
void showDigit(int digit) {
for (int s = 0; s < 7; s++) {
digitalWrite(allSegments[s], digitPatterns[digit][s]);
}
}
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(allSegments[i], OUTPUT);
}
}
void loop() {
int potVal = analogRead(potPin);
int potDigit = map(potVal, 0, 1023, 0, 9);
showDigit(potDigit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment