Basic problem

Hey i am pretty new to Arduino so there must be something basic im missing here.

I have two vibration motors, i am generating a random variable, either 1 or 0 and then the respective vibration motor activates for a period of 3 seconds.

I want to have a button for each motor that once the motor is activated and the respective button is pressed once during the time the motor is activated., it returns a value. Below is what i have thus far

const int VibMotorLeftPin = 6;
const int VibMotorRightPin = 5;

const int ButtonLeftPin = 13;
const int ButtonRightPin = 12;

int MotorSpeed = HIGH;
long rand_val;

int ButtonLeftState, ButtonRightState;

void setup(){
  Serial.begin(9600);
  pinMode(VibMotorLeftPin,OUTPUT);
  pinMode(VibMotorRightPin,OUTPUT);
  
  pinMode(ButtonLeftPin,INPUT);
  pinMode(ButtonRightPin,INPUT);
 
  randomSeed(analogRead(0));
}

void loop(){
  
  changeSides();
  userPress();
   
}

void changeSides(){
  
  rand_val = random(0,2);
  Serial.println(rand_val);
  
  if(rand_val == 0){
    digitalWrite(VibMotorLeftPin,MotorSpeed);
    delay(3000);
    analogWrite(VibMotorLeftPin,LOW);
  }
  else if(rand_val == 1){
    digitalWrite(VibMotorRightPin,MotorSpeed);
    delay(3000);
    analogWrite(VibMotorRightPin,LOW);
  }  
    
}

void userPress(){
  
  ButtonLeftState = digitalRead(ButtonLeftPin);
  ButtonRightState = digitalRead(ButtonRightPin);
  
  if((ButtonLeftState == 0) && (VibMotorLeftPin == 1)){
    Serial.println("TRUE");
  }

}

All activity is suspended during this:

delay(3000);

During the delay(), it's not looking at your buttons.

Have a look at Blink Without Delay

Ah ok i figured that was the case. Thanks for the link il check it out!

else if(rand_val == 1)

This second 'if' is redundant since you know the value is 0 or 1 and you also know it's not 0.

You also seem to be using digitalWrite() and analogWrite() the wrong way around - digitalWrite accepts HIGH/LOW and analogWrite accepts a number in the range 0 .. 255.

There are lots of different ways to structure this code and no absolute right or wrong way to do it, but it seems to me that the current structure is confused about which part of your code does the timing and which part does the input detection. If I were you I would structure the code as follows:

Select a motor at random.
Start the selected motor.
Monitor the button associated with the selected motor for three seconds and detect whether the button has changed from not pressed to pressed in that period.
Stop the selected motor.
Report whether the button press occurred.

I'd put the motor pins in an array indexed by motor number (0 or 1) and put the corresponding input pin numbers in an array too. Then you can handle either motor+switch without having to duplicate your code. E.g.:

int side = selectSide();
analogWrite(motorPin[side], MotorSpeed);
bool result = waitForButtonPress(buttonPin[side], 3000);
digitalWrite(motorPin[side], LOW);

if(result)
{
    Serial.println("Something");
}
else
{
    Serial.println("Something else");
}

waitForButtonPress() is a function you would write which uses a while loop or similar to poll the specified input until the interval had elapsed, looking for state transitions.

Hey that sounds like a better route than the one i have currently taken, could you please elaborate on the below regarding the waitForButtonPress, i am a little confused here =/

bool result = waitForButtonPress(buttonPin[side], 3000);

I have rewritten my code altering it a little more to the structure you suggested and its helped a lot. The only issue i still have however is that it only prints TRUE when the button is pressed on the exact moment the rand_val is defined. I need the user to be able to press the button anytime during the 3 second the motor is activated and then TRUE should only be output once.

const int VibMotorLeftPin = 6;
const int VibMotorRightPin = 5;

const int ButtonLeftPin = 13;
const int ButtonRightPin = 12;

int MotorSpeed = HIGH;
long rand_val;

int ButtonLeftState, ButtonRightState;

int LeftMotor, RightMotor;

long previousMillis = 0;
long interval = 2000;

void setup(){
  Serial.begin(9600);
  pinMode(VibMotorLeftPin,OUTPUT);
  pinMode(VibMotorRightPin,OUTPUT);

  pinMode(ButtonLeftPin,INPUT);
  pinMode(ButtonRightPin,INPUT);

  randomSeed(analogRead(0));

  long previousMillis;
  long interval = 3000;
}

void loop(){

  changeSides();
  //userPress();

}

void changeSides(){

  rand_val = random(0,2);
  Serial.println(rand_val);
  
  unsigned long currentMillis = millis();

  if(rand_val == 0){
    digitalWrite(VibMotorLeftPin,HIGH);
    digitalWrite(VibMotorRightPin,LOW);
  }
  
  else if(rand_val == 1){
    digitalWrite(VibMotorRightPin,HIGH);
    digitalWrite(VibMotorLeftPin,LOW);
  }
 
   userPress();
 
 delay(3000);
}

void userPress(){

  //Buttons 0 is pressed and 1 us unpressed
  // Replace the stupid delays with millis()
  ButtonLeftState = digitalRead(ButtonLeftPin);
  ButtonRightState = digitalRead(ButtonRightPin);

  if((ButtonLeftState == 0) && (rand_val == 0)){
    Serial.println("TRUE");
  }
}

UNarmed:
The only issue i still have however is that it only prints TRUE when the button is pressed on the exact moment the rand_val is defined. I need the user to be able to press the button anytime during the 3 second the motor is activated and then TRUE should only be output once.

 delay(3000);

And no wonder! You have a "delay()" function in there. That is never going to get you anywhere. This function is really only for crude demonstration code. It does precisely nothing useful for three seconds; doesn't check your keys, nothing.

You have to write a function that continuously (repeatedly) tests your buttons (and does some debouncing) for three seconds. It's a loop or more precisely, it is in the main "loop".

And don't be tempted to use "while" either ... for the same reasons.