making my code simpler

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!

Anybody?

A good first step in simplifying the code will be to use structs/ classes to encapsulate your data items (pins and so forth). An initial approximation might be:

typedef struct _ColourPinType {
int red_pin;
int green_pin;
int blue_pin;
} ColourPinType;

You can then create an array of instances of this type:

ColourPinType pins[] = {
{0, 2, 4}, // first item's pins
{5, 1, 3}, // second item's pins
{0} // sentinel value, marks the end
};

Create structures for the other data items in a similar way. You can then loop over these arrays instead of accessing each element individually.

If you are unfamiliar with these concepts, I recommend getting hold of a good C reference and reading through it.

thanks, i was looking at other arduino codes, and i saw alot of code in setup and in void loop, and noticed once they had all the code setup, all they had to do was just type "write_button(2, blue)" and that would make the code soooooooo much esier, but unfortunitly i dont have the coding expieance to understand their code, and use it in my code, but i will probably do what you said and look at a c refrence, even though it's hard for me sometimes becuase they arent always talking about arduino, their talking about computer programs...

A good first step might be to organize your code into functions, then call the functions from setup() or loop(). This lets you organize simple code without dealing with the complexities of structs or classes (yet).
Have you used functions in your code yet? Or do you need an example to see what I mean?

Oops - scrolling down farther in your code I see that you are familiar with this :-[

yea, a little, but i'm still trying to figure out a way to make calling the digital pot's alot more efficient...

is there a way for me to make calling intigers, and digital pot's esier?

like in the digital pot code, they made it fso instead of writing confusing bytes and bits, they made sending a command like this: Write_pot(2, 255)

how do i make my code do that so for example, i can make button_color(button #, button color)

so ex button_color(12, b);

and to make button 12 white, i'd add button_color(12, r); button_color(12, g);
and to make the button turn off completely, i would use something like button_clear

can anybody tell me a way that could be done?

thanks!

The best advice I can give is to learn a bit more C. The questions you raise can be answered in a very general way, but you are looking for advice on specific functions, and I think a better approach to giving you a fish is to teach you how to fish. Essentially, what you want to do is create a function to which you pass parameters, then apply those parameters within the body of the function.

A good C reference & tutorial will be your best friends here.

ok, ill have to look one up

thanks for the help, i'll post back when or if i get results

quick and dirty...

When you create the function you specify the data type that the function will return. For functions that don't return a value, the type is "void" (you will see a lot of this). If you want actual data returned, for example an integer, you would write a function that looks like this (to steal from one of my favorite webcomics):

int randomValue()
{
int r = 4; //chosen by dice roll, guaranteed random
return r;
}

You call this function in your code like this:

int val = randomValue();

Now when you call this function, the value of r (a strictly local variable, only the function that creates it gets to use it) gets passed to val, a variable with more scope (since it is created in the main loop of the code, it can be seen globally).

If you want to pass a value to the function, you need to add an "argument" to the empty parentheses after the function name when you write it:

int randomValue(int a)
{
int r = 4; //chosen by dice roll, guaranteed random
r = r + a;
r = r / 2;
return r;
}

You call THIS version like this:

int val = 8;
int newVal = randomValue(val);

...and voila, randomValue() will take the 8 you sent it, add 4, divide by 2, and return your processed value to the variable newVal.

For a little more detail on functions, try this link http://www2.its.strath.ac.uk/courses/c/section3_9.html
For a lot more detail on functions, read The C Programming Language by Ritchie and Kernighan (sp?). Functions are the essential building blocks for structuring C code, so practice, practice, practice!

ok, ok that helps, i can probably smoothen out the delays with that