can't make a fade effect

i'm working on a button pad with a constantly updating code, and i need to be able to fade in and out, and i can't figure out how to do that. It seems so simple in theory, but i can't get it working in real life!

#define DATAOUT 11//MOSI
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK  13//sck
#define SLAVESELECT 10
byte potpin = 0;
byte r1=0;
byte g2=1;
byte g1=2;
byte b2=3;
byte b1=4;
byte r2=5;
byte res1=255;
byte res2=255;
byte res3=255;
byte res4=255;
byte res5=255;
byte res6=255;
byte delay1 = 1;
byte val1 = 0;
char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

void setup()
{
  byte i;
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device
  // SPCR = 01010000
  //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
  //sample on leading edge of clk,system clock/4 (fastest)
  SPCR = (1<<SPE)|(1<<MSTR);
  clr=SPSR;
  clr=SPDR;
  delay(10);
  for (i=0;i<6;i++)
  {
    write_pot(i,255);
  }
}

byte write_pot(int address, int value)
{
  digitalWrite(SLAVESELECT,LOW);
  //2 byte opcode
  spi_transfer(address);
  spi_transfer(value);
  digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
}

void loop()
{
 

  digitalWrite(15, LOW);
  digitalWrite(14, HIGH);
delay(delay1);
  digitalWrite(7, LOW);                    //button 1, 1  and 1, 2:

 delay(delay1);
 digitalWrite(7, HIGH);
 digitalWrite(6, LOW);                // button 2, 1 and 2, 2:

 delay(delay1);
 digitalWrite(5, LOW);
 digitalWrite(6, HIGH);                // button 3, 1 and 3, 2:

 delay(delay1);
 digitalWrite(4, LOW);
 digitalWrite(5, HIGH);                  //button 4, 1 and 4, 2:

 delay(delay1);
 digitalWrite(4, HIGH);

digitalWrite(15, HIGH);
digitalWrite(14, LOW);
     
                     // second relay starts here
           delay(delay1);              
                 
 digitalWrite(7, LOW);            // button 1, 3 and 1, 4:


  delay(delay1);
 digitalWrite(7, HIGH);
 digitalWrite(6, LOW);               //button 2, 3 and 2, 4:

 delay(delay1);
 digitalWrite(5, LOW);
 digitalWrite(6, HIGH);             //button 3, 3 and 3, 4:

 delay(delay1);
 digitalWrite(4, LOW);
 digitalWrite(5, HIGH);              // button 4, 3 and 4, 4:

 delay(delay1);
 digitalWrite(4, HIGH);

 


}

that's my code, and i need to be able to make a value that lower from 255 to 0 and then go back, and all i've been able to do is make it go from 255 to 0, then directly back to 255, which does not make a fading affect, it makes a half fading affect that only makes it get from bright to off gently, then back to beright immedietly!

so here is how you would make a color:

if you want to make button 1 light up, you go to button's 1, 1 and 1,2 section.
then write write_pot(r1, 0) which would turn on whichever button you put in that section.

i need to make it : write_pot(r1, val) and that val needs to gently go from 255 to 0 and gently back.
i'm gussing it can only fade for a quarter second since it updates every 1 milisecond x 255 steps?

somebody please give me a small if statement or a for statement that will open up posibilities :slight_smile:
thanks in advance for any help!!

A general algorithm for bouncing between the limits of a range is:

  1. Set counter to 0.
  2. If counter is at 0, set direction to 1.
  3. If counter is at max, set direction to -1
  4. Add direction to counter.
  5. Do stuff with counter.
  6. Go to 2.

Obviously you can speed up the process with different values of direction. The important thing is that they differ in sign.

HTH!

ok, so what kind if statement do i use?

if statement? for statement?

how do i make them not contrdict itself?

cuz if i do : if (val1 < 255)
{ val1 ++ }
if (val1 > 255)
{ vla-- }

that controdicts itself, becuase it's always going to be less then 55, and if it's not, then value will stop.

get why i'm confused?

more like:

int direction, counter;

void c_init() {
counter=0;
direction=1;
}

int c_iterate() {
counter+=direction
if (counter >= 255) direction=-1;
if (counter <= 0) direction=1;
return counter;
}

for (c_init();:wink:
doStuffWith(c_iterate()); // or whatever works for your program

ok, so what kind if statement do i use?

if statement? for statement?

how do i make them not contrdict itself?

cuz if i do : if (val1 < 255)
{ val1 ++ }
if (val1 > 255)
{ vla-- }

that controdicts itself, becuase it's always going to be less then 55, and if it's not, then value will stop.

get why i'm confused?

That's why I specified to use a separate direction variable. Re-read my algorithm and reply if you're still stuck.

LOLOLOL right after you talked about this, i went to my code and started thinking:

hmmm, maybe if i add a second variable, then i will make the if statement like before, and if the variable is changed, then both parts of the if statement will be true, and it will work.
but i'm betting i just decoded what u said in my mind without knowing it and made it work... except it dident work lol i messed up somewhere...

but thanks for the idea!! it's a really good one, now if i could only get it working for me...

thanks for the code triffid but i'm really confused on how that would work... after you wrote the direction statements, what am i sapposed to put on the bottem part of the code?

sorry, i'm still a bit noobish on c, and any refrence i see just confuses me more, so i'm trying to learn through people :slight_smile:
i'll work on triffid's idea, and ill try to get a working code, and ill post back a little later... thanks for the help

Are you trying to create a fading effect on one LED or digital PIN? If yes, then you can use PWM (AnalogWrite).

i wish it was that simple...

i'm using 2 digital pots controlled through spi, and i constantly refresh the button pad to make an effect of 16 individually controlled lights, when really it's only 1 row changing grounds of the columns fast....