cycling 4 LEDs at varrying speeds

understood, I changed it to 2 but I need to understand how to get 3,4 and 5 in there as well. so that I can have them blink with this code. Everything that I try gives me an error.

What errror?

Maybe you're trying to teach me 5th grade algebra when I need to learn 1st grade addition. What's simple to you is not simple to me. and I'm sure if roles were reversed I'd bend over backwards to help you.

I think that where we are having difficulty is that we have no idea that you need help with 1st grade addition. So, we assume that its 5th grade algebra that you need help with, since that is what you are asking questions about.

If we answer the question(s) in ways that you don't understand, you bear some responsibility to let us know that. Berating people who are trying to help you isn't the best response.

If we know that your difficulty isn't with 5th grade algebra, but is more basic than that, we can back up and start with more basic answers.

If, on the other hand, we assume that, though the question was about 5th grade algebra, you don't even understand 1st grade addition, you'd be even more insulted, and rightly so.

Mr. Gammon,

now that looks like some information I can use. when I get to my laptop I will try to understand those codes that you have on your site. it seems like it's laid out in a way I can understand but when I start trying to apply it I will probably have lots if questions.

Thanks

PaulS:

Maybe you're trying to teach me 5th grade algebra when I need to learn 1st grade addition. What's simple to you is not simple to me. and I'm sure if roles were reversed I'd bend over backwards to help you.

I think that where we are having difficulty is that we have no idea that you need help with 1st grade addition. So, we assume that its 5th grade algebra that you need help with, since that is what you are asking questions about.

If we answer the question(s) in ways that you don't understand, you bear some responsibility to let us know that. Berating people who are trying to help you isn't the best response.

If we know that your difficulty isn't with 5th grade algebra, but is more basic than that, we can back up and start with more basic answers.

If, on the other hand, we assume that, though the question was about 5th grade algebra, you don't even understand 1st grade addition, you'd be even more insulted, and rightly so.

can you please help me now?

can you please help me now?

Sure. With what?

come on, really?

come on, really?

Yes, really.

I'm beginning to suspect that the problem is that you don't know how to formulate your requirements in a form that someone else could implement them. If I'm mistaken, then please define them. We can't really help you implement them without seeing them.

If I'm not, then do your best to state what you want to accomplish, and we'll help you come up with a set of requirements that can be implemented, and to implement them, too.

you don't know how to formulate your requirements in a form that someone else could implement them

You're probably right because I'm not sure what this means.

Tell me if I'm way off when I say, "I want to take this current code that cycles 4 LEDs at a rate that increases as each cycle is completed by what ever value that's inputted and have it increase speed not by cycle completion but by time. For example: it starts at 700 and every second that passes it reduces by a factor of 20, in 5 seconds, it will reduce to 600 and so on, until it reaches a target value which in this case is 20.

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

Now I know you want me to intruduce millis() and remove delay()

So here's what I know about delay(), when it's read it stops all other function until the task is complete. And I know that Millis allows other functions to take place at the same the same time other tasks are being completed. Which will be perfect to allow a counter to run while the LEDs cycle.

Let me also throw out a disclamer that this isn't for any school project, nor is it a project that is going to financially benifit me. It is just me trying to have fun with the Arduino. and as you can tell at this point i'm not having much fun. but dispite the rhetoric I'm learning quite a bit.

Jeff

The 'blink without delay' example sketch demonstrates the technique which is fundamental to solving this problem.

Have you tried running that? If not, give it a go and play with the source code and make sure you understand how it works.

Making the blink frequency and duty cycle vary over time is as simple as using variables to control the blinking instead of constants, and changing those variables however you want. If you want the frequency to reduce over time, then put in code that runs at regular intervals and reduces the blink interval. (And refer back to the 'blink without delay' to see how to run code at regular intervals.)

If you want to apply this logic to multiple LEDs independently then you need to replicate the code and data used to blink an LED. This can be as simple as copy/paste and rename the variables to make everything unique, and there's nothing stopping you from giving that a go with two LEDs to prove the concept. However, a better way to do similar things to multiple outputs is to put your data in an array and have your code loop through the array doing the same thing for each entry. In this scheme, instead of having a constant or variable holding the pin number for the LED, you'd have an array of pin numbers; instead of having a variable holding the last 'blink' time for the LED you'd have an array of 'last blink times', one per LED; if the frequency is variable then you'd have an array holding the current blink interval for each LED. And so on with any other data you introduce to control your LEDs.

