millis (different intervals) in a switch state

Below is the complete sketch I'm using. It works fine....the top half (JOYSTICK CONTROL) works as needed with a joystick to control horizontal and vertical. Another button turns on the circle motor.

The 2nd (else) random section (RANDOM CONTROL) is where I need help. I have used the millis rather than the delay, but I'd like a different interval for each motor. I don't know how to incorporate separate intervals with millis in a switch case.

I'd like something a beginner can grasp that is more basic Arduino coding. :slight_smile:

Thanks for your help.

steve

/*vericalBot 3 motors 2 with limit switches. Vertical, Horizontal and Circles.
 by steve craig for Steve's Machine Empire. 2015.
 */

//VERTICAL MOTOR
const int switchPin = 7;    // switch input
const int switch2Pin = 8;    // switch input
const int motor1Pin = 2;    // H-bridge WHITE
const int motor2Pin = 4;    // H-bridge GRAY
const int enablePin = 3;    // H-bridge BLACK
int potPin = A0;
int motorSpeed = 0;

//HORIZONTAL
const int switchPinB = 5;    // switch input
const int switch2PinB = 6;    // switch input
const int motor1PinB = 12;    // H-bridge ORANGE
const int motor2PinB = 13;    // H-bridge RED
const int enablePinB = 11;    // H-bridge BROWN
int potPinB = A1;
int motorSpeedB = 0;


//CIRCLES
const int switchPinC = 10;    // switch input
const int motor1PinC = 18;    // H-bridge YELLOW
const int motor2PinC = 19;    // H-bridge GREEN
const int enablePinC = 9;    // H-bridge BLUE
int potPinC = A2;
int motorSpeedC = 0;

const int randomSwitch = 17;
int motorState = LOW;
const unsigned long     interval    = 250;
long previousMillis = 0;

void setup() {
  // VERTICAL
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);

//HORIZONTAL
  pinMode(switchPinB, INPUT);
  pinMode(switch2PinB, INPUT);


//CIRCLES
  pinMode(switchPinC, INPUT);
  pinMode(randomSwitch, INPUT);



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

  pinMode(motor1PinB, OUTPUT);
  pinMode(motor2PinB, OUTPUT);
  pinMode(enablePinB, OUTPUT);

  pinMode(motor1PinC, OUTPUT);
  pinMode(motor2PinC, OUTPUT);
  pinMode(enablePinC, OUTPUT);


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

void loop() {

//JOYSTICK CONTROL OF VERT AND HORIZ. BUTTON ON-OFF FOR CIRCLES.
  if (digitalRead(randomSwitch) == HIGH) {  


    if (digitalRead(switchPin) == HIGH) {  
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      motorSpeed = analogRead(potPin) / 4;
      analogWrite(enablePin, motorSpeed);
      delay (10);

    }
    // if the switch is high, motor will turn on one direction:
    else if (digitalRead(switch2Pin) == HIGH) {
      digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      motorSpeed      = analogRead(potPin) / 4;
      analogWrite(enablePin, motorSpeed);
      delay (10);

    }
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay (10);

    }

    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPinB) == HIGH) {
      digitalWrite(motor1PinB, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2PinB, HIGH);  // set leg 2 of the H-bridge high
      motorSpeedB      = analogRead(potPinB) / 4;
      analogWrite(enablePinB, motorSpeedB);
      delay (10);

    }
    // if the switch is high, motor will turn on one direction:
    else if (digitalRead(switch2PinB) == HIGH) {
      digitalWrite(motor1PinB, HIGH);   // set leg 1 of the H-bridge low
      digitalWrite(motor2PinB, LOW);  // set leg 2 of the H-bridge high
      motorSpeedB      = analogRead(potPinB) / 4;
      analogWrite(enablePinB, motorSpeedB);
      delay (10);

    }
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1PinB, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2PinB, LOW);   // set leg 2 of the H-bridge low
      delay (10);

    }

    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPinC) == HIGH) {


      digitalWrite(motor1PinC, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2PinC, HIGH);  // set leg 2 of the H-bridge high
      motorSpeedC      = analogRead(potPinC) / 4;
      analogWrite(enablePinC, motorSpeedC);
      delay (10);

    }
    else {
      digitalWrite(motor1PinC, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2PinC, LOW);   // set leg 2 of the H-bridge low
      delay (10);

    }
  }

//RANDOM CONTROL.
  else {



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

      motorSpeed      = analogRead(potPin) / 4;
      motorSpeedB      = analogRead(potPinB) / 4;
      motorSpeedC      = analogRead(potPinB) / 4;


      int move        = random(0, 9);

      switch (move) {

      case 0:

        digitalWrite(motor1Pin, motorState);
        digitalWrite(motor2Pin, !motorState);

        analogWrite(enablePin, motorSpeed);
        break;

      case 1:

        digitalWrite(motor1Pin, !motorState);
        digitalWrite(motor2Pin, motorState);

        analogWrite(enablePin, motorSpeed);
        break;

      case 2:

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

      case 3:

        digitalWrite(motor1PinB, motorState);
        digitalWrite(motor2PinB, !motorState);

        analogWrite(enablePinB, motorSpeedB);
        break;

      case 4:

        digitalWrite(motor1PinB, !motorState);
        digitalWrite(motor2PinB, motorState);

        analogWrite(enablePinB, motorSpeedB);
        break;

      case 5:

        digitalWrite(motor1PinB, motorState);
        digitalWrite(motor2PinB, motorState);
        break;

      case 6:

        digitalWrite(motor1PinC, motorState);
        digitalWrite(motor2PinC, !motorState);

        analogWrite(enablePinC, motorSpeedC);
        break;

      case 7:

        digitalWrite(motor1PinC, !motorState);
        digitalWrite(motor2PinC, motorState);

        analogWrite(enablePinC, motorSpeedC);
        break;

      case 8:

        digitalWrite(motor1PinC, motorState);
        digitalWrite(motor2PinC, motorState);
        break;
      }


    } 
  }
}

stevex:
The 2nd (else) random section (RANDOM CONTROL) is where I need help. I have used the millis rather than the delay, but I'd like a different interval for each motor. I don't know how to incorporate separate intervals with millis in a switch case.

     unsigned long           currentMillis   = millis();

