Why does this program only work when I use a Serial.* statement in it?:::SOLVED

Shades of particle/wave duality here. This is my first day so be kind. I couldn’t figure out why this program didn’t work so a friend told me about debugging with some Serial.somethings thrown in. Serial.println is as good as Serial.begin. Just like the electron, if I watch it I seem to alter its behavior. I hate to put a line of code in that I don’t need or understand why I need it. I only need one, not both. If I take both out my little motor doesn’t run.

At the moment the motor makes one revolution and pauses. The pause is just kind of a place holder for a switch that will be used to make it cycle again. I throw that in just in case someone wants to point me to a good reference for the next part. (I may have solved this part by searching on ‘button press’ instead of ‘key press’ which kept giving me keyboard examples)

const int drive_pin = 13;
const int direction_pin = 8;

void setup() {                
  pinMode(direction_pin, OUTPUT);     
  pinMode(drive_pin, OUTPUT);
  digitalWrite(direction_pin, HIGH);
  digitalWrite(drive_pin, LOW);
  Serial.begin(115200);
}

void loop() {

   delay(1000);
   advance();
}  

void advance(){
  int i = 0;
  do  {
    digitalWrite(drive_pin, HIGH);
    delayMicroseconds(100);          
    digitalWrite(drive_pin, LOW); 
    delayMicroseconds(100); 
    i++;       
  }
    while (i < 1600);
    Serial.println(i);
    return;
}

What motor are you using? What type of device are you using to control the motor?

I don’t think this:

while (i<1600);

is what you want to do.

When i is less than 1600, it does nothing. If i is >= 1600 it will hang and go into an infinite loop.

Change drive_pin to some other pin. Pin 13 is 'special' in different ways on different Arduinos.

SurferTim:
What motor are you using? What type of device are you using to control the motor?

Easy driver 3. No revision number, just 3.

I used this motor for now mainly because of the wide support available.

KeithRB:
I don’t think this:

while (i<1600);

is what you want to do.

When i is less than 1600, it does nothing. If i is >= 1600 it will hang and go into an infinite loop.

Or better yet, change it to a for loop.

Since I don't know what he expects that code to do, I don't really know what to suggest. It could be that it is a clever way to hang after 1600 loops.

GoForSmoke:
Change drive_pin to some other pin. Pin 13 is 'special' in different ways on different Arduinos.

I was using that so I could see the LED for some visual feedback at first. I changed it to pin 7 but no joy. I still need a Serial.something to make it work.

You guys are missing the “do”. The OP has it coded a bit odd. This is what it does.

do {
  // motor stuff
  i++;
} while(i < 1600);

Mine doesn’t hang, but i am not driving a motor controller with a motor.

It's been too many years since my college programming classes and they were few. My occupation and hobby has kept me around code but I simply don't write much at all. I'm using the while loop because the for loop didn't work. Obviously that was my fault. I'm not sure why it would hang with out the Serial.something and not hang without it though. Right now the motor makes a revolution, pauses and them makes another. Been doing that for over an hour now.

My led is blinking merrily away without any Serial calls in the sketch at all. No Serial.begin() or Serial.print() calls.

edit: How does your led do? Remove the motor and controller. Does the led blink? I'm using D13 for the test.

outofoptions:
Right now the motor makes a revolution, pauses and them makes another. Been doing that for over an hour now.

Which is what you want, but take out the print and it hangs? That is weird.

If it was me, I’d be checking my wiring.

If you moved the drive pin off 13 then how about substituting a flash on pin 13 for the print line?

I disconnected pin 13 and the driver board power and remarked out the Serial.* stuff and no blink. I wonder if I have a strange board or a corrupted library now. I thought it was my code since this is day one.

So you removed the driver board connections completely? It is no longer connected to the Arduino at all, and the LED doesn't blink without the Serial calls?

The motor driver may have a problem.

Make the led blink in setup().

Thanks surferTim, I sure missed the do/while. In this case a while would have worked just as well.

OK. It looks like the Serial.* is what is helping me 'return' from the function call.

I'm running your original code with the Serial stuff remarked out. The D13 led blinks half bright once a second.

Thanks. Guess I'll just muddle on to the next step for now. I borrowing this stuff until mine arrives. I'm starting to suspect a corrupted library or something.

Try running some known-good examples. If the hardware or IDE is faulty it might show up quick.