Arduino “having11” Guy
Published © GPL3+

Minecraft Sword for Real Life

Use an actual Minecraft sword to control the character onscreen. When you swing the sword the sword onscreen also swings.

IntermediateFull instructions provided3 hours439
Minecraft Sword for Real Life

Things used in this project

Story

Read more

Schematics

Schematic

Schematic vazffjp8q8

Code

ESP8266 Code

C/C++
#include <Wire.h>
#include <SPI.h>
#include <SparkFunLSM9DS1.h>

LSM9DS1 imu;
#define LSM9DS1_M  0x1E 
#define LSM9DS1_AG  0x6B

#define PRINT_SPEED 500

void setup() {
  // put your setup code here, to run once:
  Wire.begin(4,5);
  Serial.begin(115200);
  imu.settings.device.commInterface = IMU_MODE_I2C;
  imu.settings.device.mAddress = LSM9DS1_M;
  imu.settings.device.agAddress = LSM9DS1_AG;
  if (!imu.begin())
  {
    Serial.println("Failed to communicate with LSM9DS1.");
    Serial.println("Double-check wiring.");
    Serial.println("Default settings in this sketch will " \
                  "work for an out of the box LSM9DS1 " \
                  "Breakout, but may need to be modified " \
                  "if the board jumpers are.");
    while (1)
      ;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
if ( imu.accelAvailable() )
  {
    // To read from the accelerometer, first call the
    // readAccel() function. When it exits, it'll update the
    // ax, ay, and az variables with the most current data.
    imu.readAccel();
  }
  if(abs(imu.calcAccel(imu.ax))>1.9 || abs(imu.calcAccel(imu.ay))>1.9 || abs(imu.calcAccel(imu.az)) > 1.9){
      Serial.println("hit");
      delay(PRINT_SPEED);
    }
}

Python Code

Python
import pyautogui
from time import sleep
from serial import Serial

pa = pyautogui

ser = Serial("COM10", baudrate=115200)

x, y = pa.size()

sleep(3)

pa.moveTo(x/2,y/2)

sleep(3)

try:
    while 1:
        if ser.in_waiting:
            data = ser.readline()
            print(data)
            if data:
                pa.click()

except KeyboardInterrupt:
    ser.close()

Credits

Arduino “having11” Guy

Arduino “having11” Guy

31 projects • 212 followers
IoT, Arduino, and Raspberry Pi. I can create designs, code, and build projects for only $22/hr. Just message me with your project request.

Comments