Newbie wants a Logical Toggle

So I am trying to do a logical toggle (press button once pin goes high and press button again and pin goes low) The only problem is I am using a Wii chuck and Michael Dreher's code. The Wii Chuck uses the following code to decode the Z Button:

// byte outbuf[5] contains bits for z and c buttons // it also contains the least significant bits for the accelerometer data // so we have to check each bit of byte outbuf[5] if ((outbuf[5] >> 0) & 1) { z_button = 1; } if ((outbuf[5] >> 1) & 1) { c_button = 1; }

if ((outbuf[5] >> 2) & 1) { accel_x_axis += 2; } if ((outbuf[5] >> 3) & 1) { accel_x_axis += 1; }

if ((outbuf[5] >> 4) & 1) { accel_y_axis += 2; } if ((outbuf[5] >> 5) & 1) { accel_y_axis += 1; }

if ((outbuf[5] >> 6) & 1) { accel_z_axis += 2; } if ((outbuf[5] >> 7) & 1) { accel_z_axis += 1; }

All I want to do is toggle pin 12 on/off with a z button press

Here is what I have:

val = z_button;

if ((val == HIGH) && (old_val == LOW)) { state = 1 - state; }

old_val = val;

if (state == 0) { digitalWrite(headlightPin, HIGH); } else { digitalWrite(headlightPin, LOW); }