landstorm
Published

Autonomous Robot Nerf Target

Step by step instructions for making a nerf target that moves by itself and stops when shot enough times!

IntermediateFull instructions provided1,209
Autonomous Robot Nerf Target

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
USB Cable Assembly, USB Type A Plug to Micro USB Type B Plug
USB Cable Assembly, USB Type A Plug to Micro USB Type B Plug
×1
Normally closed switch
×1
18650 battery
×2
Spdt slide switch
×1
Resistor 100 ohm
Resistor 100 ohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
1/8 inch EVA foam with adhesive
×1

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
Soldering iron (generic)
Soldering iron (generic)
hack saw
wire strippers

Story

Read more

Custom parts and enclosures

electronics base

motor holder

swivel wheel brace

top

main robot body

Code

v5_nerf_robot_code.ino

C/C++
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B connections
int enB = 3;
int in3 = 5;
int in4 = 4;
//digital pins for difficulty settings
int easy = 12;
int hard = 11;

int hitCounter = 0;
int hitMax = 0;

const int buzzer = 10;
const int frontBumper = 2;
volatile byte state = LOW;
int buttonState = 0;
long randNum;
long rightOrLeft;
bool alarm = true;
unsigned long time;
bool endGame = false;
bool hitActive = false;
bool canStart = false;
int timeMax = 0;
bool hitFlag = false;

void setup() {
  Serial.begin(9600);
             
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  pinMode(buzzer, OUTPUT);
  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  
  pinMode(frontBumper, INPUT);
  pinMode(frontBumper, INPUT_PULLUP);
  
  randomSeed(analogRead(0));//need this for taking random values
  attachInterrupt(digitalPinToInterrupt(frontBumper), hit, FALLING);//use an interupt for the targed bumper so that it will activate anytime in the code.
  
  pinMode(easy, INPUT);
  pinMode(hard, INPUT);
}

void difficulty(){//determines the difficulty based off of the possition of the slide switch.
  if(digitalRead(easy)==HIGH){
   
    hitMax = 3;
    timeMax = 20000;
    
  }
  else if(digitalRead(hard)==HIGH){
    
    hitMax = 6;
    timeMax = 30000;
    
  }
  else{
    
    Serial.println("meduum");
    hitMax = 4;
    timeMax = 20000;
    
  }
}
void left(int motorSpeed, int quarters){
  analogWrite(enA, motorSpeed);
  analogWrite(enB, motorSpeed);

  // Turn on motor A & B
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(500);
  hitFlag = false;

}
void right(int motorSpeed, int quarters){
  analogWrite(enA, motorSpeed);
  analogWrite(enB, motorSpeed);
  //Serial.println("right");
  // Turn on motor A & B
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(500);
  hitFlag = false;
    
}


void straight(int motorSpeed){
  analogWrite(enA, motorSpeed);
  analogWrite(enB, motorSpeed);

  // Turn on motor A & B
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
 
}

void stopMotors(){ 
 
    Serial.println("stop");
    digitalWrite(enA, LOW);
    digitalWrite(enB, LOW);
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
}

void hit(){
  
  if(hitFlag == false && canStart == true){
   
    hitFlag = true;
    while(true){
    
    tone(buzzer, 1000, 500);
    hitFlag = true;
    buttonState = digitalRead(frontBumper);
    if(buttonState == LOW){
      hitCounter = hitCounter+1;
      hitFlag = true;
      break;
    }
  
  }
  
  if (hitCounter >= hitMax){ //game ends when hit enough times.
    
    stopMotors();
     tone(buzzer, 1000, 500);
    endGame = true;
  }
  
  }
  else{
    hitFlag = true;
  }
  
  }
  
  
void randomMove(){
  
  //each increment represents a quarter turn
  
  randNum = random(0, 8);
  rightOrLeft = random(0,2);
  // below code randomly makes the robot turn left or right.
  if(rightOrLeft ==0){
    right(255, randNum);
  }
  else{
    left(255, randNum);
  }
  
  if(endGame == false){
  straight(255);
  }
 
    delay(500);
    hitFlag=false;
 //start the timer and keep track of how long the robot has been runing.
  time = millis();
  if(time >= 20000){//if time has ran out.
    stopMotors();
    tone(buzzer, 1500, 1000);
    endGame = true;
  }
  
}
void start(){
  while(endGame == false){
    
    randomMove();
  
  }
  if(alarm == true){
  tone(buzzer, 1000, 500);
  delay(500);
  tone(buzzer, 1000, 500);
  delay(500);
  tone(buzzer, 1000, 500);
  }
  alarm = false;
  
  
}

void loop() {

 delay(4000); //delay for four seconds to give the player some time to get ready.
  difficulty();
  canStart = true;
  start();
  
  
}

Credits

landstorm
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.