Skip to content

Instantly share code, notes, and snippets.

@FreeER
Created September 8, 2022 12:49
Show Gist options
  • Save FreeER/a94d04aa285b0e87f19cb6f6482b66ba to your computer and use it in GitHub Desktop.
Save FreeER/a94d04aa285b0e87f19cb6f6482b66ba to your computer and use it in GitHub Desktop.
playing around with a seven segment display and a 74HC595N shift register from the elegoo mega 2560 kit
// https://www.ti.com/lit/ds/symlink/sn74hc595.pdf?ts=1662270700415
// https://www.youtube.com/watch?v=Ys2fu4NINrA
// https://lastminuteengineers.b-cdn.net/wp-content/uploads/arduino/7-Segment-Common-Anode-Common-Cathode-Pinout.png
enum { OE = 8,
LATCH = 10,
CLK = 11,
SER /*serial*/ = 12 };
#define BUTTON 13
enum { A = 128,
B = 1,
C = 8,
D = 4,
E = 2,
F = 64,
G = 32,
DP = 16 };
u8 digits[10] = { ~G & ~DP, B | C, ~F & ~C & ~DP, ~F & ~E & ~DP, F | B | C | G, ~B & ~E & ~DP, ~B & ~DP, A | B | C, ~DP, ~E & ~DP };
void set(u8 bits) {
digitalWrite(LATCH, LOW); // lock
shiftOut(SER, CLK, MSBFIRST, bits);
digitalWrite(LATCH, HIGH); // unlock/update
}
void setOutput(bool state) {
digitalWrite(OE, !state);
}
void setup() {
pinMode(LATCH, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(SER, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
pinMode(OE, OUTPUT);
setOutput(true);
Serial.begin(9600);
}
u8 i = 0;
unsigned long buttonTime;
unsigned long flashTime;
unsigned long dpTime;
bool dp = false;
void loop() {
set(digits[i] | (dp ? DP : 0));
if (digitalRead(BUTTON) == LOW) {
setOutput(HIGH);
if (millis() - buttonTime > 350) {
buttonTime = millis();
if (++i >= sizeof(digits) / sizeof(*digits)) i = 0;
}
/*
if(i==0) i = 1;
else i *= 2;
*/
} else if (millis() - flashTime > 750) {
flashTime = millis();
setOutput(digitalRead(OE));
}
if (millis() - dpTime > 150) {
dpTime = millis();
if (random(0, 2)) dp = !dp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment