Go Down

Topic: Fade all and light (Read 1 time) previous topic - next topic

neumanj

Apr 11, 2013, 12:02 pm Last Edit: Apr 11, 2013, 12:42 pm by neumanj Reason: 1
I found in the forum  "Fade all IO by Osamu Iwasaki" http://arduino.cc/forum/index.php?PHPSESSID=25e86fe6b8bcb57c319ec59c6ef370d3&topic=140777.msg1057745#msg1057745 . But I need to only 4 outputs light and other fade all like is original code of "Fade all" at the same time. Can you please help me? I have Arduino Uno and really brand new user  :smiley-eek:

Code: [Select]

// Fade all IO by Osamu Iwasaki
// November 10, 2010

#include <MsTimer2.h>

#define OUT_MIN 0  // First pin
#define OUT_MAX 19  // Last pin (this includes the analog pins)

byte luminance[20];
byte lumi[20] = {};

void setup()
{
int i;

for(i = OUT_MIN; i <= OUT_MAX; i++){
   pinMode(i, OUTPUT);
   digitalWrite(i,LOW);
}


for(i = OUT_MIN; i <= OUT_MAX; i++){
   digitalWrite(i,HIGH);
   delay(150);
}
for(i = OUT_MIN; i <= OUT_MAX; i++){
   digitalWrite(i,LOW);
   //delay(50);
}
delay(1000);

MsTimer2::set(1, int_pwm);
MsTimer2::start();
}


void int_pwm() {

static int f_wait[20];
static int f_max_time[20];
static int f_speed[20];
static int f_ct[20];
static long f_interval[20];

static int ch;

for(ch = OUT_MIN; ch <= OUT_MAX; ch++){
   if(f_wait[ch] == 0){
     f_wait[ch] = f_speed[ch] + 1;

     if( (f_ct[ch] < 255) )
       if(luminance[ch] != 255)
         luminance[ch] ++;

     if(f_ct[ch] == 255){
       if(f_max_time[ch] == 0)
         f_max_time[ch] = 0; // On Max Time
       else
         f_max_time[ch] --;
     }

     if( (f_ct[ch] > 255) && (f_ct[ch] < 511) && (f_max_time[ch] == 0))
       if(luminance[ch] != 0)
         luminance[ch] --;

     if(f_ct[ch] >= 511){
       if(f_interval[ch] == 0){
         f_interval[ch] = 10 * random(100); // fading seed
         f_speed[ch] = random(100); // fading speed
         f_ct[ch] = 0;
       }
       else
         f_interval[ch] --;
     }
     else
       f_ct[ch] ++;
   }
   f_wait[ch] --;
}
}

void fading(){

static byte counter = 0;
static boolean prev_off[20];
int i;

for(i = OUT_MIN; i <= OUT_MAX; i++){
   if(lumi[i] == 0){
     if(prev_off[i] == HIGH){
       digitalWrite(i,LOW);
       prev_off[i] = LOW;
     }
   }
   else
     lumi[i] --;
}

counter --;

if(counter == 0){
   for(i = OUT_MIN; i <= OUT_MAX; i++){
     if(luminance[i]){
       digitalWrite(i,HIGH);
       prev_off[i] = HIGH;
     }
     lumi[i] = luminance[i];
   }
}
}


void loop(){
fading();
}


thx

codlink

It depends on what board you have.  But, I changed the code to NOT light 4 pins.  Those 4 pins you will have to find during testing because I don't know what board you have.

Code: [Select]
// Fade all IO by Osamu Iwasaki
// November 10, 2010

#include <MsTimer2.h>

#define OUT_MIN 4  // First pin
#define OUT_MAX 19  // Last pin (this includes the analog pins)

byte luminance[20];
byte lumi[20] = {};

void setup()
{
int i;

for(i = OUT_MIN; i <= OUT_MAX; i++){
   pinMode(i, OUTPUT);
   digitalWrite(i,LOW);
}


for(i = OUT_MIN; i <= OUT_MAX; i++){
   digitalWrite(i,HIGH);
   delay(150);
}
for(i = OUT_MIN; i <= OUT_MAX; i++){
   digitalWrite(i,LOW);
   //delay(50);
}
delay(1000);

MsTimer2::set(1, int_pwm);
MsTimer2::start();
}


void int_pwm() {

static int f_wait[20];
static int f_max_time[20];
static int f_speed[20];
static int f_ct[20];
static long f_interval[20];

static int ch;

for(ch = OUT_MIN; ch <= OUT_MAX; ch++){
   if(f_wait[ch] == 0){
     f_wait[ch] = f_speed[ch] + 1;

     if( (f_ct[ch] < 255) )
       if(luminance[ch] != 255)
         luminance[ch] ++;

     if(f_ct[ch] == 255){
       if(f_max_time[ch] == 0)
         f_max_time[ch] = 0; // On Max Time
       else
         f_max_time[ch] --;
     }

     if( (f_ct[ch] > 255) && (f_ct[ch] < 511) && (f_max_time[ch] == 0))
       if(luminance[ch] != 0)
         luminance[ch] --;

     if(f_ct[ch] >= 511){
       if(f_interval[ch] == 0){
         f_interval[ch] = 10 * random(100); // fading seed
         f_speed[ch] = random(100); // fading speed
         f_ct[ch] = 0;
       }
       else
         f_interval[ch] --;
     }
     else
       f_ct[ch] ++;
   }
   f_wait[ch] --;
}
}

void fading(){

static byte counter = 0;
static boolean prev_off[20];
int i;

for(i = OUT_MIN; i <= OUT_MAX; i++){
   if(lumi[i] == 0){
     if(prev_off[i] == HIGH){
       digitalWrite(i,LOW);
       prev_off[i] = LOW;
     }
   }
   else
     lumi[i] --;
}

counter --;

if(counter == 0){
   for(i = OUT_MIN; i <= OUT_MAX; i++){
     if(luminance[i]){
       digitalWrite(i,HIGH);
       prev_off[i] = HIGH;
     }
     lumi[i] = luminance[i];
   }
}
}


void loop(){
fading();
}
//LiNK

neumanj

Thank you, but maybe I explained it wrong, but I wanted to make only 4 pins leds shine (HIGH) and rest random fading at the same time, like is original code.
Thanks for any idea...

KirAsh4

See the define OUT_MIN and OUT_MAX variables?  Those correspond to the first pin and last pin that you want the program to control for FADING.  So, by setting the OUT_MIN to the value 4, that means pins 0, 1, 2, and 3 will NOT be fading in fact, they won't do anything because the program won't touch them.  Meaning, you are now free to do whatever you want with those four pins, like turn them HIGH at all times while the rest are controlled by the fading routines.

Keep in mind that the four pins you pick must be either the first four, or the last four.  They can't be any random pin.

neumanj

Super, that I understand, but how write it to the code? Because, how you wrote, they 1-4 pins do nothing :O  I just paste the original code to arduiono and I was happy, but I really don't know nothing about programing. Thanks

KirAsh4

So read up on digitalWrite() then.  Adding that to the beginning of the loop should do the trick.

neumanj

#6
Apr 12, 2013, 01:01 am Last Edit: Apr 12, 2013, 01:09 am by neumanj Reason: 1
I tried it and changed the code to my project -;) Thank you very much for help... My code is here
Code: [Select]
// Fade all IO by Osamu Iwasaki
// November 10, 2010

#include <MsTimer2.h>

#define OUT_MIN 6  // First pin
#define OUT_MAX 10  // Last pin (this includes the analog pins)

int ledPins[] = {0,1,2,3,4};   //  Led HIGH
int fadePin = 5;                 // LED FADE



byte luminance[20];
byte lumi[20] = {};

