Go Down

### Topic: cycling 4 LEDs at varrying speeds (Read 12322 times)previous topic - next topic

#### JeffCoalfax

#45
##### Oct 09, 2012, 03:06 pm
come on,  really?

#### PaulS

#46
##### Oct 09, 2012, 03:18 pm
Quote
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.

#### JeffCoalfax

#47
##### Oct 09, 2012, 07:53 pm
Quote
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.

Code: [Select]
`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

#### PeterH

#48
##### Oct 09, 2012, 08:11 pm
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.
I only provide help via the forum - please do not contact me for private consultancy.

#### Nick Gammon

#49
##### Oct 09, 2012, 11:05 pm

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.

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?

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

What does that mean?

Quote
... it starts at 700  ...

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

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

Inputted how? Keyboard? Knob? Serial port?
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### JeffCoalfax

#50
##### Oct 10, 2012, 02:22 am
Quote

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.

Quote
Quote
... 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
Code: [Select]
`// lower the delay if timer >= 20 `
from 700 milliseconds
Code: [Select]
`int timer = 700;   `
to the targeted 20 milliseconds
Code: [Select]
`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.

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

I believe it's set in ms.

Quote
Inputted how? Keyboard? Knob? Serial port?

imbedded in the code
Code: [Select]
`if (timer >= 20) {      timer = timer - 1;    }`

#51
##### Oct 10, 2012, 03:00 am

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.

Code: [Select]
`/* 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 LEDlong 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);  }}`
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

#52
##### Oct 10, 2012, 03:23 am

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. http://www.amazon.com/Simon-Monk/e/B003VOT2DI

General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

#### JeffCoalfax

#53
##### Oct 10, 2012, 03:37 amLast Edit: Oct 10, 2012, 03:39 am by JeffCoalfax Reason: 1
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

#### Nick Gammon

#54
##### Oct 10, 2012, 04:39 am
The code above seems to do what you asked for. This code:

Code: [Select]
`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?
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### JeffCoalfax

#55
##### Oct 10, 2012, 02:32 pm
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

#### KJMM

#56
##### Oct 23, 2012, 10:26 amLast Edit: Oct 23, 2012, 10:28 am by KJMM Reason: 1
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!!!

#### AWOL

#57
##### Oct 23, 2012, 10:32 am
There is no explicit interrupt in the blink without delay example.
The implicit one, for the timer overflow, is hidden and handled for you.
"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.

#### KJMM

#58
##### Oct 23, 2012, 11:27 am
Hi AWOL

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

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...

Code: [Select]
`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

#### Arrch

#59
##### Oct 23, 2012, 05:10 pm

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.

Go Up

Please enter a valid email to subscribe