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
thanks in advance for any help!!