Created
August 4, 2016 14:36
-
-
Save thehig/eba2e32de40eaee6f2ab65e542bfa10b to your computer and use it in GitHub Desktop.
arduino: photocell controlled led strobe
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
// CONST's | |
int DS_pin = 8; | |
int STCP_pin = 9; | |
int SHCP_pin = 10; | |
int NUM_LED = 8; | |
// 8 LED Registers | |
boolean registers[8]; | |
// Direction of LED travel | |
boolean goingUp = true; | |
// Currently lit LED | |
int currentLED = 0; | |
int photocellReading; | |
int photocellPin = 0; | |
int photoDelay = 50; | |
void setup() | |
{ | |
// Configure output pins | |
pinMode(DS_pin,OUTPUT); | |
pinMode(STCP_pin,OUTPUT); | |
pinMode(SHCP_pin,OUTPUT); | |
writereg(); | |
} | |
// Writes the LED registers to the Shift-register | |
void writereg() | |
{ | |
// Prepare to write new data | |
digitalWrite(STCP_pin, LOW); | |
for (int i = 7; i>=0; i--) | |
{ | |
// Prepare for bit | |
digitalWrite(SHCP_pin, LOW); | |
// Write bit | |
digitalWrite(DS_pin, registers[i] ); | |
// Done writing bit | |
digitalWrite(SHCP_pin, HIGH); | |
} | |
// Done writing new data | |
digitalWrite(STCP_pin, HIGH); | |
} | |
void loop() | |
{ | |
for(int i = 0; i < NUM_LED; i++){ | |
// Turn on an LED | |
// Turn off the other LEDs | |
registers[i] = i == currentLED; | |
} | |
writereg(); | |
if(goingUp){ | |
currentLED++; | |
if(currentLED == NUM_LED - 1){ | |
goingUp = false; | |
} | |
} else { | |
currentLED--; | |
if(currentLED == 0){ | |
goingUp = true; | |
} | |
} | |
photocellReading = analogRead(photocellPin); | |
photocellReading = 1023 - photocellReading; | |
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses | |
photoDelay = map(photocellReading, 0, 1023, 0, 255); | |
delay(photoDelay); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/watch?v=bqfPZXEuyuc