Skip to content

Instantly share code, notes, and snippets.

@emilyhorsman
Created February 17, 2015 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilyhorsman/4596b7c78f8204936403 to your computer and use it in GitHub Desktop.
Save emilyhorsman/4596b7c78f8204936403 to your computer and use it in GitHub Desktop.
/*
* 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