well, i finally got my sparkfun buttons working flawlessly ( almost... ) and i'm ready to simplify my code into something easier to interact with buttons and pots and such...
since i don't know how to make a library or how to simplify my code, i was hoping that somebody who is good at it. If anybody can simplify it, or make into a library, that would make programing this alot easier.
just one note:
i need to connect the buttons from the button pad to the arduino. Yes, i know it's a button pad without buttons for now, but i'll add them when they are easier to interface with the lights...
code:
#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;
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);
}
as you can see where i wrote "button 1, 1 and 1, 2:" i don't actualy have any commands, but the commands would only look like this = "write_pot(r1, 0);" that one will make the first top left button of the specific side light up red. g1 and b1 represent green, and blue. r2 g2 b2 are the second button on the specific half.
That's pretty much it..
except i also don't know how to make the buttons dimm off, then dimm on, and off and on and so fourth... i only can make it dim off, or on, but not both one after the other.
please help, iv'e been working on this for a month!
thanks in advance to any help!