JeffCoalfax:
Tell me if I'm way off when I say, "I want to take this current code that cycles 4 LEDs at a rate that increases as each cycle is completed by what ever value that's inputted and have it increase speed not by cycle completion but by time. For example: it starts at 700 and every second that passes it reduces by a factor of 20, in 5 seconds, it will reduce to 600 and so on, until it reaches a target value which in this case is 20.

Can you clarify please?

4 LEDs. What do you mean by "cycle"? One and then the next, and so on? So only one is one at once? Or all flashing together? Or one on, then two on, then three on, and then four on?

... and have it increase speed not by cycle completion but by time ...

What does that mean?

... it starts at 700 ...

700 what? You mean 700 Hz? You wouldn't spot anything much over 25 Hz cycle.

... as each cycle is completed by what ever value that's inputted ...

Inputted how? Keyboard? Knob? Serial port?

Can you clarify please?

4 LEDs. What do you mean by "cycle"? One and then the next, and so on? So only one is one at once? Or all flashing together? Or one on, then two on, then three on, and then four on?

One right after the other: STEP1 led1 on, STEP2 led1 off (same time) led2 on, STEP3 led2 off (same time) led3 on, STEP4 led3 off (same time) led4 on, STEP5 led4 off (same time) led1 on etc.

... and have it increase speed not by cycle completion but by time ...

What does that mean?

The current code increases the speed of the LEDs after every cycle (I define a cycle as when all 4 LEDs have lit up once) by 20 milliseconds

// lower the delay if timer >= 20

from 700 milliseconds

int timer = 700;

to the targeted 20 milliseconds

if (timer >= 20) {
      timer = timer - 1;
    }

This code works well for the slower cycles but once the cycles start to speed up the rate at which 20 is subtracted becomes really quick it cascades to 20 rather quickly.

700 what? You mean 700 Hz? You wouldn't spot anything much over 25 Hz cycle.

I believe it's set in ms.

Inputted how? Keyboard? Knob? Serial port?

imbedded in the code

if (timer >= 20) {
      timer = timer - 1;
    }

Take a look at this blink with out delay code that is modified using a few bits of your code and the "timer" variable from your sketch.
Note that this sketch is using LED 13, you can change or add LEDs as you like.

Each time you make a change, and need help with the next step post your code and ask more questions. Each effort by you will be rewarded with help.

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.
 
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long timer = 700;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > timer) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // lower the delay if timer >= 20       
    if (timer >= 20) {
      timer = timer - 1;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

If I may suggest, you should consider a book from Simon Monk, a regular poster on this forum. I personally have is book "Programming Arduino, Getting Started With Sketches"

Here is a link to Amazon with his book list. Amazon.com: Simon Monk: books, biography, latest update

Yeah, it looks like I'm going to have to take a few steps back and get on the same page as everyone. I'm no where near where everyone expects me to be. All the advice that is given to me flies way over my head. I'm totally missing what apparently is staring me right in the face with this "blink without delay" sketch

I know that everyone that reads this is asking themselves, "Is he really that dense?" well the answer is emphatically yes

Please bear with me...

The code above seems to do what you asked for. This 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);    
  }
}

I tried it out, the LEDs flash in sequence, gradually (very gradually) getting faster.

Is that not what you wanted?

Thank you sir, It sounds like it's exactly what I'm looking for. I will have to load it this evening to play with it.

Jeff

HI ALL

in this case the "delay":

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

will not block the cpu? to make it working with some kind of interrupt!

KR

edit:
I attend to use BlinkWithoutDelay, but controling the HIGH / LOW in different times!!!

There is no explicit interrupt in the blink without delay example.
The implicit one, for the timer overflow, is hidden and handled for you.

Hi AWOL

I understand what you mean.. but if i got some button interrupt.. Will not use delay because it will block my buttons...

AWOL:
There is no explicit interrupt in the blink without delay example.
The implicit one, for the timer overflow, is hidden and handled for you.

is possibel to use some other kind of "delay" in millis so will run.? like...

const int ledPin =  13; 

// Variables will change:
int ledState = 0; 
long previousMillis = 0; 
int timer = 700;                       //!!!! 

long interval = 1000;

void setup() {
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   

    if (ledState == 0)
      ledState = 150;
    else
      ledState = 0;
      delay(timer);                   // !!!!

    digitalWrite(ledPin, ledState);
  }
}

I´m stuck at this point :! tks in advance

KJMM:
is possibel to use some other kind of "delay" in millis so will run.? like...

You can change the value of interval if you want to blink at different rates. You can even change it within the code given a specific condition.

Perhaps it would be better if you were to define what you are trying to accomplish, rather than how you are trying to accomplish it.