void setup()
{
  pinMode(ledPins[0],OUTPUT);
  pinMode(ledPins[1],OUTPUT);
  pinMode(ledPins[2],OUTPUT);
  pinMode(ledPins[3],OUTPUT);
  pinMode(ledPins[4],OUTPUT);
  pinMode(fadePin, OUTPUT);
 
int i;

for(i = OUT_MIN; i <= OUT_MAX; i++){
   pinMode(i, OUTPUT);
   digitalWrite(i,LOW);
}


for(i = OUT_MIN; i <= OUT_MAX; i++){
   digitalWrite(i,HIGH);
   delay(150);
}
for(i = OUT_MIN; i <= OUT_MAX; i++){
   digitalWrite(i,LOW);
   //delay(50);
}
delay(1000);

MsTimer2::set(1, int_pwm);
MsTimer2::start();
}


void int_pwm() {

static int f_wait[20];
static int f_max_time[20];
static int f_speed[20];
static int f_ct[20];
static long f_interval[20];

static int ch;

for(ch = OUT_MIN; ch <= OUT_MAX; ch++){
   if(f_wait[ch] == 0){
     f_wait[ch] = f_speed[ch] + 1;

     if( (f_ct[ch] < 255) )
       if(luminance[ch] != 255)
         luminance[ch] ++;

     if(f_ct[ch] == 255){
       if(f_max_time[ch] == 0)
         f_max_time[ch] = 0; // On Max Time
       else
         f_max_time[ch] --;
     }

     if( (f_ct[ch] > 255) && (f_ct[ch] < 511) && (f_max_time[ch] == 0))
       if(luminance[ch] != 0)
         luminance[ch] --;

     if(f_ct[ch] >= 511){
       if(f_interval[ch] == 0){
         f_interval[ch] = 10 * random(100); // fading seed
         f_speed[ch] = random(100); // fading speed
         f_ct[ch] = 0;
       }
       else
         f_interval[ch] --;
     }
     else
       f_ct[ch] ++;
   }
   f_wait[ch] --;
}
}

void fading(){

static byte counter = 0;
static boolean prev_off[20];
int i;

for(i = OUT_MIN; i <= OUT_MAX; i++){
   if(lumi[i] == 0){
     if(prev_off[i] == HIGH){
       digitalWrite(i,LOW);
       prev_off[i] = LOW;
     }
   }
   else
     lumi[i] --;
}

counter --;

if(counter == 0){
   for(i = OUT_MIN; i <= OUT_MAX; i++){
     if(luminance[i]){
       digitalWrite(i,HIGH);
       prev_off[i] = HIGH;
     }
     lumi[i] = luminance[i];
   }
}
}
void fade()
{
// fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
    // sets the value (range from 0 to 255):
    analogWrite(fadePin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(10);                           
  }

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
    // sets the value (range from 0 to 255):
    analogWrite(fadePin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(10);                           
  }
}
void loop()
{
  fading();
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[3], HIGH);
  digitalWrite(ledPins[4], HIGH);
fade();


}



But right now, the random fade does not work.... becouse I want the led pin5 to extra fading a 4 pins light a rest random, but random fading not working... can you help
me where is mistake in the code, i think the mistake is in the void loop, but where?

thx


KirAsh4

1. Physical pin 5 is not a PWM pin, it will never fade.
2. You are blocking the whole loop with your fade() routine, meaning nothing will run till your fade() is completed, and you're blocking it with every single cycle.  I don't know what you're trying to achieve with that, but it's causing everything else to not run properly.

codlink

If you want the 4 LEDs to stay on while the Arduino is on, then you can put the digitalWrite in setup(). (code attached)  Let us know how this helps.

Don't forget to put resistors in-line with your LEDs!!

Code: [Select]
// Fade all IO by Osamu Iwasaki
// November 10, 2010

#include <MsTimer2.h>