if ( (currentMillis - previousMillis) > interval)
    {
      previousMillis  = currentMillis;

Then you need a separate set of time variables for each motor. Something like this for the second motor:

    unsigned long  currentMillis2 = millis();
    if ( (currentMillis2 - previousMillis2) > interval2)
    {
      previousMillis2  = currentMillis2;

if you are randomly moving it then maybe you could randomly change interval at the same time so it will move randomly for a random period of time

previousMillis must be "unsigned long" as well.

Yes, Henry_Best, but if I have 9 cases, 3 motors in each direction plus stop, where do I put the

"unsigned long currentMillis2 = millis();
if ( (currentMillis2 - previousMillis2) > interval2)
{
previousMillis2 = currentMillis2;

? In other words, how does case 3,4,5 know it belongs to motor 2 and interval2?

stevex:
where do I put the

Use arrays

const byte numMotors = 3;
unsigned long prevMillis[numMotors]
unsigned long interval[numMotors]

then you can use a FOR loop

for (byte n = 0; n < numMotors; n ++) {
   if (currentMillis - prevMillis[n] >= interval[n]) {
      // whatever
   }
}

You could be even more sophisticated and use a struct to hold the data for each motor and then create an array of structs. But I will leave you to research that yourself.

…R

Robin2:
You could be even more sophisticated and use a struct to hold the data for each motor and then create an array of structs. But I will leave you to research that yourself.

…R

yes! like this example

not sure if something as simple as this would work

/*vericalBot 3 motors 2 with limit switches. Vertical, Horizontal and Circles.
 by steve craig for Steve's Machine Empire. 2015.
 */

//VERTICAL MOTOR
const int switchPin = 7;    // switch input
const int switch2Pin = 8;    // switch input
const int motor1Pin = 2;    // H-bridge WHITE
const int motor2Pin = 4;    // H-bridge GRAY
const int enablePin = 3;    // H-bridge BLACK
int potPin = A0;
int motorSpeed = 0;

//HORIZONTAL
const int switchPinB = 5;    // switch input
const int switch2PinB = 6;    // switch input
const int motor1PinB = 12;    // H-bridge ORANGE
const int motor2PinB = 13;    // H-bridge RED
const int enablePinB = 11;    // H-bridge BROWN
int potPinB = A1;
int motorSpeedB = 0;
int move = 0;
int onetime = 0;

//CIRCLES
const int switchPinC = 10;    // switch input
const int motor1PinC = 18;    // H-bridge YELLOW
const int motor2PinC = 19;    // H-bridge GREEN
const int enablePinC = 9;    // H-bridge BLUE
int potPinC = A2;
int motorSpeedC = 0;

const int randomSwitch = 17;
int motorState = LOW;
unsigned long interval    = 250;
unsigned long previousMillis = 0;

void setup() {
  // VERTICAL
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);

  //HORIZONTAL
  pinMode(switchPinB, INPUT);
  pinMode(switch2PinB, INPUT);


  //CIRCLES
  pinMode(switchPinC, INPUT);
  pinMode(randomSwitch, INPUT);



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

  pinMode(motor1PinB, OUTPUT);
  pinMode(motor2PinB, OUTPUT);
  pinMode(enablePinB, OUTPUT);

  pinMode(motor1PinC, OUTPUT);
  pinMode(motor2PinC, OUTPUT);
  pinMode(enablePinC, OUTPUT);


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

void loop() {

  //JOYSTICK CONTROL OF VERT AND HORIZ. BUTTON ON-OFF FOR CIRCLES.
  if (digitalRead(randomSwitch) == HIGH) {


    if (digitalRead(switchPin) == HIGH) {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      motorSpeed = analogRead(potPin) / 4;
      analogWrite(enablePin, motorSpeed);
      delay (10);

    }
    // if the switch is high, motor will turn on one direction:
    else if (digitalRead(switch2Pin) == HIGH) {
      digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      motorSpeed      = analogRead(potPin) / 4;
      analogWrite(enablePin, motorSpeed);
      delay (10);

    }
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay (10);

    }

    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPinB) == HIGH) {
      digitalWrite(motor1PinB, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2PinB, HIGH);  // set leg 2 of the H-bridge high
      motorSpeedB      = analogRead(potPinB) / 4;
      analogWrite(enablePinB, motorSpeedB);
      delay (10);

    }
    // if the switch is high, motor will turn on one direction:
    else if (digitalRead(switch2PinB) == HIGH) {
      digitalWrite(motor1PinB, HIGH);   // set leg 1 of the H-bridge low
      digitalWrite(motor2PinB, LOW);  // set leg 2 of the H-bridge high
      motorSpeedB      = analogRead(potPinB) / 4;
      analogWrite(enablePinB, motorSpeedB);
      delay (10);

    }
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1PinB, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2PinB, LOW);   // set leg 2 of the H-bridge low
      delay (10);

    }

    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPinC) == HIGH) {


      digitalWrite(motor1PinC, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2PinC, HIGH);  // set leg 2 of the H-bridge high
      motorSpeedC      = analogRead(potPinC) / 4;
      analogWrite(enablePinC, motorSpeedC);
      delay (10);

    }
    else {
      digitalWrite(motor1PinC, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2PinC, LOW);   // set leg 2 of the H-bridge low
      delay (10);

    }
  }

  //RANDOM CONTROL.
  else {
    unsigned long currentMillis   = millis();

    if (onetime == 0) {
      move = random(0, 9);

      switch (move) {

        case 0:
          interval = 1000;
          break;

        case 1:
          interval = 2000;
          break;

        case 2:
          interval = 3000;
          break;

        case 3:
          interval = 1500;
          break;

        case 4:
          interval = 1000;
          break;

        case 5:
          interval = 2500;
          break;

        case 6:
          interval = 3500;
          break;

        case 7:
          interval = 100;
          break;

        case 8:
          interval = 10000;
          break;
      }
      previousMillis  = currentMillis;
      onetime = 1;
    }

    if ( (currentMillis - previousMillis) > interval)
    {
      onetime = 0;

      motorSpeed      = analogRead(potPin) / 4;
      motorSpeedB      = analogRead(potPinB) / 4;
      motorSpeedC      = analogRead(potPinB) / 4;

      switch (move) {

        case 0:

          digitalWrite(motor1Pin, motorState);
          digitalWrite(motor2Pin, !motorState);

          analogWrite(enablePin, motorSpeed);
          break;

        case 1:

          digitalWrite(motor1Pin, !motorState);
          digitalWrite(motor2Pin, motorState);

          analogWrite(enablePin, motorSpeed);
          break;

        case 2:

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

        case 3:

          digitalWrite(motor1PinB, motorState);
          digitalWrite(motor2PinB, !motorState);

          analogWrite(enablePinB, motorSpeedB);
          break;

        case 4:

          digitalWrite(motor1PinB, !motorState);
          digitalWrite(motor2PinB, motorState);

          analogWrite(enablePinB, motorSpeedB);
          break;

        case 5:

          digitalWrite(motor1PinB, motorState);
          digitalWrite(motor2PinB, motorState);
          break;

        case 6:

          digitalWrite(motor1PinC, motorState);
          digitalWrite(motor2PinC, !motorState);

          analogWrite(enablePinC, motorSpeedC);
          break;

        case 7:

          digitalWrite(motor1PinC, !motorState);
          digitalWrite(motor2PinC, motorState);

          analogWrite(enablePinC, motorSpeedC);
          break;

        case 8:

          digitalWrite(motor1PinC, motorState);
          digitalWrite(motor2PinC, motorState);
          break;
      }
    }
  }
}

