Got myself an Arduino and some other stuff and im having fun playing around with it XD
Eventualy i was playing around with 8 leds and a 74HC595, and made a nice led chaser.
It worked fine but i wanted more, i wanted to have a variable speed of the “chase” based
on the value of a sensor, the only sensor i have at the moment is a LDR so i decided to make the
chase speed based on the amount of light, the lighter it is, the slower the chase.
After about an hour of trial and error it finally worked and i was very happy with it.
it gave me a feeling of satisfaction 8)
But, the code is pretty long and i want to know if there is an easier way to do this, i just can’t seem
to figure it out myself, i’m still very new to all this
Maybe someone can point me into the right direction?
I’ve also attached a fritzing image of the setup
Any help would be appreciated
/*
Led chaser with variating speed depending on LDR values.
The darker it gets, the faster it goes.
8 leds, 74HC595, ldr, resistors and a bunch of wires.
*/
int ldrValue; // Create ldrValue
int ldrDelay; // Create ldrDelay
int clockPin = 12; // SH_CP pin 11 on the 74HC595
int latchPin = 8; // ST_CP pin 12 on the 74HC595
int dataPin = 11; // DS pin 14 on the 74HC595
void setup() {
pinMode(clockPin, OUTPUT); // Set clockPin as output
pinMode(latchPin, OUTPUT); // Set latchPin as output
pinMode(dataPin, OUTPUT); // Set dataPin as output
}
void loop() {
ldrDelay = analogRead(ldrValue); // Put the ldrvalue into the ldrDelay
digitalWrite(latchPin, LOW); // Turn the latch low to accept data
shiftOut(dataPin, clockPin, MSBFIRST, B00000001); // Send the commands to the 74HC595
digitalWrite(latchPin, HIGH); // Turn latch high to tell that the data is done
delay(ldrDelay); // Use the ldrdelay that was just measured as length of delay
ldrDelay = analogRead(ldrValue); // Put a new value into ldrDelay, and so on
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00000010);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00000100);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00001000);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00010000);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00100000);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B01000000);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B10000000);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B01000000);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00100000);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00010000);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00001000);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00000100);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
ldrDelay = analogRead(ldrValue);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B00000010);
digitalWrite(latchPin, HIGH);
delay(ldrDelay);
}