Question about "Blink Without Delay" sketch

In the “BlinkWithoutDelay” sketch, the following is declared in the loop:

unsigned long currentMillis = millis();

Does that have to be in loop() or can it just be placed at the top with all my other usual declarations?

The reason I ask is I am experimenting with a very very simple motor controller using two NPN transistors and two PNP transistors. Since I don’t want to chance turning the wrong ones on at the same time I just placed the on/off for the proper ones in a function.

/* Simple motor controller.  Two NPN transistors and two PNP transistors connected to a small hobby motor.
VERY IMPORTANT that the proper ON/OFF sequence is observed or else there will be a direct path from VCC to GND.

VCC----------I---------------------I
             I                     I
     TranA  NPN             TranB NPN
             I                     I
             >--------Motor--------<
             I                     I
     TranC  PNP             TranD PNP
             I                     I
GND----------I---------------------I

Thought it best to write the control as a function. */

const int TranA = 0;
const int TranB = 1;
const int TranC = 2;
const int TranD = 3;
unsigned long previousMillis = 0;        // will store last time LED was updated
long TimeOn = 250;           // Microseconds that motor is on
unsigned long currentMillis = millis();

void setup() {
pinMode(TranA, OUTPUT);
pinMode(TranB, OUTPUT);
pinMode(TranC, OUTPUT);
pinMode(TranD, OUTPUT);

}

void loop() {
  MotorClockwise();
  delay(2500);
  MotorCounterClockwise();
  delay(2500);
}

void MotorClockwise() {
  digitalWrite(TranA, HIGH);
  digitalWrite(TranD, HIGH);
if(currentMillis - previousMillis > TimeOn) {
  previousMillis = currentMillis;
  digitalWrite(TranA, LOW);
  digitalWrite(TranD, LOW);
  delay(10);
}

}

void MotorCounterClockwise() {
  digitalWrite(TranB, HIGH);
  digitalWrite(TranC, HIGH);
if(currentMillis - previousMillis > TimeOn) {
  previousMillis = currentMillis;
  digitalWrite(TranB, LOW);
  digitalWrite(TranC, LOW);
  delay(10);
}
}

I realize since I can’t put a blocking diode in the circuit in the usual way, couldn’t I just use a 1N4148 coming off the control pins in series to the transistor’s base?

EDIT: Maybe this would be better, but you get the idea:

Does that have to be in loop() or can it just be placed at the top with all my other usual declarations?

The assignment is so you get the time now, but the declaration can be at global level.

In my opinion, putting it as an automatic variable is more appropriate since the value is only valid within loop() after the time has been returned by millis() - it is not something that needs to be persisted between successive calls to loop(), and arguably it isn't sensible for it to be persisted since 'current time' is inherently a transient value. The only reason I can see for making it global would be if it needed to be used in multiple places in the code (i.e. not just within loop() itself).

I realize since I can't put a blocking diode in the circuit in the usual way,

You put them like this ( fourth schematic down )
http://www.thebox.myzen.co.uk/Workshop/Motors_2.html

couldn't I just use a 1N4148 coming off the control pins in series to the transistor's base?

No.

Grumpy_Mike:

I realize since I can't put a blocking diode in the circuit in the usual way,

You put them like this ( fourth schematic down )
Motors 2

couldn't I just use a 1N4148 coming off the control pins in series to the transistor's base?

No.

THANK YOU! That helped tremendously. Digikey doesn't have the LM18293 but I found this instead:

Looks like it would be fine for some small hobby motors? Thanks again!