#define OUT_MIN 6  // First pin
#define OUT_MAX 10  // Last pin (this includes the analog pins)

int ledPins[] = {0,1,2,3,4};   //  Led HIGH
int fadePin = 5;                 // LED FADE



byte luminance[20];
byte lumi[20] = {};

void setup()
{
  pinMode(ledPins[0],OUTPUT);
  pinMode(ledPins[1],OUTPUT);
  pinMode(ledPins[2],OUTPUT);
  pinMode(ledPins[3],OUTPUT);
  pinMode(ledPins[4],OUTPUT);
  pinMode(fadePin, OUTPUT);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[3], HIGH);
  digitalWrite(ledPins[4], HIGH);
 
int i;

for(i = OUT_MIN; i <= OUT_MAX; i++){
   pinMode(i, OUTPUT);
   digitalWrite(i,LOW);
}


for(i = OUT_MIN; i <= OUT_MAX; i++){
   digitalWrite(i,HIGH);
   delay(150);
}
for(i = OUT_MIN; i <= OUT_MAX; i++){
   digitalWrite(i,LOW);
   //delay(50);
}
delay(1000);

MsTimer2::set(1, int_pwm);
MsTimer2::start();
}


void int_pwm() {

static int f_wait[20];
static int f_max_time[20];
static int f_speed[20];
static int f_ct[20];
static long f_interval[20];

static int ch;

for(ch = OUT_MIN; ch <= OUT_MAX; ch++){
   if(f_wait[ch] == 0){
     f_wait[ch] = f_speed[ch] + 1;

     if( (f_ct[ch] < 255) )
       if(luminance[ch] != 255)
         luminance[ch] ++;

     if(f_ct[ch] == 255){
       if(f_max_time[ch] == 0)
         f_max_time[ch] = 0; // On Max Time
       else
         f_max_time[ch] --;
     }

     if( (f_ct[ch] > 255) && (f_ct[ch] < 511) && (f_max_time[ch] == 0))
       if(luminance[ch] != 0)
         luminance[ch] --;

     if(f_ct[ch] >= 511){
       if(f_interval[ch] == 0){
         f_interval[ch] = 10 * random(100); // fading seed
         f_speed[ch] = random(100); // fading speed
         f_ct[ch] = 0;
       }
       else
         f_interval[ch] --;
     }
     else
       f_ct[ch] ++;
   }
   f_wait[ch] --;
}
}

void fading(){

static byte counter = 0;
static boolean prev_off[20];
int i;

for(i = OUT_MIN; i <= OUT_MAX; i++){
   if(lumi[i] == 0){
     if(prev_off[i] == HIGH){
       digitalWrite(i,LOW);
       prev_off[i] = LOW;
     }
   }
   else
     lumi[i] --;
}

counter --;

if(counter == 0){
   for(i = OUT_MIN; i <= OUT_MAX; i++){
     if(luminance[i]){
       digitalWrite(i,HIGH);
       prev_off[i] = HIGH;
     }
     lumi[i] = luminance[i];
   }
}
}
void fade()
{
// fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
    // sets the value (range from 0 to 255):
    analogWrite(fadePin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(10);                           
  }

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
    // sets the value (range from 0 to 255):
    analogWrite(fadePin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(10);                           
  }
}
void loop()
{
  fading();
  fade();
}
//LiNK

neumanj

My project is starry heaven for my little child using optical fibers and fiber connect each LED. There are nearly 30 volumes of optical fibers. About 12 of them will just be on (HIGH) and the rest will fade. But one optical fiber bundle I wanted extra FADE (pin 5). Lasts code doesn 't work - video. http://youtu.be/c4_CdgeW1vU

thx

KirAsh4

How is your separate fade pin any different from all the others?  Why can't you have the first four lit all the time and the rest fading?  I don't see why you want one as a "separate" fade, and the others also.  You're over-complicating things.

Go Up