Help Stop my motor!

I have a sketch which controls several aspects of a robot I'm creating.
everything works great BUT I'm trying to add a feature where the motor that turns the head (Via. Tb6612 motor controller) Will return to center when all other functions of the sketch are not being executed. (ie: when I'm done turning it, it looks forward)

simular to homing a stepper motor.

I have a limit switch set to signal when the motor is in the correct position, and am using "motor1.drive(255);" to rotate motor and "brake(motor1, motor2);" to stop it when limit switch is pressed.

I've placed the following code at the end of the sketch in the "else{ " statement and it turns and stops correctly when the limit switch is pressed, but it also locks out the rest of the sketch.

while (digitalRead(homeSwitch) ) {
        motor1.drive(255);
      }
      while (!digitalRead(homeSwitch) ){
      brake(motor1, motor2);
}

I have tried setting up a function to call in the same place thinking removing it from the main loop would help but I keep getting error codes and am completely lost :

void headHoming() {
   while (digitalRead(homeSwitch) ) {
        motor1.drive(255);
      }
      while (!digitalRead(homeSwitch) ){
      brake(motor1, motor2);
}
}

The Above returns: "a function-definition is not allowed here before '{' token"

Is there a better way of doing this? can someone help me with some code of function that will home this motor WITHOUT interrupting or blocking the rest of the sketch?

Thank you!

Recever_homeing_tests.ino (7.76 KB)

BubbleHockey:
Is there a better way of doing this?

Yes. Post your entire sketch using code tags. Those little snippets are not much help. If you auto format your code (Ctrl-T) you will see that your function definition for headHomeing() is inside the setup() function. This is not allowed. Function definitions need to be outside other functions.

blh64:
Yes. Post your entire sketch using code tags. Those little snippets are not much help. If you auto format your code (Ctrl-T) you will see that your function definition for headHomeing() is inside the setup() function. This is not allowed. Function definitions need to be outside other functions.

Thank you, moving it outside of the setup and placing it at the end of the sketch allowed me to compile and upload it. However, calling the function from outside the loop did not solve the issue it still locks up all other functions.

I attached the entire sketch in the original post because its to large to post.

everything within the Loop fails to run after the setup is complete when I try to call the function from inside the "else" statement.

I can post the Loop below if that helps but again its to big to post the entire sketch.
I guess what I need is another way to call the "headHoming ()" that does not interrupt the "buttonState[]"

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#12
    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#13
    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#14
    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#15
    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#16
    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#17
    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#18
    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(255);
  }
  // Motor LEFT Button
  else if (buttonState[5] == HIGH) {  //Manuly turn Head LEFT
    digitalWrite(recvLed, HIGH);
    motor1.drive(-255);
  }
    else {
    headHoming();
    digitalWrite(recvLed, LOW);    
}
}
 void headHoming () {
      while (digitalRead(homeSwitch) ) {
        motor1.drive(255);
      }
      while (!digitalRead(homeSwitch) ){
      brake(motor1, motor2);
}
}

BubbleHockey:
I attached the entire sketch in the original post because its to large to post.

Oh no it's not :slight_smile:

// Receiver talk action and dome motor correct pins for rf24 Start up sounds
#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 homeSwitch 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(49, 48); // CE, CSN   originaly 7, 8 

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;
int i;

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(homeSwitch, INPUT_PULLUP); //dome centering limit switch.<<<<<<<<change
  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); //<<<< play startup sound (track 026 in folder 02)
  for (i=0; i<4; i++) {  //<<<< for loop blinking all lights 4 times. (add Any other lights to loop)
  digitalWrite(octoWhite, HIGH);
  digitalWrite(sideVent, HIGH);
  delay(400);
  digitalWrite(octoWhite, LOW);
  digitalWrite(sideVent, LOW);
  delay(400);
  }
  digitalWrite(octoBlu, HIGH);
  digitalWrite(sideVent, HIGH);
  delay(20);
  
  void headHoming () {
      while (digitalRead(homeSwitch) ) {
        motor1.drive(255);
      }
      while (!digitalRead(homeSwitch) ){
      brake(motor1, motor2);
}
}
}
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#12
    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#13
    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#14
    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#15
    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#16
    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#17
    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#18
    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(255);
  }
  // Motor LEFT Button
  else if (buttonState[5] == HIGH) {  //Manuly turn Head LEFT
    digitalWrite(recvLed, HIGH);
    motor1.drive(-255);
  }
    else {
    headHoming();
    digitalWrite(recvLed, LOW);    
}
}

...R

Robin2:
Oh no it's not :slight_smile:

Oh, perhaps my level? When I tried it said it was to many characters.

Thank you!

Your code is very badly laid out. Use the AutoFormat tool and it should be obvious that you are trying to define the headHoming() function inside your setup() function - which is not allowed.

I suspect the reason the program locks up is because the headHoming() function is always called by the catch-all ELSE clause. You need to have a specific circumstance in which the headHoming() function is called.

All those ELSE IF clauses can be simplified like this - i.e. test them in descending order

if (buttonState[0]>= 1013) {

else if (buttonState[0]>=993) {

else if (buttonState[0]>=974) {

// etc etc

...R

BubbleHockey:
Oh, perhaps my level?

No, you just needed to put it in a Post with fewer other characters. I have the same 9000 char limit as you.

...R

Robin2:
I suspect the reason the program locks up is because the headHoming() function is always called by the catch-all ELSE clause. You need to have a specific circumstance in which the headHoming() function is called.

I guess that IS my main problem but I have no idea how I would do this outside of adding a button to "home" the motor manually.

WHICH I could DO, but I was hoping to be able to have it do it automatically, so later down the line, when other complex movements are added, so It's one less thing I need to do between these movements.

BubbleHockey:
but I was hoping to be able to have it do it automatically,

Describe in english (not code) the circumstances in which you want the homing to happen. Give as much detail as you can.

...R

Robin2:
Describe in english (not code) the circumstances in which you want the homing to happen. Give as much detail as you can.

...R

Ok, here goes...

The Robot head it controlled by the Dc motor in question, spinning it around Via. a geared track.

I use a home-made rocker switch (in the code buttonState[4] buttonState[5]) to move or rotate the head manually, stoping it when neither button is pressed.

I also have it rotate randomly either right or left using millis (at the top of the loop) corresponding with a random sound, for some realism.

I have a Limit switch (with a pull up resistor) on the Geared track, so when it has rotated to the forward position, the limit switched is pressed. signaling that it is now looking straight.

I want to have the "head" return to center, or forward whenever I am NOT moving it manually or the Random move is not being completed.

SO, after I move the head to the right or left, when I let go of the rocker switch, no matter the orientation to which I leave the head, it will always return the head to the forward position. Without me having to manually return it to forward and guess at the position.

Almost as if it were on a spring or rubber band returning it back to where it cam from.

(the other buttons being effected on the "Else if" mess, are to control different functions down the line, and to play different mp3 tracks.)

I hope that helps.

BubbleHockey:
I want to have the "head" return to center, or forward whenever I am NOT moving it manually or the Random move is not being completed.

SO, after I move the head to the right or left, when I let go of the rocker switch, no matter the orientation to which I leave the head, it will always return the head to the forward position. Without me having to manually return it to forward and guess at the position.

Almost as if it were on a spring or rubber band returning it back to where it cam from.

That makes things much clearer but I think we need a 2-stage approach to solving the problem.

As the code is written in Reply #3 you have a long list of ELSE IF tests. I think they should not all be a single group. I think you need a group for buttonState[0], another group for buttonState[1] and another group for the buttons that move the head.

Start be reorganising the code like that and post the new version together with a description of how it behaves. And please (VERY IMPORTANT) use the AutoFormat tool in the Arduino IDE to indent the code consistently before posting it.

...R

Robin2:
Start be reorganising the code like that and post the new version together with a description of how it behaves. And please (VERY IMPORTANT) use the AutoFormat tool in the Arduino IDE to indent the code consistently before posting it.

...R

Ok, Here is how the following sketch works.
its for a robot that will do a lot more when other sketches are integrated.
it uses Rf24 to receive commands from a remote, a Dfplayer mini for the sounds, and a tb6612 to move the motor.

but for now all it does, is:

Flashes some lights and plays a track at start

Loop:

Turns the Head randomly either right or left every min to a set movement and plays a random track from a set of 144 tracks.

in the rest of the loop it receives input from 2 analog inputs 2 digital and a joystick(for later use) in an array

button state [0] & [1]:
A2= 10 buttons controlling sounds, plays set track on the dfplayer mini
A3= 8 buttons (will control other complex functions later, for now they are placeholder lighting an led each)

2 digital buttons (button state [4]&[5]) = Turn the Head Right & Left when pressed, and STOP when its NOT.

Each of these also lights the same led to signal its received the command.

I've marked each of these sections with "******" hopefully that helps.
this is also the working version WITHOUT the headHoming() function that is at the end. Just uses motor1.break to stop motor.

** the code I'll post below, I'm over 9000 characters**

#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 received activity 
#define sideVent 23  // Lights for Both Side Vents
#define octoWhite 25  // Octagon Lights WHITE
#define octoBlu 27  // Octagon Lights BLUE
#define homeSwitch 29  //Head turning Switch
#define AIN1 24
#define BIN1 28
#define AIN2 26
#define BIN2 30
#define PWMA 5
#define PWMB 6
#define STBY 9

   //******Dfplayer mini & Rf24l01 Setup ***************

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

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;
int i;

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(homeSwitch, INPUT_PULLUP); //dome centering limit switch.<<<<<<<<change
  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);  //Starting Dfplayermini

  myMP3.volume(24);
  delay(20);

  //*******Flash Octagons & Vents on startup***********

  myMP3.playFolder(2, 26); //<<<< play startup sound (track 026 in folder 02)
  for (i = 0; i < 4; i++) { //<<<< for loop blinking all lights 4 times. (add Any other lights to loop)
    digitalWrite(octoWhite, HIGH);
    digitalWrite(sideVent, HIGH);
    delay(400);
    digitalWrite(octoWhite, LOW);
    digitalWrite(sideVent, LOW);
    delay(400);
  }
  digitalWrite(octoBlu, HIGH);
  digitalWrite(sideVent, HIGH);
  delay(20);
}


void loop()
{
  delay(5);
  randomMove = random(0, 10);
  unsigned long currentTime = millis();

  //******** Setting Head to move randomly right or left with random track*******

  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;
  }
  //******** Start Rf24 listening to controller for commands *******
  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));
    }
  }

  //*****************Buttonstate[0]*******************

  //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);
  }

  //**************** Buttonstate[1]**************

  //Button #11:
  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#12
    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#13
    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#14
    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#15
    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#16
    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#17
    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#18
    digitalWrite(recvLed, HIGH);
    digitalWrite(funtionpin[7], HIGH); // Pin 41 HIGH
    delay(10);
    digitalWrite(funtionpin[7], LOW); // Pin 41 LOW
  }

  //**************** Buttonstate[4]&[5] Head Movement******************

  // Motor RIGHT Button
  if (buttonState[4] == HIGH) {   //Manuly turn head RIGHT
    digitalWrite(recvLed, HIGH);
    motor1.drive(255);
  }
  // Motor LEFT Button
  else if (buttonState[5] == HIGH) {  //Manually turn Head LEFT
    digitalWrite(recvLed, HIGH);
    motor1.drive(-255);
  }
  else {
    motor1.brake();
    digitalWrite(recvLed, LOW);
  }
}

  //**** Homing head******

void headHoming () {
  while (digitalRead(homeSwitch) ) {
    motor1.drive(255);
  }
  while (!digitalRead(homeSwitch) ) {
    brake(motor1, motor2);
  }
}

Apart from the headHoming() is the code in Reply #12 working properly?

I presume you want the headHoming() function to be called (in some way, not yet decided) from the last block of code that deals with buttons 4 and 5.

Is the switch on buttons 4 and 5 a 3-position toggle switch with centre off? If so, I assume it should home when neither 4 nor 5 is selected. BUT - you won't want it to start homing just while you are moving the switch from position 4 to 5 or 5 to 4. So there probably needs to be a short time interval between moving to the centre and the homing beginning.

Does that make any sense? And please tell me if I have completely misunderstood the requirement.

...R

You just need to put your headHoming() call in your final else() clause. The way you have your code currently structured, you really don't need it. Your random movements move left and then right before you code continues. If you want to make those more random and not always move back, then it does make sense.

Ideally, you would re-structure your code so you are not constantly waiting for movements and then going back to loop. A state machine would be better.

As for the headHoming() code, you don't need to continuously send the drive() command

void headHoming () {
  if (digitalRead(homeSwitch) == HIGH ) {
    // not home, so turn on motor
    motor1.drive(255);
  }
  while (digitalRead(homeSwitch) == HIGH ) {
    // not home so wait
  }
  brake(motor1, motor2);
}

Robin2:
Apart from the headHoming() is the code in Reply #12 working properly?

I presume you want the headHoming() function to be called (in some way, not yet decided) from the last block of code that deals with buttons 4 and 5.

Is the switch on buttons 4 and 5 a 3-position toggle switch with centre off? If so, I assume it should home when neither 4 nor 5 is selected. BUT - you won't want it to start homing just while you are moving the switch from position 4 to 5 or 5 to 4. So there probably needs to be a short time interval between moving to the centre and the homing beginning.

Does that make any sense? And please tell me if I have completely misunderstood the requirement.

...R

No, thats it exactly.

Yes, the Sketch in reply 12 is working perfectly.

With the headhoming(): YES, it would go with buttons 4&5.
I got the motor to spin until I clicked the limit switch, which is what I wanted. but it locked up the rest. So If calling headHoming() as it is in the sketch works and is a good way to do it, Yes. if there is a better way I'm open to any changes.

the Limit switch (pull_up) is connected to pin 29

With buttons 4&5, I have it designed with springs over each button so they are OFF (not pressed) when the rocker in in center position, So, yes, the first one, I'd like it to start homing when neither of the buttons are pressed.

blh64:
You just need to put your headHoming() call in your final else() clause. The way you have your code currently structured, you really don't need it. Your random movements move left and then right before you code continues. If you want to make those more random and not always move back, then it does make sense.

Not exactly, its more to do with the buttons from buttonstate 4 & 5. I'd like it to return while THOSE buttons are not pressed.
In doing so, I'm making the assumption, that when I put in the 'Homing" portion, while nothing is happening, within the "random" movement portion, I would simply remove the second movement so that the Homing portion of the code would then take care of returning the head to center after each random movement.

Try this. The idea is that it will only call headHoming() if neither button has been pressed for 5 seconds

  //**************** Buttonstate[4]&[5] Head Movement******************

  // Motor RIGHT Button
  if (buttonState[4] == HIGH) {   //Manuly turn head RIGHT
    digitalWrite(recvLed, HIGH);
    motor1.drive(255);
    lastTimeButtonPushed = millis();
  }
  // Motor LEFT Button
  else if (buttonState[5] == HIGH) {  //Manually turn Head LEFT
    digitalWrite(recvLed, HIGH);
    motor1.drive(-255);
    lastTimeButtonPushed = millis();
  }
  else {
    motor1.brake();
    digitalWrite(recvLed, LOW);
  }
  if (millis() - lastTimeButtonPushed >= 5000) {
    headHoming();
  }

...R

Robin2:
Try this. The idea is that it will only call headHoming() if neither button has been pressed for 5 seconds

  //**************** Buttonstate[4]&[5] Head Movement******************

// Motor RIGHT Button
  if (buttonState[4] == HIGH) {  //Manuly turn head RIGHT
    digitalWrite(recvLed, HIGH);
    motor1.drive(255);
    lastTimeButtonPushed = millis();
  }
  // Motor LEFT Button
  else if (buttonState[5] == HIGH) {  //Manually turn Head LEFT
    digitalWrite(recvLed, HIGH);
    motor1.drive(-255);
    lastTimeButtonPushed = millis();
  }
  else {
    motor1.brake();
    digitalWrite(recvLed, LOW);
  }
  if (millis() - lastTimeButtonPushed >= 5000) {
    headHoming();
  }




...R

First I declared the "lastTimeButtonPushed" as an int. is that correct? int lastTimeButtonPushed;

When I made the changes two things happened.

I had the head offset to start so the switch was NOT pressed. All seemed to work I moved the head a bit 5 seconds later it returned to home. and locked everything up again.

I turned the head manually so the homing switch was not pressed. and during that time BEFORE the 5 seconds, I was able to get confirm that the sounds were working.

so I looks like when the switch is pressed its locking out all other functions and not allowing me to turn the head or any other part of the sketch.

esle if means if the beginning if-condition is false only then the else-ifs are checked.
do you really want the else-if-conditions

  // Motor RIGHT Button
  else if (buttonState[4] == HIGH) {   //Manuly turn head LEFT
    digitalWrite(recvLed, HIGH);
    motor1.drive(255);
  }
  // Motor LEFT Button
  else if (buttonState[5] == HIGH) {  //Manuly turn Head LEFT
    digitalWrite(recvLed, HIGH);
    motor1.drive(-255);
  }

be checked if the beginning if-condition

if (buttonState[0]>= 1013 && buttonState[0]<= 1015)

delivers evaluates to false?

see this tutorial

There is a famous wording about programming

"The bug always has its hands on the keyboard and is staring at the screen"

best regards Stefan