It doesn't appear to work...changes I make to intervals and the motors don't seem to be following that...
thanks, tho...

struct is a bit beyond me...I think I'm about 30 tutorials behind on that...

stevex:
struct is a bit beyond me...I think I'm about 30 tutorials behind on that...

Then use arrays and learn about structs later.

...R

stevex:
It doesn't appear to work...changes I make to intervals and the motors don't seem to be following that...
thanks, tho...

strange I added serial prints to see what I had done wrong and it seems to work correctly. I did notice that its not that random. You may want to use randomseed on a spare analog pin.

think I spotted the error now. I had to add a else to the millis compare as you want to use it until the timer is done

if you get a chance test this sketch using serial print.

/*vericalBot 3 motors 2 with limit switches. Vertical, Horizontal and Circles.
 by steve craig for Steve's Machine Empire. 2015.
 */

//VERTICAL MOTOR
const int switchPin = 7;    // switch input
const int switch2Pin = 8;    // switch input
const int motor1Pin = 2;    // H-bridge WHITE
const int motor2Pin = 4;    // H-bridge GRAY
const int enablePin = 3;    // H-bridge BLACK
int potPin = A0;
int motorSpeed = 0;

//HORIZONTAL
const int switchPinB = 5;    // switch input
const int switch2PinB = 6;    // switch input
const int motor1PinB = 12;    // H-bridge ORANGE
const int motor2PinB = 13;    // H-bridge RED
const int enablePinB = 11;    // H-bridge BROWN
int potPinB = A1;
int motorSpeedB = 0;
int move = 0;
int onetime = 0;

//CIRCLES
const int switchPinC = 10;    // switch input
const int motor1PinC = 18;    // H-bridge YELLOW
const int motor2PinC = 19;    // H-bridge GREEN
const int enablePinC = 9;    // H-bridge BLUE
int potPinC = A2;
int motorSpeedC = 0;

const int randomSwitch = 17;
int motorState = LOW;
unsigned long interval    = 250;
unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);
  // VERTICAL
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);

  //HORIZONTAL
  pinMode(switchPinB, INPUT);
  pinMode(switch2PinB, INPUT);


  //CIRCLES
  pinMode(switchPinC, INPUT);
  pinMode(randomSwitch, INPUT);



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

  pinMode(motor1PinB, OUTPUT);
  pinMode(motor2PinB, OUTPUT);
  pinMode(enablePinB, OUTPUT);

  pinMode(motor1PinC, OUTPUT);
  pinMode(motor2PinC, OUTPUT);
  pinMode(enablePinC, OUTPUT);


  // set enablePin high so that motorS can turn on:
  digitalWrite(enablePin, HIGH);
  digitalWrite(enablePinB, HIGH);
  digitalWrite(enablePinC, HIGH);
  randomSeed (analogRead (3));
}

