Motor Control Via. Limit Switch

Hello everyone!

I'm working on a robot which has a head that spins on a track, controlled by a Dc motor.

What I have is working perfectly, but I'm trying to add a feature where when NONE of the actions are being preformed I can have the motor spin until a limit switch is pressed. (where I have it set to depress when the head is centered)

So far everything I have tried has failed or cut out other functions.

What Im looking for is something that says, "when no other motor movement is happening; Spin until limit switch is pressed, then Stop" but NOT have it effect the rest of the sketch.

the tricky part for me, is I have 2 buttons on a remote that spin the head and stop when not pressed ie: "else { brake(motor1, motor2);
}" at the end.
And so far everything I have tried has resulted in cutting out the above "brake" the the buttons cause it to just spin and spin.

Connections and commands are:

#define centerSwitch 29  //Dome Switch
pinMode(centerSwitch, INPUT); //dome centering limit switch.

motor1.drive(100); // spins motor right
brake(motor1, motor2); // Stops motor

I'll Post the rest of the code in another post as its to long.

BubbleHockey:
I'll Post the rest of the code in another post as its to long.

Just add the code as one attachment per file, it's okay. Don't split it up to post inline.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>
#include <SparkFun_TB6612.h>

const unsigned long eventInterval = 60000; // 90000= 1.5 min change value to change chatter time
unsigned long previousTime = 0;

#define recvLed 22  // Led Showing recived activity 
#define sideVent 23  // Lights for Both Side Vents
#define octoWhite 25  // Octogon Lights WHITE
#define octoBlu 27  // Octogon Lights BLUE
#define centerSwitch 29  //Dome Switch
#define AIN1 24
#define BIN1 28
#define AIN2 26
#define BIN2 30
#define PWMA 5
#define PWMB 6
#define STBY 9

SoftwareSerial mySerial(10, 11); // RX, TX
DFPlayerMini_Fast myMP3;
RF24 radio(7, 8); // CE, CSN

int randomInt;
int funtionpin[9] = { 34, 35, 36, 37, 38, 39, 40, 41, 42 }; //array for action pins buttons 11-18
int pinCount = 9;
const int offsetA = 1;
const int offsetB = 1;
const byte addresses[][6] = {"00001", "00002"};
int buttonState[7];
int randomMove;

Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);

void setup() 
{
  pinMode(22, OUTPUT); //recvLed Led shows recept of transmittion.
  pinMode(recvLed, OUTPUT); //set recvLed
  pinMode(sideVent, OUTPUT);  // Side Vents Leds
  pinMode(octoWhite, OUTPUT); // Octogon White Leds
  pinMode(octoBlu, OUTPUT);   // Octogon Blue Leds
  pinMode(centerSwitch, INPUT); //dome centering limit switch.
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {  // 4 loop setting pins 34-42 as output
    pinMode(funtionpin[thisPin], OUTPUT); 
}
  radio.begin();
  radio.openWritingPipe(addresses[1]); // 00002
  radio.openReadingPipe(1, addresses[0]); // 00001
  radio.setPALevel(RF24_PA_MIN);

  int16_t numSdTracks();
  int16_t numFolders();
  void playFolder(uint8_t folderNum, uint8_t trackNum);  // (folder= 02 Digits Track= 3 Digits)
  
  Serial.begin(115200);
  mySerial.begin(9600);

  myMP3.begin(mySerial);
  
  myMP3.volume(24);
  delay(20);
  //Flash Ocotgons on startup
  myMP3.playFolder(2, 26);
  digitalWrite(octoWhite, HIGH);
  digitalWrite(sideVent, HIGH);
  delay(500);
  digitalWrite(octoWhite, LOW);
  digitalWrite(sideVent, LOW);
  delay(500);
  digitalWrite(octoWhite, HIGH);
  digitalWrite(sideVent, HIGH);
  delay(500);
  digitalWrite(octoWhite, LOW);
  digitalWrite(sideVent, LOW);
  delay(500);
  digitalWrite(octoWhite, HIGH);
  digitalWrite(sideVent, HIGH);
  delay(500);
  digitalWrite(octoWhite, LOW);
  digitalWrite(sideVent, LOW);
  delay(500);
  digitalWrite(octoBlu, HIGH);
  digitalWrite(sideVent, HIGH);
  delay(20);
}
void loop() 
{
  delay(5);
  randomMove = random(0,10);
unsigned long currentTime = millis();

  if ( currentTime - previousTime >= eventInterval) { // delay event for R2s "chatter" sounds folder 01

  randomInt = random(1, 144);  //change second number for number of tracks in folder 01
  myMP3.playFolder(1, randomInt); //Plays random from folder 01
  if (randomMove>=5){
    motor1.drive(100,100);
    delay(300);
    motor1.drive(-100,100);
    delay(300);
  }
  else if (randomMove<=4){
    motor1.drive(-100,200);
    delay(300);
    motor1.drive(100,200);
    delay(300);
  }
  previousTime = currentTime;
  }
  
  radio.startListening();
  buttonState[0] = analogRead(A2);    //analog button1
  buttonState[1] = analogRead(A3);    //analog buttons 2
  buttonState[2] = analogRead(A4);    //Joystick y
  buttonState[3] = analogRead(A5);    //Joystick x
  buttonState[4] = digitalRead(3);      //motor button right
  buttonState[5] = digitalRead(5);      //motor button left
  buttonState[6] = digitalRead(2);      //Joystick button
  
  if ( radio.available()) {
    while (radio.available()) {
      radio.read(&buttonState, sizeof(buttonState));
    }}
    //button#1:
    if (buttonState[0]>= 1013 && buttonState[0]<= 1015) {  // Sound Button#1
      digitalWrite(recvLed, HIGH);
      myMP3.playFolder(2, 1);  // Change for Different sound (folder, Track)
    }
    //Button #2:
  else if (buttonState[0]>=612 && buttonState[0]<=614){  // Sound Button#2
    digitalWrite(recvLed, HIGH);
    myMP3.playFolder(2, 2);
    }
  //Button #3:
  else if (buttonState[0]>=993  && buttonState[0]<=995){ // Sound Button#3
    digitalWrite(recvLed, HIGH);
    myMP3.playFolder(2, 18);
    }
  //Button #4:
  else if (buttonState[0]>=770  && buttonState[0]<=771){  // Sound Button#4
    digitalWrite(recvLed, HIGH);
    myMP3.playFolder(2, 4);
     }
  //Button #5:
  else if (buttonState[0]>=974  && buttonState[0]<=975){  // Sound Button#5
    digitalWrite(recvLed, HIGH);
    myMP3.playFolder(2, 5);
    }
  //Button #6:
  else if (buttonState[0]>=805  && buttonState[0]<=807){  // Sound Button#6
    digitalWrite(recvLed, HIGH);
    myMP3.playFolder(2, 6);
    }
  //Button #7:
  else if (buttonState[0]>=947  && buttonState[0]<=948){  // Sound Button#7
    digitalWrite(recvLed, HIGH);
    myMP3.playFolder(2, 7);
    }
  //Button #8:
  else if (buttonState[0]>=841  && buttonState[0]<=843){  // Sound Button#8
    digitalWrite(recvLed, HIGH);
    myMP3.playFolder(2, 8);
    }
  //Button #9:
  else if (buttonState[0]>=931  && buttonState[0]<=932){  // Sound Button#9
    digitalWrite(recvLed, HIGH);
    myMP3.playFolder(2, 9);
    }
  //Button #10:
  else if (buttonState[0]>=870  && buttonState[0]<=872){  // Sound Button#10
    digitalWrite(recvLed, HIGH);
    myMP3.playFolder(2, 25);
  }
  //Button #11:
  else if (buttonState[1]>=1011  && buttonState[1]<=1012){  // Action Button#11
    digitalWrite(recvLed, HIGH);
      digitalWrite(funtionpin[0], HIGH); // Pin 34 HIGH
      delay(10);
      digitalWrite(funtionpin[0], LOW); // Pin 34 LOW 
  }
  //Button #12
  else if (buttonState[1]>=802  && buttonState[1]<=803){  // Action Button#11
    digitalWrite(recvLed, HIGH);
      digitalWrite(funtionpin[1], HIGH); // Pin 35 HIGH
      delay(10);
      digitalWrite(funtionpin[1], LOW); // Pin 35 LOW 
  }
  //Button #13
  else if (buttonState[1]>=992  && buttonState[1]<=993){  // Action Button#11
    digitalWrite(recvLed, HIGH);
      digitalWrite(funtionpin[2], HIGH); // Pin 36 HIGH
      delay(10);
      digitalWrite(funtionpin[2], LOW); // Pin 36 LOW 
  }
  //Button #14
  else if (buttonState[1]>=837  && buttonState[1]<=838){  // Action Button#11
    digitalWrite(recvLed, HIGH);
      digitalWrite(funtionpin[3], HIGH); // Pin 37 HIGH
      delay(10);
      digitalWrite(funtionpin[3], LOW); // Pin 37 LOW 
  }
  //Button #15
  else if (buttonState[1]>=972  && buttonState[1]<=973){  // Action Button#11
    digitalWrite(recvLed, HIGH);
      digitalWrite(funtionpin[4], HIGH); // Pin 38 HIGH
      delay(10);
      digitalWrite(funtionpin[4], LOW); // Pin 38 LOW 
  }
  //Button #16
  else if (buttonState[1]>=866  && buttonState[1]<=867){  // Action Button#11
    digitalWrite(recvLed, HIGH);
      digitalWrite(funtionpin[5], HIGH); // Pin 39 HIGH
      delay(10);
      digitalWrite(funtionpin[5], LOW); // Pin 39 LOW 
  }
  //Button #17
  else if (buttonState[1]>=944  && buttonState[1]<=946){  // Action Button#11
    digitalWrite(recvLed, HIGH);
      digitalWrite(funtionpin[6], HIGH); // Pin 40 HIGH
      delay(10);
      digitalWrite(funtionpin[6], LOW); // Pin 40 LOW 
  }
  //Button #18
  else if (buttonState[1]>=929  && buttonState[1]<=931){  // Action Button#11
    digitalWrite(recvLed, HIGH);
      digitalWrite(funtionpin[7], HIGH); // Pin 41 HIGH
      delay(10);
      digitalWrite(funtionpin[7], LOW); // Pin 41 LOW 
  }
  // Motor RIGHT Button
  else if (buttonState[4] == HIGH) {   //Manuly turn head LEFT
    digitalWrite(recvLed, HIGH);
    motor1.drive(100);
  }
  // Motor LEFT Button
  else if (buttonState[5] == HIGH) {  //Manuly turn Head LEFT
    digitalWrite(recvLed, HIGH);
    motor1.drive(-100);
  }
    else {
      brake(motor1, motor2);
      digitalWrite(recvLed, LOW);
    }
    
}