Skip to content

Instantly share code, notes, and snippets.

@hdo
Created January 5, 2015 12:48
Show Gist options
  • Save hdo/9c448c2a283778d92695 to your computer and use it in GitHub Desktop.
Save hdo/9c448c2a283778d92695 to your computer and use it in GitHub Desktop.
Arduino code for controlling cheap china 7-segment led tube (simple counter)
#include <Arduino.h>
#define LED_PIN 13 // LED connected to digital pin 13
#define SW_PIN 10
#define CLOCK 7
#define LATCH 5
#define DATA 6
byte VALUE_CONSTANT[] ={ B11000000, // 0
B11111001, // 1
B10100100, // 2
B10110000, // 3
B10011001, // 4
B10010010, // 5
B10000010, // 6
B11111000, // 7
B10000000, // 8
B10010000, // 9
B11111111};// nothing
uint32_t loop_counter = 0;
uint32_t counter = 0;
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(CLOCK, OUTPUT);
pinMode(LATCH, OUTPUT);
pinMode(DATA, OUTPUT);
digitalWrite(CLOCK, HIGH);
digitalWrite(LATCH, HIGH);
digitalWrite(DATA, HIGH);
}
void displayDigitAt(byte value, byte index) {
if (value >= 0 && value <= 10 && index >= 0 && index < 4) {
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLOCK, MSBFIRST, VALUE_CONSTANT[value]);
shiftOut(DATA, CLOCK, MSBFIRST, (1 << index ));
digitalWrite(LATCH, HIGH);
}
}
void loop() {
// very simple delay
if (loop_counter++ > 5) {
loop_counter = 0;
// update counter
if (counter++ > 9999) {
counter = 0;
}
}
// display value
displayDigitAt(counter % 10, 0);
displayDigitAt((counter / 10) % 10, 1);
displayDigitAt((counter / 100) % 10, 2);
displayDigitAt((counter / 1000) % 10, 3);
}
int main(void) {
init();
setup();
while (true) {
loop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment