Go Down

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

JeffCoalfax

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

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



cyclegadget


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

cyclegadget


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


JeffCoalfax

#53
Oct 10, 2012, 03:37 am Last 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

Please bear with me...

Nick Gammon

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?

Go Up