Skip to content

Instantly share code, notes, and snippets.

@hcwiley
Last active August 29, 2015 14:02
Show Gist options
  • Save hcwiley/328c66e5e8fbe1d435c8 to your computer and use it in GitHub Desktop.
Save hcwiley/328c66e5e8fbe1d435c8 to your computer and use it in GitHub Desktop.
#define SHIFTDATA 11
#define SHIFTCLOCK 13
#define SHIFTLATCH 10
// the setup routine runs once when you press reset:
void setup() {
// open a serial port
Serial.begin(9600);
// set up shift register pins
// serial data
pinMode(SHIFTDATA, OUTPUT);
digitalWrite(SHIFTDATA, LOW);
// serial clock
pinMode(SHIFTCLOCK, OUTPUT);
digitalWrite(SHIFTCLOCK, LOW);
// latch
pinMode(SHIFTLATCH, OUTPUT);
digitalWrite(SHIFTLATCH, LOW);
}
// the loop routine runs over and over again forever:
// seven segment: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
byte digit[10] = {63, 6, 91, 79, 102, 109, 125, 7, 127, 103};
int d = 0;
void loop() {
Serial.println(digit[d]);
shiftData(digit[d]);
d++;
if (d >= 10) {
d = 0;
}
delay(500);
}
void shiftData(byte b) {
// loop through bits 0 - 7 of byte b
for (int i=7; i>=0; i--) {
// set data to high or low depending on bit
// google 'bitwise and' to see what '&' does
// google 'bit shift' to see what << does
// if evaluates true if not zero, false if zero
if (b & (1<<i)) {
digitalWrite(SHIFTDATA, HIGH);
Serial.print(1);
}
else {
digitalWrite(SHIFTDATA, LOW);
Serial.print(0);
}
// pulse the clock line
digitalWrite(SHIFTCLOCK, HIGH);
digitalWrite(SHIFTCLOCK, LOW);
}
// latch the data from the shift register to the output register
// pulse latch
digitalWrite(SHIFTLATCH, HIGH);
digitalWrite(SHIFTLATCH, LOW);
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment