Sawtooth waveform on arduino

Hello,
I am trying to generate a sawtooth waveform using Arduino and DAC 0808. I connected the two checked that the DAC works properly. That is when i make MSB = 1 and rest of the bits =0, Multimeter reads vref/2. I have configured pins 2 to 9 are output pins with 2 being LSB and 9 MSB. I also see that it works fine on serial monitor. Not sure whats wrong with it. Please help.

int val; //int j; //byte state; //int pins[] = {2, 3, 4, 5, 6, 7, 8, 9}; byte mask; void setup() { // put your setup code here, to run once: Serial.begin(9600); for (int i = 2; i < 10; i++) { pinMode(i, OUTPUT); } val = 0; }

void loop() {
// put your main code here, to run repeatedly:

//for (int j=0; j<8; j++)
//byte state = bitRead(val, j);
// Serial.println(state);
int k =2;
for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask
if (val & mask){ // if bitwise AND resolves to true
digitalWrite(k,HIGH); // send 1
Serial.println(“1”);
Serial.println(k);
//delay (2000);
}
else{ //if bitwise and resolves to false
digitalWrite(k,LOW); // send 0
Serial.println(“0”);
Serial.println(k);
//delay (2000);
}
k++;
}
//Serial.println("-------------------");
val++;
if (val > 255)

val = 0;

}