Sketch locks up arduino???

Using an Arduino duemilanove, the following sketch hangs the Arduino after a few cycles until you reset. Hang means no I/O or serial output.

int pwm = 20;
void setup(){
  pinMode(9, OUTPUT);
int getinc(float cur, float target){
  return (abs(cur-target) > 1)? 16: 1;
void setvoltage(float voltage, int ms){
  int mil = millis();
  while((millis()-mil) < ms){
  float v = analogRead(A0) * (float)0.0048828125;
  if(v < voltage && pwm < 255){pwm ++;}//= getinc(v, voltage);}
  if(v > voltage && pwm > 0){pwm --;}//= getinc(v, voltage);}
void loop(){

Why does it hang? It does this even with no hardware attached to the board.

Are you sure the Arduino is hanging? My thinking is it might be your serial port that is locking up due to the constant stream of data you're pumping out. Try blinking the on-board LED (pin 13) inside your loop to see if it's the Arduino or the serial port. Or put a delay after the analogWrite to give the serial stream a chance to "catch up" (shouldn't really be necessary but terminal programs are not all created equal).

-- The Ruggeduino: compatible with Arduino UNO, 24V operation, all I/O's fused and protected

The LED hangs in the on position after a few min. I ran the blink sketch with serial output for 5 min with no hang so there is not wrong with the Arduino or serial connection.

BTW: Thank you for the help. The ruggeduino looks like a good idea. I may buy one if my Arduino finally dies (I already replaced the FTDI chip (with bad soldering)) due to a shorted regulator applied 10v to the 5v line.

void setvoltage(float voltage, unsigned long ms){
unsigned long mil = millis();

Ohhh. I forgot about overflows. Unfortunately I do not have access to the Arduino to test it at the millisecond.