Last active
August 29, 2015 13:57
-
-
Save ElectricCoffee/9401315 to your computer and use it in GitHub Desktop.
simple 2-digit display that shows the current light level in values ranging from 00 to FF
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
#define CLOCK_PIN 2 | |
#define LATCH_PIN 3 | |
#define DATA_PIN 4 | |
#define READ_PIN 0 | |
#define OPEN_LATCH digitalWrite(LATCH_PIN, 0) | |
#define CLOSE_LATCH digitalWrite(LATCH_PIN, 1) | |
byte digits[16]; | |
void setup() { | |
pinMode(LATCH_PIN, OUTPUT); | |
pinMode(READ_PIN, INPUT); | |
digits[0x0] = B00111111; // read right-to-left, not left-to-right | |
digits[0x1] = B00000110; | |
digits[0x2] = B01011011; | |
digits[0x3] = B01001111; | |
digits[0x4] = B01100110; | |
digits[0x5] = B01101101; | |
digits[0x6] = B01111101; | |
digits[0x7] = B00000111; | |
digits[0x8] = B01111111; | |
digits[0x9] = B01101111; | |
digits[0xA] = B01110111; | |
digits[0xB] = B01111100; | |
digits[0xC] = B00111001; | |
digits[0xD] = B01011110; | |
digits[0xE] = B01111001; | |
digits[0xF] = B01110001; | |
} | |
void loop() { | |
int reading = analogRead(READ_PIN); | |
int brightness = map(reading, 0, 0x3FF, 0, 0xFF); | |
int firstDigit = brightness / 0x10; | |
int secondDigit = brightness % 0x10; | |
OPEN_LATCH; | |
shiftOut(8, digits[secondDigit], DATA_PIN, CLOCK_PIN); | |
shiftOut(8, digits[firstDigit], DATA_PIN, CLOCK_PIN); | |
CLOSE_LATCH; | |
delay(100); | |
} | |
// the "bits" parameter was added for easy expandability; | |
// but I haven't made anything yet that fully utilises it | |
// so it's sorta just there as a leftover that I'm too lazy to edit | |
void shiftOut(int bits, byte input, int dataPin, int clockPin) { | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
int pinState; | |
digitalWrite(clockPin, 0); | |
digitalWrite(dataPin, 0); | |
for(int i = bits -1; i >= 0; i--) { | |
digitalWrite(clockPin, 0); | |
if(input & (1 << i)) | |
pinState = 1; | |
else | |
pinState = 0; | |
digitalWrite(dataPin, pinState); | |
digitalWrite(clockPin, 1); | |
digitalWrite(dataPin, 0); | |
} | |
digitalWrite(clockPin, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment