Skip to content

Instantly share code, notes, and snippets.

@AdamLoi
Created May 28, 2014 08:05
Show Gist options
  • Save AdamLoi/4e668a3c5f60e0074245 to your computer and use it in GitHub Desktop.
Save AdamLoi/4e668a3c5f60e0074245 to your computer and use it in GitHub Desktop.
//GRobotronics 28-5-2014
// Arduino pins connected to the 4511
const uint8_t LedA = 5;
const uint8_t LedB = 8;
const uint8_t LedC = 7;
const uint8_t LedD = 6;
// Arduino pins connected to the segment driver transistors
const uint8_t Led1 = 1;
const uint8_t Led2 = 4;
const uint8_t Led3 = 3;
const uint8_t Led4 = 2;
void setup()
{
// Let the Arduino know which pins go where
pinMode(LedA, OUTPUT);
pinMode(LedB, OUTPUT);
pinMode(LedC, OUTPUT);
pinMode(LedD, OUTPUT);
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Led4, OUTPUT);
digitalWrite(Led1, LOW);
digitalWrite(Led2, LOW);
digitalWrite(Led3, LOW);
digitalWrite(Led4, LOW);
}
void loop()
{
print_number(9874);
}
void print_number(unsigned n) {
static int d = 2;
set_digit(Led1, d, n/1000);
set_digit(Led2, d, n/100 );
set_digit(Led3, d, n/10 );
set_digit(Led4, d, n/1 );
}
void set_digit(const uint8_t led, const unsigned d, const unsigned value) {
set_number(value);
digitalWrite(led, HIGH);
delay(d);
digitalWrite(led, LOW);
}
void set_number(const unsigned n) {
static const struct number {
uint8_t d;
uint8_t c;
uint8_t b;
uint8_t a;
} numbers[] = {
{ LOW, LOW, LOW, LOW}, /* 0 */
{ LOW, LOW, LOW, HIGH}, /* 1 */
{ LOW, LOW, HIGH, LOW}, /* 2 */
{ LOW, LOW, HIGH, HIGH}, /* 3 */
{ LOW, HIGH, LOW, LOW}, /* 4 */
{ LOW, HIGH, LOW, HIGH}, /* 5 */
{ LOW, HIGH, HIGH, LOW}, /* 6 */
{ LOW, HIGH, HIGH, HIGH}, /* 7 */
{HIGH, LOW, LOW, LOW}, /* 8 */
{HIGH, LOW, LOW, HIGH}, /* 9 */
};
digitalWrite(LedA, numbers[n%10].a);
digitalWrite(LedB, numbers[n%10].b);
digitalWrite(LedC, numbers[n%10].c);
digitalWrite(LedD, numbers[n%10].d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment