Created
February 17, 2015 19:13
-
-
Save emilyhorsman/4596b7c78f8204936403 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Adafruit Arduino - Lesson 5. Serial Monitor | |
* Based on https://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor?view=all | |
* Slight modifications by Emily Horsman | |
* 'HC495 Datasheet: https://www.sparkfun.com/datasheets/IC/SN74HC595.pdf | |
*/ | |
int latchPin = 5; // RCLK (storage register clock) | |
int clockPin = 6; // SRCLK (shift register clock) | |
int dataPin = 4; // SER (serial input) | |
int outputEnablePin = 3; // OE (high => outputs in high-impedance state) | |
byte leds = 0; | |
void setup() { | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
pinMode(outputEnablePin, OUTPUT); | |
digitalWrite(outputEnablePin, LOW); | |
updateShiftRegister(); | |
Serial.begin(9600); | |
while (! Serial); | |
Serial.println("Enter LED Number 0 to 7 or 'x' to clear"); | |
} | |
void loop() { | |
if (!Serial.available()) return; | |
char ch = Serial.read(); | |
if (ch >= '0' && ch <= '7') { | |
int led = ch - '0'; | |
boolean currentState = bitRead(leds, led); | |
bitWrite(leds, led, !currentState); | |
updateShiftRegister(); | |
if (currentState) | |
Serial.print("Turned off LED "); | |
else | |
Serial.print("Turned on LED "); | |
Serial.println(led); | |
} else if (ch == 'x') { | |
leds = 0; | |
updateShiftRegister(); | |
Serial.println("Cleared"); | |
} | |
} | |
void updateShiftRegister() { | |
digitalWrite(latchPin, LOW); | |
shiftOut(dataPin, clockPin, MSBFIRST, leds); // using MSBFIRST, 0 is now the leftmost LED | |
digitalWrite(latchPin, HIGH); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment