Created
January 5, 2015 12:48
-
-
Save hdo/9c448c2a283778d92695 to your computer and use it in GitHub Desktop.
Arduino code for controlling cheap china 7-segment led tube (simple counter)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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