Go Down

Topic: Led chaser with speeds based on LDR value (Read 777 times) previous topic - next topic

refrus

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 :smiley-sad-blue:
Maybe someone can point me into the right direction?


I've also attached a fritzing image of the setup  :D

Any help would be appreciated

Code: [Select]

/*
   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);
}

marco_c

This should do the same thing.

Code: [Select]

/*
   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() {

  for (int i=0; i<8; i++)
  {
  ldrDelay = analogRead(ldrValue);                      // Put the ldrvalue into the ldrDelay
  digitalWrite(latchPin, LOW);                          // Turn the latch low to accept data
  shiftOut(dataPin, clockPin, MSBFIRST, 1<<i);          // 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
  }

  for (int i=8; i>0; --i)
  {
        ldrDelay = analogRead(ldrValue);                      // Put the ldrvalue into the ldrDelay
  digitalWrite(latchPin, LOW);                          // Turn the latch low to accept data
  shiftOut(dataPin, clockPin, MSBFIRST, 1<<i);          // 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
  }

}
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

DVDdoug

refrus,

There a few things that make computers (and microcontrollers) and programming very useful.  As Marco showed you, one of those things is looping (doing the same thing over-and-over).

Back in the computer stone-ages, I used to think it was really funny to walk into a store where they were selling computers, and I would write a little BASIC program with a loop that would just count "forever", from 1 to as high as the computer could count, just by adding 1 every time through the loop.  (With Windows, it's not so easy to write a "little program", and now you need a compiler.)

It's very common to use nested loops (loops inside other loops).

But, you don't have to do exactly the same thing every-time through the loop, or you can sometimes go through a different loop, or skip the loop altogether.  Computers/microcontrollers are also good at conditional branching... Using if-statments to "make decisions".   "If the button is pushed, speed-up the chase-sequence", "if the button is not pushed, slow-down the chase-sequence,"... or do a different chase-pattern, etc., etc.   

Finally, computers are really good at math.  Especially if you need to do the same basic calculation over-and-over (in a loop).  If you need to make a calculation once, you can use a calculator.  But, if you need to do the same calculation a hundred times (with different values) or 10,000 times, you can't beat a computer.

Once you understand these 3 concepts - Looping, conditional branching, and mathematical operations/expressions, you can do almost anything!   ...Well, you can program almost anything that's possible with the platform/hardware you have.     (But, I'm NOT saying computer programming is easy...  In general, it's hard-hard-hard and that's why most professional programmers have a 4-year degree in computer science, engineering, or math.)


refrus

wow, that's alot shorter haha, also seems easier

Thank you both for your help, after work im going to spend some more time on this  XD


Go Up