help with arrays and procedures

I'm trying to write a procedure that reads a value from an array, and depending on its value does certain things. It is as follows

// arduino output pins
#define SHDN 51
#define CS 49
#define CLOCK  45
#define DATAIN  43
#define DATAOUT 47
#define CHN1 48
#define CHN2 44
#define CHN3 46
#define CHN4 50

// 2uS of a clock period ( sample rate = 30.3 KHz)


#define HALF_CLOCK_PERIOD 1
uint8_t j=0;
void setup()
{
  pinMode(DATAIN, OUTPUT);
  pinMode(DATAOUT, INPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(CS, OUTPUT);
  pinMode(SHDN, OUTPUT);
  digitalWrite (DATAIN, LOW);
  digitalWrite (CLOCK, LOW);
  digitalWrite (CS, LOW);
  digitalWrite (SHDN, LOW);
   Serial.begin(9600);
   int address[8]={0,0,0,0,0,0,0,1};
}
void writeValue( int address[8], uint8_t value){

  digitalWrite (CS, LOW);
  digitalWrite (SHDN, HIGH);
  digitalWrite (CLOCK, LOW);
  //send the 8 bit address array
for(int i=7; i>=0;i--){
    if (address[i]==1){
      digitalWrite(DATAIN, HIGH);
    
    }
    else if (address[i]==0){
       digitalWrite (DATAIN, LOW);
       
    }
    if(address[i]!=1||0){
    Serial.print("Unrecognized. Only 1 and 0 recognized\n");
    break;
  }
  
  delayMicroseconds(HALF_CLOCK_PERIOD);
  digitalWrite (CLOCK, HIGH);
  delayMicroseconds(HALF_CLOCK_PERIOD);
  digitalWrite (CLOCK, LOW);
}
  
// send 8 bit sample data
 for(int i=7;i>=0;i--){
   digitalWrite(DATAIN,((value&(1<<i)))>>i);
   delayMicroseconds(HALF_CLOCK_PERIOD);
  digitalWrite (CLOCK, HIGH);
  delayMicroseconds(HALF_CLOCK_PERIOD);
  digitalWrite (CLOCK, LOW);
 }
 // latch enable DAC output is set
 digitalWrite(DATAIN,LOW);
 digitalWrite(CLOCK,LOW);
 digitalWrite(CS,HIGH);
 delayMicroseconds(HALF_CLOCK_PERIOD);
 digitalWrite(CS,LOW);
}
void loop ()
{
  int address[8]={0,0,0,0,0,0,0,1};
  //a 0 to 5V triangular waveform
  //for(j=0;j<255;j+=5){
    writeValue(address,255); //error: invalid conversion from 'uint8_t' to 'int*'
    //writeValue(address,0);
  //}
//  for(j=0;j<255;j+=5){
//    writeValue(address,255-j);
//  }
}

What I want it to do is to take the ith value of the array, and if it is a 1 turn it on, if it is a zero turn the thing off, if by chance it is a 2 print an error message. and do this starting at 7, and going down to 0.

however instead of in this case only having 1 pin turned on, because there is only a single 1, all the pins are turned on, and I cannot figure out why.

Thsi:

int address[8]={0,0,0,0,0,0,0,1};

at the end of setup isn't a very useful thing to do.
You create an array "address" mere microseconds before it goes out of scope and is lost forever.

  if(address[i]!=1||0){

Is incorrect. 1 | 0 is always 1.

When using an OR in an if statement, both sides of the OR need to represent complete tests.

if(address[i]!=1 || address[i]!=0)

However, I think you want AND in there, instead. If the value is not 0 AND the value is not 1, the value is invalid.

if(address[i]!=1 && address[i]!=0)

This could be handled differently. By making address unsigned, the possibility of negative numbers is eliminated. Then, you only need to test that address is greater than 2 to see that it is invalid.

But, since you have already tested the two good conditions, the else clause would be invoked only if the previous two ifs did not evaluate to true:

if(address[i] == 0)
  // Do the 0 thing
else if(address[i] == 1)
  // Do the 1 thing
else
  // address[i] is neither 0 or 1

Or, you could use a switch statement:

switch(address[i])
{
   case 0:
     // Do the 0 thing
     break;
   case 1:
     // Do the 1 thing
     break;
   default:
     // address[i] is neither 0 or 1
     break;
}

[edit]I see that neither of us addressed your question about why all the pins are turned on. I don't see anything wrong with the code. Therefore, there are two possibilities.

One is that the data being passed to the pin (that is connected to a shift register) is not what you thought was being passed to the function. Add some Serial.print and Serial.println statements to confirm what is passed in to the function.

The other is that the shift register is not wired correctly, or not being clocked or latch correctly.[/edit]

...or you could eliminate all posibility of error, and use binary :wink:

...or you could eliminate all posibility of error, and use binary

There are 10 kinds of people in the world. Those that understand binary, and those that don't.

thanks, I twigged it was the or statement and have sorted it now to

 if((test!=1)&&(test!=0)){
    Serial.println(only 1 and 0 recognized invalid input );

It seems to be working, also binary is the next step, we go one step at a time otherwise i go crazy!