pinMode(pin, OUTPUT);
analogWrite(pin,value);
//stuff
digitalWrite(pin,HIGH);
pinMode(pin, OUTPUT);
analogWrite(pin,value);
//stuff
pinMode(pin, OUTPUT);
digitalWrite(pin,HIGH);
Yes, analogWrite is putting the pin into a different mode, which digitalWrite can't use. I'll work on digitalWrite next week.