millis and state...

The sketch, Blink without delay is what I'd like to do, but it is for an LED. One pin only.
The state, int ledState = LOW; is either high or low.

I want to use this with a DC motor, but the motor has two pins. One is high and one is low; that's one direction...one is low and one is high; that's the other direction. And when they are both low, that is stopped.

So my question is, how to I refer to the state of the motor when it has two pins. I want to be able to control the time it's on in one direction, the opposite direction, and how long it's off. I want to use millis instead of delay. How do I refer to the two pins of the motor, like the one pin of the LED?

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

In BWOD the state of 1 pin is changed as you say but you could change the state of 2 pins or use the single pin state to drive 2 pins using

digitalWrite(motorPin1, motorState);
digitalWrite(motorPin2, !motorState);

However, driving a motor directly from an Arduino pin is not advisable due to the limited current available. You would be better off using a relay or transistors.

What happens when both pins are high?

Just out of curiosity - you are not thinking of connecting your arduino pins directly to a DC motor, I hope.

If you want a DC motor to chnge directions, the usual solution is a thing called an H-Bridge. The come as a shield which you clip onto the top of your arduino.

The demo Several Things at a Time is an extended example of BWoD and may help your understanding of the concept.

...R

Thanks everyone. UKHeliBob got what I was after.

digitalWrite(motorPin1, motorState);
digitalWrite(motorPin2, !motorState);

I needed to put the ! (NOT) on the state where needed.

I need to think more like a digital IC.

Of course I'm using an H-Bridge...it has become so routine I forgot to mention it. I am new to the Arduino and new to coding, so I'm still banging my head against the wall...but I'm slowly getting it.

This drives a DC motor CW, CCW, and stopped for "interval" length of time at random. It also has a speed control.

If you see anything odd or questionable, let me know.

const int switchPin = 7;    // switch input
const int switch2Pin = 8;    // switch input
const int motor1Pin = 2;    // H-bridge leg 1 (pin 2)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7,)
const int enablePin = 3;    // H-bridge enable pin
int potPin = A0;
int motorSpeed = 0;

int motorState = LOW;

long previousMillis = 0;

long interval = 500;

void setup() {

  Serial.begin(9600);

  // set the switch as an input:
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);


  // set enablePin high so that motor can turn on:
  digitalWrite(enablePin, HIGH);
} 

void loop() {

  unsigned long currentMillis = millis();
  int move = random(0, 3);


  switch (move) {

  case 0:
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;

      digitalWrite(motor1Pin, motorState);
      digitalWrite(motor2Pin, !motorState);
      motorSpeed = analogRead(potPin)/4;
      analogWrite(enablePin, motorSpeed);
      break;
    }
  case 1:
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;

      digitalWrite(motor1Pin, !motorState);
      digitalWrite(motor2Pin, motorState);
      motorSpeed = analogRead(potPin)/4;
      analogWrite(enablePin, motorSpeed);
      break;
    }
  case 2:
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;

      digitalWrite(motor1Pin, motorState);
      digitalWrite(motor2Pin, motorState);
      break;
    }


  } 
}

If you see anything odd or questionable, let me know.

The timing variables should be unsigned long to avoid problems with millis() rollover
Many of your variables are ints when their value will never exceed 255 so they could be bytes
You do not use the switch inputs declared in the program but you could usefully set them as INPUT_PULLUP to avoid the need for external resistors if/when you do use them.

Move the break in the switch/case outside of the enclosing brace so if won't fall thru to the sequential case statement.

Even move the timing checks from each case statement and consolidate to s single timing check before the switch.

All the global variables could be moved into the loop routine by changing most of them to local static variables.

Uncoiled /untested

const uint8_t           switchPin   = 7;    // switch input
const uint8_t           switch2Pin  = 8;    // switch input
const uint8_t           motor1Pin   = 2;    // H-bridge leg 1 (pin 2)
const uint8_t           motor2Pin   = 4;    // H-bridge leg 2 (pin 7,)
const uint8_t           enablePin   = 3;    // H-bridge enable pin
const uint8_t           xpotPin     = A0;

const uint8_t           MOTOR_OFF   = LOW;
const uint8_t           MOTOR_ON    = HIGH;

const unsigned long     INTERVAL    = 500UL;

void loop()
{
    static int              motorSpeed      = 0;
    static int              motorState      = LOW;
    static unsigned long    previousMillis  = 0;

    unsigned long           currentMillis   = millis();
    if ( (currentMillis - previousMillis) > INTERVAL )
    {
        previousMillis  = currentMillis;

        motorSpeed      = analogRead(potPin) / 4;
        int move        = random(0, 3);
        switch ( move )
        {
            case 0:
                // SOME DIRECTION
                digitalWrite(motor1Pin,  motorState);
                digitalWrite(motor2Pin, !motorState);

                analogWrite(enablePin, motorSpeed);

                break;

            case 1:
                // THE OPPOSITE DIRECTION
                digitalWrite(motor1Pin, !motorState);
                digitalWrite(motor2Pin,  motorState);

                analogWrite(enablePin, motorSpeed);

                break;

            case 2:
                // STOPED
                digitalWrite(motor1Pin, motorState);
                digitalWrite(motor2Pin, motorState);

                break;
        } 
    }
}

void setup()
{
    Serial.begin(9600);

    pinMode(switchPin, INPUT);
    pinMode(switch2Pin, INPUT);

    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);


    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, MOTOR_ON);
}