GithubHelp home page GithubHelp logo

motor's Introduction

Motor

#include <AFMotor.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

int xposPin = A0; // 雙軸按鍵搖桿 VRx 接 Arduino Analog pin A0
int yposPin = A1; // 雙軸按鍵搖桿 VRy 接 Arduino Analog pin A1
int Xpos, Ypos;
char command;

void setup() {
  Serial.begin(9600);
  Serial.println("Motor control via Serial Monitor!");
  motor1.setSpeed(255);
  motor1.run(RELEASE);
  motor2.setSpeed(255);
  motor2.run(RELEASE);

}

void loop() {
  Xpos = analogRead(xposPin);
  Ypos = analogRead(yposPin);
  Serial.print(Xpos);
  Serial.print("--");
  Serial.println(Ypos);
  Serial.println(command);

  if (Xpos > 800 && Ypos <800 && Ypos>300) {
    command = 'W';
  } else if (Xpos < 10 && 300 < Ypos <800 ) {
    command = 'S';
  } else if (Ypos > 800 && 300 < Xpos <800 ) {
    command = 'D';
  } else if (Ypos < 10 && Xpos <800 && Xpos>300) {
    command = 'A';
  } else{
    command = 'X';  
  }

  switch (command) {
    case 'W':
      motor1.run(FORWARD);
      motor2.run(FORWARD);
      delay(100);
      break;
    case 'A':
      motor1.run(BACKWARD);
      motor2.run(FORWARD);
      delay(100);
      break;
    case 'S':
      motor1.run(BACKWARD);
      motor2.run(BACKWARD);
      delay(100);
      break;
    case 'D':
      motor1.run(FORWARD);
      motor2.run(BACKWARD);
      delay(100);
      break;
    case 'X':
      motor1.run(RELEASE);
      motor2.run(RELEASE);
      delay(100);
      break;
  }
  Xpos = 524;
  Ypos = 524;
  delay(100);
}

motor's People

Contributors

ray-uncoding avatar mason45ok avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.