Skip to content

Instantly share code, notes, and snippets.

@cbscribe
Created November 19, 2020 19:47
Show Gist options
  • Save cbscribe/68f6368c7aacc43e20341ed20c29cfa9 to your computer and use it in GitHub Desktop.
Save cbscribe/68f6368c7aacc43e20341ed20c29cfa9 to your computer and use it in GitHub Desktop.
Pick a random number and display it on the 7-segment display
int A = 7;
int B = 8;
int C = 9;
int D = 10;
int E = 11;
int F = 12;
int G = 13;
byte digits[] = {126, 48, 109, 121, 51,
91, 95, 112, 127, 115};
void setup(void)
{
pinMode(2, INPUT_PULLUP);
randomSeed(analogRead(A0));
for (int i = A; i <= G; i++)
{
pinMode(i, OUTPUT);
}
}
int n = 0;
int button;
int last_button;
void loop()
{
// This is our button pressing code from before
last_button = button;
button = digitalRead(2);
if (button == LOW && last_button == HIGH)
{
n = random(1, 6);
}
// Displays the digit "n" on the 7-segment display
byte mask = 64;
for (int segment = A; segment <= G; segment++)
{
digitalWrite(segment, digits[n] & mask);
mask = mask >> 1;
}
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment