Pages: [1] 2 3 4   Go Down
Author Topic: cycling 4 LEDs at varrying speeds  (Read 6423 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

I just purchased my first arduino uno and I'm eager to learn as much as possible.

I just put 4 LEDs on pin 2-5 and they share a ground on the board. What I want to do is have these 4 LED's chase each other at varying speeds that can be written in the code.

ie. the LEDs chase each other at the rate of 50 cycles per minute and then gradually go from 50 to 2000 cycles per minute at the rate of 10 cycles a second, and then back down to any variable lets say 500 cycles per minute.

Here is the code that I have right now, It's a varition of this code as you can see I can change the speed manually by changing int timer = but I want to have it all programmed and watch it do it automatically:



int timer = 700;           // The higher the number, the slower the timing.

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 2; thisPin < 6; thisPin++)  {
    pinMode(thisPin, OUTPUT);     
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 6; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH); 
    delay(timer);                 
    // turn the pin off:
    digitalWrite(thisPin, LOW);   
  }
 
}

Logged

Offline Offline
Newbie
*
Karma: 2
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great to see new people getting interested in microcontrollers smiley.
First, you have resistors in there too right?

Anyway.
You can change 'timer' in the loop as you like. For example:

Code:
void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 6; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH); 
    delay(timer);
           
    // lower the delay if timer >= 20       
    if (timer >= 20) {
      timer = timer - 10;
    }

    // turn the pin off:
    digitalWrite(thisPin, LOW);   
  }
}

This will lower 'timer' by 10 each time the loop runs until 'timer' is 10 (milliseconds).
Have fun!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50157
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but I want to have it all programmed and watch it do it automatically
You can programmatically change the value of the variable called timer.

The variable called timer has nothing to do with any timers, so the name is a poor choice for a variable.

dwell or interval or waitSomeTime would all be better choices.

Loop index variables are typically one letter names, like i, j, k, etc.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome thanks for the help. I played with changing some of the values and I get the lowering the delay by 10 every cycle (sorry for the lack of lingo) and it works well until the cycle times speed up then it cascades to max speed rather quickly.

Is there an easy way to have it increase based on time and not cycles? for instance: have it increase by 10 every second instead of cycle?

Oh, and yes I have resistors in line of my LEDs.
Here's what I have so far:

Code:
int timer = 700;           // The higher the number, the slower the timing.

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 2; thisPin < 6; thisPin++)  {
    pinMode(thisPin, OUTPUT);     
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 6; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH); 
    delay(timer);                 
    // lower the delay if timer >= 20       
    if (timer >= 20) {
      timer = timer - 1;
    }

    // turn the pin off:
    digitalWrite(thisPin, LOW);   
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but I want to have it all programmed and watch it do it automatically
You can programmatically change the value of the variable called timer.

The variable called timer has nothing to do with any timers, so the name is a poor choice for a variable.

dwell or interval or waitSomeTime would all be better choices.

Loop index variables are typically one letter names, like i, j, k, etc.

I sort of getting what you're saying but will changing timer to dwell, interval do anything different to the code?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50157
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
will changing timer to dwell, interval do anything different to the code?
You mean besides making it easier to understand? Isn't that sufficient reason by itself?

Quote
Is there an easy way to have it increase based on time and not cycles? for instance: have it increase by 10 every second instead of cycle?
The millis() function bears looking into. Keep track of when you last made a change, and what time it is now. Make a change only if sufficient time has elapsed.

While you're at it, get rid of delay() altogether.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks paul, do you have a link to millis()

and when I removed delay() the LEDs all lit up and stayed on.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50157
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
do you have a link to millis()
Sure: http://arduino.cc/en/Reference/Millis
(I'll bet you could have found that...)

Quote
and when I removed delay() the LEDs all lit up and stayed on.
Getting rid of delay() requires more than the delete key. Look at the blink without delay example. Slowly blinking LEDs with nary a delay() in sight.

That is what you need to rewrite your code to do.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you mean I have to read and understand code that makes no sense to me? smiley Ok I'll try... But be ready for lots of questions!!!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, just as I expected, it makes zero sense to me. Let me say that I don't know how to write or read code, I learn best by taking something and changing values to see what I changed, then things me more sense.

how would I apply millis() to my current project.

I'm going to need you guys to hold my hand before I feel comfortable with doing things myself.
Logged

Offline Offline
Edison Member
*
Karma: 36
Posts: leet
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the blink without delay example.

Note that you can use multiple timing variables.  In your situation, you need one "delay" (actually a "time reference" rather than a delay) for the chase speed, and another time reference for the speed-change rate (i.e . 10 per second).

It would be a good idea to review the entire Language Reference to get an idea of what you can do.   You don't have to memorize the whole thing.     But, try to understand if-statements & loops.    If-statements (and other conditional logic) are the way that computers "make decisions".   Along with loops (doing things over& over, perhaps changing variable-values each time through the loop) these are the two most important things in programming...  They are the things that make programming useful.


Quote
Let me say that I don't know how to write or read code, I learn best by taking something and changing values to see what I changed, then things me more sense.
That's not a bad approach...   When you are modifying code, it's a good idea to change a little at a time.    When something goes-wrong, it's not always easy to figure-out what's wrong and it you change one or two things at at time, at least you know where the problem is.

The BEST way to learn programming is to take a class.   The 2nd best way would be to get a book.   But, I don't know if you can find a class or book that teaches beginning programming concepts as well as Arduino programming.    So, unless you want to work through a general-purpose C/C++ programming book (unrelated to the Arduino), you might just have to struggle-through.

The same goes for writing code from scratch.   Professional programmers don't write the whole program before trying it out...  They write and tests one thing at a time.    Beginners should try to write and test one or two lines at a time, except where you need to add more to make the program comple & run. 

Just FIY - Programming is NOT easy, and it's probably different than anything you've done before.   People who are good at math & logic tend to make good programmers.    Just as an indication of how difficult programming can be, professional programmers make mistakes (create bugs) every day!    If doctors or bridge designers made as many mistakes they would be fired!  smiley-grin    (Most of these bugs are fixed before anyone sees them.)

I've been programmed (part-time for work and as a hobby for many years in several different programming languages...  I'm still not an expert! 
« Last Edit: October 08, 2012, 01:45:31 pm by DVDdoug » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26482
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The blink without delay example has about four key lines.
Understand those lines (play with the code, add extra cycles or different on/off times), and you're well on your way to doing what you want.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess I have no clue what I'm doing, nothing makes sense. Thanks for trying to help.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ok, just as I expected, it makes zero sense to me.

It will take longer than eight minutes to understand.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess what i'm looking for is someone to take what I have... or scrap it all together and help me with cycling 4 leds from 30 cycles per minute to 2000 cycles per minute at the rate of 1 cycle per second.


Lets use what I have as example and move from there. All these links are great but I would much rather understand it at a pace that I can see results with what I have.

I know it requires less work for me and more work for the arduino community, but the "more work" part for you is easy because you understand it, I don't. if it were more work for me I would have to devote more time than I have to doing this
Thanks Guys

Jeff

« Last Edit: October 08, 2012, 01:35:37 pm by JeffCoalfax » Logged

Pages: [1] 2 3 4   Go Up
Jump to: