Weapon Prototype

Turret 

Prototype 01

With DC motor as a gun controller

With 360 degrees servo as a gun controller

YouTube: Video

Circuit:


Code:

(With DC motor as gun controller, this code is under modification)


#include <Servo.h>
#include <HCSR04.h>

Servo base;

void rotateBase();
void shoot();

int pir = 5;

int trig = 11;
int echo = 10;

int laser = 13;
int gun = 3;

int motion = 0, detect = 0, shot = 0, minDistance = 100;
long distance = 0;
float duration = 0;
float oneRotation = 10.87; //DC Motor's one rotation time taken in ms
int loaded = 0;

void setup()
{
  base.attach(2);
  pinMode(pir, INPUT);
  pinMode(laser, OUTPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);
  base.write(90);
  digitalWrite(laser, OUTPUT);
  digitalWrite(gun, OUTPUT);
}

void loop()
{
  motion = digitalRead(pir);

  if (motion == 1)
  {
    digitalWrite(laser, 1);
    rotateBase();
  }
  else
  {
    digitalWrite(laser, 0);
  }
}

void rotateBase()
{
  int basePos = 90;
  for (basePos = 90; basePos <= 180; basePos++)
  {
    base.write(basePos);
    digitalWrite(trig, 0);
    delayMicroseconds(2);
    digitalWrite(trig, 1);
    delayMicroseconds(10);
    digitalWrite(trig, 0);
    duration = pulseIn(echo, 1);
    distance = duration * 0.034 / 2;
    Serial.print("Distance:");
    Serial.println(distance);
    if (distance <= minDistance)
    {
      Serial.println("Detected!");
      detect++;
      shoot();
    }
    delay(15);
  }
  for (basePos = 180; basePos >= 0; basePos--)
  {
    base.write(basePos);
    digitalWrite(trig, 0);
    delayMicroseconds(2);
    digitalWrite(trig, 1);
    delayMicroseconds(10);
    digitalWrite(trig, 0);
    duration = pulseIn(echo, 1);
    distance = duration * 0.034 / 2;
    Serial.print("Distance:");
    Serial.println(distance);
    if (distance <= minDistance)
    {
      Serial.println("Detected!");
      detect++;
      shoot();
    }
    delay(15);
  }
}

void shoot()
{
  digitalWrite(laser, 1);
  digitalWrite(gun, 1);
  delay(oneRotation);
  digitalWrite(gun, 0);
  shot++;
  Serial.print("Total Detection:");
  Serial.println(detect);
  Serial.print(shot);
  Serial.println("Shot!");
  loop();
}

Prototype 02

Remote control + Automatic

Circuit:


Code 01:

(With DC motor as gun controller, this code is also under modification)

#include <Servo.h>
#include <IRremote.h>

int IRpin = 12;

Servo base;

IRrecv sensor(IRpin);
decode_results value1;

void rotateBase(); //Automatic mode
void automaticShoot();
void controlledShoot();
void remoteControl();
void manualMode();

int systemState = 13; //Turret On/Off
int systemMode[3]; //Mode

int pir = 3;
int indicator = 2;

int trig = 7;
int echo = 6;

int laser = 8;

int gun = 5;
int oneRotation = 10.87;

int motion = 0, detect = 0, shot = 0, minDistance = 100;
long distance = 0;
float duration = 0;

int loaded = 0;
int number = 0;

void setup()
{
  pinMode(systemState, OUTPUT);// green led
  for (int i = 0, j = 9; i <= 2, j <= 11; i++, j++) //RGB led
  {
    systemMode[i] = j;
    pinMode(systemMode[i], OUTPUT);
    digitalWrite(systemMode[i], 0);
  }
  base.attach(4);
  pinMode(pir, INPUT);
  pinMode(indicator, OUTPUT); //blue led
  pinMode(laser, OUTPUT);     //red led
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);
  base.write(90);
  digitalWrite(laser, 0);
  pinMode(gun, OUTPUT);

  sensor.enableIRIn();
}

void loop()
{
  base.write(90);
  digitalWrite(laser, 0);
  digitalWrite(gun, 0);
  digitalWrite(indicator, 0);
  digitalWrite(laser, 0);
  digitalWrite(systemMode[2], 0);
  digitalWrite(systemMode[1], 0);
  digitalWrite(systemMode[0], 0);


  if (sensor.decode(&value1)) {
    unsigned int state = value1.value;
    Serial.println(state);
    if (state == 2704 ) {
      if (number % 2 == 0)
      {
        digitalWrite(systemState, 1);
        motion = digitalRead(pir);

        if (motion == 1)
        {
          digitalWrite(indicator, 1);
          remoteControl();
        }
        else
        {
          digitalWrite(indicator, 0);
          loop();
        }
        delay(100);
      }
      else
      {
        digitalWrite(systemState, 0); loop();
      }
    }
    number++;
    sensor.resume();
  }
}

void remoteControl()
{
  digitalWrite(systemMode[2], 0);
  digitalWrite(systemMode[1], 0);
  digitalWrite(systemMode[0], 0);
  digitalWrite(laser, 0);
  if (sensor.decode(&value1)) {
    unsigned int state = value1.value;
    Serial.println(state);
    switch (state)
    {
      case 1234: Serial.println("...Automatic Mode..."); rotateBase(); break;
      case 4321: Serial.println("...Manual Mode..."); manualMode(); break;
      case 2704: Serial.println("...System Off..."); loop(); break;
    }
    sensor.resume();
  }
}

void manualMode()
{
  digitalWrite(systemMode[2], 1);
  digitalWrite(systemMode[1], 0);
  digitalWrite(systemMode[0], 1);

  int initial = 90;
  int minBaseRotation = 10;
  int controlMode = 1;
  base.write(initial);
  digitalWrite(laser, 1);

  while (controlMode) {
    if (sensor.decode(&value1)) {
      unsigned int controlButton = value1.value;
      Serial.println(controlButton);
      switch (controlButton)
      {
        case 1: /*Rotate right*/ initial += minBaseRotation; base.write(initial); break;
        case 2: /*Rotate left*/ initial -= minBaseRotation; base.write(initial); break;
        case 3: controlledShoot(); break;
        case 4: rotateBase(); break; //Automatic
        case 2704: Serial.println("...System Off..."); loop(); break;
      }
      sensor.resume();
    }
  }
}

void controlledShoot()
{
  digitalWrite(gun, 1);
  delay(oneRotation);
  digitalWrite(gun, 0);
  shot++;
  Serial.print("Total Detection:");
  Serial.println(detect);
  Serial.print(shot);
  Serial.println("Shot!");
  manualMode();
}

void rotateBase()
{
  digitalWrite(systemMode[2], 0);
  digitalWrite(systemMode[1], 0);
  digitalWrite(systemMode[0], 1);
  motion = digitalRead(pir);
  if (motion == 1)
  {
    int basePos = 90;
    for (basePos = 90; basePos <= 180; basePos++)
    {
      base.write(basePos);
      digitalWrite(trig, 0);
      delayMicroseconds(2);
      digitalWrite(trig, 1);
      delayMicroseconds(10);
      digitalWrite(trig, 0);
      duration = pulseIn(echo, 1);
      distance = duration * 0.034 / 2;
      Serial.print("Distance:");
      Serial.println(distance);
      if (distance <= minDistance)
      {
        Serial.println("Detected!");
        detect++;
        automaticShoot();
      }
      delay(15);
    }
    for (basePos = 180; basePos >= 0; basePos--)
    {
      base.write(basePos);
      digitalWrite(trig, 0);
      delayMicroseconds(2);
      digitalWrite(trig, 1);
      delayMicroseconds(10);
      digitalWrite(trig, 0);
      duration = pulseIn(echo, 1);
      distance = duration * 0.034 / 2;
      Serial.print("Distance:");
      Serial.println(distance);
      if (distance <= minDistance)
      {
        Serial.println("Detected!");
        detect++;
        automaticShoot();
      }
      delay(15);
    }
  }
  else
  {
    loop();
  }

}

void automaticShoot()
{
  digitalWrite(gun, 1);
  delay(oneRotation);
  digitalWrite(gun, 0);
  shot++;
  Serial.print("Total Detection:");
  Serial.println(detect);
  Serial.print(shot);
  Serial.println("Shot!");
  delay(100);
  rotateBase();
}

YouTube: 

Code 02:

Functional code
#include <IRremote.h>
#include <Servo.h>
Servo base;

int button = 0;

//Functions
void power();
void automatic();
void manual();
void baseRotation();
void fire();
int ultrasonic();
int changeMode();

//Components
int systemState = 13; //Turret On/Off
int systemMode[3]; //Mode
int pir = 3;
int indicator = 2; //Motion Indicator blue
int trig = 7;//HC-SR04
int echo = 6;
int laser = 8; //red
int gun = 5;//DC Motor
int oneRotation = 10.87;

//Operations
int state = 0;
int modeAccess = 0;
int motion = 0;
long duration = 0;
float distance = 0;
int basePos = 90; //base position
int shot = 0;
int rotation = 90;
int end = 0;

int mapCodeToButton(unsigned long code) {
  if ((code & 0x0000FFFF) == 0x0000BF00) {
    code >>= 16;
    if (((code >> 8) ^ (code & 0x00FF)) == 0x00FF) {
      return code & 0xFF;
    }
  }
  return -1;
}

int readInfrared() {
  int result = -1;
  if (IrReceiver.decode()) {
    unsigned long code = IrReceiver.decodedIRData.decodedRawData;
    result = mapCodeToButton(code);
    IrReceiver.resume();
  }
  return result;
}

void setup()
{
  IrReceiver.begin(12);
  Serial.begin(9600);
  Serial.println("Switch On to Run");
  pinMode(systemState, OUTPUT);
  pinMode(laser, OUTPUT);
  pinMode(indicator, OUTPUT);

  base.attach(4);
  base.write(90);

  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

  for (int i = 0, j = 9; i <= 2, j <= 11; i++, j++) //RGB led
  {
    systemMode[i] = j;
    pinMode(systemMode[i], OUTPUT);
    digitalWrite(systemMode[i], 0);
  }
}

void loop()
{
  digitalWrite(systemMode[2], 0);
  digitalWrite(systemMode[1], 0);
  digitalWrite(systemMode[0], 0);

  button = readInfrared();
  if (button >= 0) {
    Serial.println(button);
    switch (button)
    {
      case 0: power(); break; //Button On/Off
      case 16: if (modeAccess)manual(); break; //Button 1
      case 17: if (modeAccess)automatic(); break; //Button 2
      default: break;
    }
  }
  delay(10);
}

void power()
{
  if (state % 2 == 0)
  {
    Serial.println("POWER ON");
    digitalWrite(systemState, 1);
    modeAccess = 1;
    state++;
  }
  else
  {
    Serial.println("POWER OFF");
    digitalWrite(systemState, 0);
    digitalWrite(laser, 0);
    digitalWrite(indicator, 0);
    modeAccess = 0;
    state++;
  }
}

void manual()
{
  Serial.println("Manual Mode");

  digitalWrite(systemMode[2], 1);
  digitalWrite(systemMode[1], 0);
  digitalWrite(systemMode[0], 1);

  int enter = 1;
  while (enter)
  {
    digitalWrite(laser, 1);
    button = readInfrared();
    if (button >= 0)
    {
      Serial.println(button);
      switch (button)
      {
        case 0: power(); goto label; break; //Button On/Off
        case 16: manual(); break; //Button 1
        case 17: automatic(); goto label; break; //Button 2
        case 5: fire(); break;
        case 6: if (rotation > 0 && rotation < 180) {
            rotation -= 10;
            base.write(rotation);
          } break;//left rotation
        case 4: if (rotation > 0 && rotation < 180) {
            rotation += 10;
            base.write(rotation);
          } break; //right rotation
        default: break;
      }
    }
    delay(10);
  }
label:
  Serial.println("Switch On to Run");
}

void automatic()
{
  Serial.println("Automatic Mode");

  digitalWrite(systemMode[2], 0);
  digitalWrite(systemMode[1], 0);
  digitalWrite(systemMode[0], 1);

  int enter = 1;
  while (enter)
  {
    button = readInfrared();
    if (button == 16)
    {
      Serial.println(button);
      manual();
      goto last;
    }
    else if (button == 0)
    {
      Serial.println(button);
      power();
      goto last;
      Serial.println("Switch On to Run");
      //goto last;
    }
    else
    {
      motion = digitalRead(pir);
      if (motion == HIGH)
      {
        digitalWrite(indicator, 1);
        for (basePos = 90; basePos <= 180; basePos += 5)
        {
          base.write(basePos);
          distance = ultrasonic();
          if (distance <= 50)
          {
            fire();
            Serial.println("In Range");
          }
          delay(10);
          end = changeMode();
          if (end)goto last;
        }
        for (basePos = 180; basePos >= 0; basePos -= 5)
        {
          base.write(basePos);
          distance = ultrasonic();
          if (distance <= 50)
          {
            Serial.println("In Range");
            fire();
          }
          delay(10);
          end = changeMode();
          if (end)goto last;
        }
      }
      base.write(90);
      digitalWrite(indicator, 0);
    }
  }
last:
  digitalWrite(indicator, 0);
}

void fire()
{
  digitalWrite(laser, 1);
  digitalWrite(gun, 1);
  delay(500);
  digitalWrite(gun, 0);
  shot++;
  Serial.print(shot);
  Serial.println("Shot!");
  delay(1000);
  digitalWrite(laser, 0);
}

int ultrasonic()
{
  digitalWrite(trig, 0);
  delayMicroseconds(2);
  digitalWrite(trig, 1);
  delayMicroseconds(10);
  digitalWrite(trig, 0);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.0343 / 2;
  Serial.print("Distance:");
  Serial.print(distance);
  Serial.println("cm");
  return distance;
}

int changeMode()
{
  button = readInfrared();
  if (button == 16)
  {
    Serial.println(button);
    manual();
    return 117;
  }
  else if (button == 0)
  {
    Serial.println(button);
    power();
    Serial.println("Switch On to Run");
    return 117;
  }
  return 0;
}

Comments

Popular posts from this blog

Keypad locker system

Basic Remote control car