void loop() {

  //JOYSTICK CONTROL OF VERT AND HORIZ. BUTTON ON-OFF FOR CIRCLES.
  if (digitalRead(randomSwitch) == HIGH) {


    if (digitalRead(switchPin) == HIGH) {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      motorSpeed = analogRead(potPin) / 4;
      analogWrite(enablePin, motorSpeed);
      delay (10);

    }
    // if the switch is high, motor will turn on one direction:
    else if (digitalRead(switch2Pin) == HIGH) {
      digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      motorSpeed      = analogRead(potPin) / 4;
      analogWrite(enablePin, motorSpeed);
      delay (10);

    }
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay (10);

    }

    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPinB) == HIGH) {
      digitalWrite(motor1PinB, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2PinB, HIGH);  // set leg 2 of the H-bridge high
      motorSpeedB      = analogRead(potPinB) / 4;
      analogWrite(enablePinB, motorSpeedB);
      delay (10);

    }
    // if the switch is high, motor will turn on one direction:
    else if (digitalRead(switch2PinB) == HIGH) {
      digitalWrite(motor1PinB, HIGH);   // set leg 1 of the H-bridge low
      digitalWrite(motor2PinB, LOW);  // set leg 2 of the H-bridge high
      motorSpeedB      = analogRead(potPinB) / 4;
      analogWrite(enablePinB, motorSpeedB);
      delay (10);

    }
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1PinB, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2PinB, LOW);   // set leg 2 of the H-bridge low
      delay (10);

    }

    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPinC) == HIGH) {


      digitalWrite(motor1PinC, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2PinC, HIGH);  // set leg 2 of the H-bridge high
      motorSpeedC      = analogRead(potPinC) / 4;
      analogWrite(enablePinC, motorSpeedC);
      delay (10);

    }
    else {
      digitalWrite(motor1PinC, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2PinC, LOW);   // set leg 2 of the H-bridge low
      delay (10);

    }
  }

  //RANDOM CONTROL.
  else {
    unsigned long currentMillis   = millis();

    if (onetime == 0) {

      move = random(0, 9);
      Serial.print("random results ");
      Serial.println(move);
      switch (move) {

        case 0:
          interval = 1000;
          break;

        case 1:
          interval = 2000;
          break;

        case 2:
          interval = 3000;
          break;

        case 3:
          interval = 1500;
          break;

        case 4:
          interval = 1000;
          break;

        case 5:
          interval = 2500;
          break;

        case 6:
          interval = 3500;
          break;

        case 7:
          interval = 100;
          break;

        case 8:
          interval = 10000;
          break;
      }
      previousMillis  = currentMillis;
      Serial.print("interval ");
      Serial.println (interval);
      onetime = 1;
    }

    if ( (currentMillis - previousMillis) > interval)
    {
      Serial.println("time done");
      onetime = 0;
    }
    else {

      motorSpeed      = analogRead(potPin) / 4;
      motorSpeedB      = analogRead(potPinB) / 4;
      motorSpeedC      = analogRead(potPinB) / 4;

      switch (move) {

        case 0:

          digitalWrite(motor1Pin, motorState);
          digitalWrite(motor2Pin, !motorState);

          analogWrite(enablePin, motorSpeed);
          break;

        case 1:

          digitalWrite(motor1Pin, !motorState);
          digitalWrite(motor2Pin, motorState);

          analogWrite(enablePin, motorSpeed);
          break;

        case 2:

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

        case 3:

          digitalWrite(motor1PinB, motorState);
          digitalWrite(motor2PinB, !motorState);

          analogWrite(enablePinB, motorSpeedB);
          break;

        case 4:

          digitalWrite(motor1PinB, !motorState);
          digitalWrite(motor2PinB, motorState);

          analogWrite(enablePinB, motorSpeedB);
          
          break;

        case 5:

          digitalWrite(motor1PinB, motorState);
          digitalWrite(motor2PinB, motorState);
          break;

        case 6:

          digitalWrite(motor1PinC, motorState);
          digitalWrite(motor2PinC, !motorState);

          analogWrite(enablePinC, motorSpeedC);
          break;

        case 7:

          digitalWrite(motor1PinC, !motorState);
          digitalWrite(motor2PinC, motorState);

          analogWrite(enablePinC, motorSpeedC);
          Serial.println("motor doing its thing 100ms");
          break;

        case 8:

          digitalWrite(motor1PinC, motorState);
          digitalWrite(motor2PinC, motorState);
       break;
      }
    }
  }
}

I think it would be better if you also added code so it can not repeat the same random number.