changing time/frequency

Hello all,

I have a program with five LEDs arranged in a sequence. I want each one of them to fade on and off in order, starting with the first one in the series and finishing with the last. However, I want the amount of time it takes for each one to fade on and off to be one second. I’ve tried messing around with the T = 1/f equation, I’ve tried researching on the internet, but to no avail.

Anybody have any ideas? Thanks!

Here is my code thus far:

int ledPin[5] = {
3, 5, 6, 9, 10};
int timeCounter = 1;
int i;

void setup() {
Serial.begin(9600);
}

void loop() {
for(i = 0; i <5; i++){
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin*, fadeValue); *

  • // wait for 30 milliseconds to see the dimming effect *
  • delay(20); *
  • }*
  • // fade out from max to min in increments of 5 points:*
  • for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {*
  • // sets the value (range from 0 to 255):*
    _ analogWrite(ledPin*, fadeValue); _
    _
    // wait for 30 milliseconds to see the dimming effect _
    _
    delay(20); _
    _
    }_
    _
    if(i == 5){_
    _
    i = 0;_
    _
    }_
    _
    timeCounter++;_
    _
    Serial.println(timeCounter);_
    _
    if(timeCounter == 6){_
    _
    digitalWrite(3, HIGH); // sets the LED on*_
    * digitalWrite(5, HIGH); // sets the LED on*
    * digitalWrite(6, HIGH); // sets the LED on*
    * digitalWrite(9, HIGH); // sets the LED on*
    * digitalWrite(10, HIGH); // sets the LED on*
    * delay(1000); // waits for a second*
    * digitalWrite(3, LOW); // sets the LED off*
    * digitalWrite(5, LOW); // sets the LED off*
    * digitalWrite(6, LOW); // sets the LED off*
    * digitalWrite(9, LOW); // sets the LED off*
    * digitalWrite(10, LOW); // sets the LED off*
    * delay(1000); // waits for a second*

* timeCounter = 1;*
* }*
* }*
}

How can I change the fade time of each LED to half a minute if I want?

Divide the number of milliseconds in the period by the number of steps in the fade.

I divided 2 by 255 (I figured the period was equal to 2 since it says on the analogWrite() page that the default frequency is roughly 500 Hz. I got 0.0078431373. What do I do with that number? Do I stick it in the delay() function as an argument?

// wait for 30 milliseconds to see the dimming effect delay(20);

If comment and code disagree, the code is allways right - I don't recall who wrote this but still very true ...

How can I change the fade time of each LED to half a minute if I want?

if you want to do 255 steps in 30 seconds = 30.000 milliseconds each delay = 30.000/255 = 117.64 milliseconds. As the analogWrite() takes time too a delay(117) could be too long. If you want to do it more precise you might want to use - http://www.arduino.cc/en/Reference/DelayMicroseconds

The function delay() expects a long as parameter see - http://arduino.cc/en/Reference/Delay

The reference section and the tutorial section have plenty of interesting code to dive into. Time spent there is worth it.

I just notice,

analogWrite(ledPin, fadeValue);

LedPin has no index.

When I try post ledPin(with index in breakes, it doesn't show up on page). Is it how it supposed to work?