Created
February 20, 2016 20:41
-
-
Save bassdread/07a7e50dfdc0b4873b90 to your computer and use it in GitHub Desktop.
Basic code to control 8 bit shift register
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
/* | |
Adafruit Arduino - Lesson 4. 8 LEDs and a Shift Register - Brightness | |
*/ | |
int latchPin = 5; | |
int clockPin = 6; | |
int dataPin = 4; | |
int outputEnablePin = 3; | |
byte leds = 0; | |
String inputString = ""; // a string to hold incoming data | |
boolean stringComplete = false; // for serial message complete | |
String location = ""; | |
boolean california = false; | |
boolean uk = false; | |
boolean moscow = false; | |
boolean sydney = false; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(latchPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(outputEnablePin, OUTPUT); | |
setBrightness(255); | |
// set all to red | |
Serial.println("All Red"); | |
bitSet(leds, 0); | |
bitSet(leds, 2); | |
bitSet(leds, 4); | |
bitSet(leds, 6); | |
updateShiftRegister(); | |
// delay(2000); | |
// bitSet(leds, 1); | |
// bitSet(leds, 3); | |
// bitSet(leds, 5); | |
// bitSet(leds, 7); | |
// updateShiftRegister(); | |
} | |
void loop() | |
{ | |
if (stringComplete) { | |
inputString.replace("\n",""); | |
location = inputString; | |
Serial.println(location); | |
changeLEDState(); | |
// clear the string: | |
inputString = ""; | |
stringComplete = false; | |
} | |
delay(500); | |
} | |
void changeLEDState() | |
{ | |
if (location == "california") { | |
// green for ok or true | |
if (california == true) { | |
Serial.println("updating California to down"); | |
bitClear(leds, 1); | |
updateShiftRegister(); | |
bitSet(leds, 0); | |
updateShiftRegister(); | |
california = !california; | |
} else { | |
Serial.println("updating California to up"); | |
bitClear(leds, 0); | |
updateShiftRegister(); | |
bitSet(leds, 1); | |
updateShiftRegister(); | |
california = !california; | |
} | |
} | |
else if (location == "uk") { | |
// green for ok or true | |
if (uk == true) { | |
Serial.println("updating uk to down"); | |
bitClear(leds, 3); | |
updateShiftRegister(); | |
bitSet(leds, 2); | |
updateShiftRegister(); | |
uk = !uk; | |
} else { | |
Serial.println("updating uk to up"); | |
bitClear(leds, 2); | |
updateShiftRegister(); | |
bitSet(leds, 3); | |
updateShiftRegister(); | |
uk = !uk; | |
} | |
} else if (location == "moscow") { | |
// green for ok or true | |
if (moscow == true) { | |
Serial.println("updating moscow to down"); | |
bitClear(leds, 5); | |
updateShiftRegister(); | |
bitSet(leds, 4); | |
updateShiftRegister(); | |
moscow = !moscow; | |
} else { | |
Serial.println("updating moscow to up"); | |
bitClear(leds, 4); | |
updateShiftRegister(); | |
bitSet(leds, 5); | |
updateShiftRegister(); | |
moscow = !moscow; | |
} | |
} else if (location == "sydney") { | |
// green for ok or true | |
if (sydney == true) { | |
Serial.println("updating sydney to down"); | |
bitClear(leds, 7); | |
updateShiftRegister(); | |
bitSet(leds, 6); | |
updateShiftRegister(); | |
sydney = !sydney; | |
} else { | |
Serial.println("updating sydney to up"); | |
bitClear(leds, 6); | |
updateShiftRegister; | |
bitSet(leds, 7); | |
updateShiftRegister(); | |
sydney = !sydney; | |
} | |
} | |
} | |
void serialEvent() { | |
while (Serial.available()) { | |
// get the new byte: | |
char inChar = (char)Serial.read(); | |
// add it to the inputString: | |
inputString += inChar; | |
// if the incoming character is a newline, set a flag | |
// so the main loop can do something about it: | |
if (inChar == '\n') { | |
stringComplete = true; | |
} | |
} | |
} | |
void updateShiftRegister() | |
{ | |
digitalWrite(latchPin, LOW); | |
shiftOut(dataPin, clockPin, LSBFIRST, leds); | |
digitalWrite(latchPin, HIGH); | |
} | |
void setBrightness(byte brightness) // 0 to 255 | |
{ | |
analogWrite(outputEnablePin, 255-brightness); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment