wired control by usb joystick

Post Reply
limanad
Posts: 7
Joined: Thu Feb 06, 2014 2:32 pm

wired control by usb joystick

Post by limanad »

Here is the code i used to control my copter by usb joystick+arduino mini. Its not complete yet, have bugs, but worked for me to test quad.
Image

Code: Select all

//analog pins- input from joystick
#define axis_X 0
#define axis_Y 1
#define axis_Z 2
#define axis_W 3
#define axis_A 2  //button aux connected to digital pin 2
int value_X, value_Y, value_Z, value_W, value_R;
int value_A = 0; //aux
//pins for output signal
const int thrPin = 9;
const int ptchPin = 10;
const int rollPin = 11;
const int yawPin = 12;
const int auxPin = 13;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(thrPin, OUTPUT);
  pinMode(ptchPin, OUTPUT);
  pinMode(rollPin, OUTPUT);
  pinMode(yawPin, OUTPUT);
  pinMode(auxPin, OUTPUT);
  pinMode(axis_A, INPUT); //button input
}

void loop() {
  // if there's any serial available, read it, and convert to 1000-2000:
value_X = analogRead(axis_X);   // read X axis from 1 stick
  value_X = map(value_X, 0, 1023, 1000, 2000);
value_Y = analogRead(axis_Y);    // read Y axis from 1 stick
  value_Y = map(value_Y, 0, 1023, 1000, 2000);
value_Z = analogRead(axis_Z);   // read X axis from 2 stick
  value_Z = map(value_Z, 0, 1023, 1000, 2000);
value_W = analogRead(axis_W);    // read Y axis from 2 stick
  value_W = map(value_W, 0, 1023, 1000, 2000);
  value_R= auxPin;
  value_A = digitalRead(axis_A);   // read button state, if
  if(value_A == HIGH)  value_R = 1000;     // aux 1000
  else value_R = 2000;
digitalWrite(axis_A, LOW);  //aux 2000

// write stick positions to multiwii
digitalWrite(thrPin,HIGH);
  delayMicroseconds(value_X);
    digitalWrite(thrPin,LOW);
  delayMicroseconds(500);
digitalWrite(ptchPin,HIGH);
  delayMicroseconds(value_Y);
    digitalWrite(ptchPin,LOW);
  delayMicroseconds(500);
digitalWrite(rollPin,HIGH);
  delayMicroseconds(value_Z);
    digitalWrite(rollPin,LOW);
  delayMicroseconds(500);
digitalWrite(yawPin,HIGH);
  delayMicroseconds(value_W);
    digitalWrite(yawPin,LOW);
  delayMicroseconds(500);
digitalWrite(auxPin,HIGH);
  delayMicroseconds(value_R);
    digitalWrite(auxPin,LOW);
  delayMicroseconds(500);

    }

Image

tovrin
Posts: 705
Joined: Tue Sep 20, 2011 4:08 pm

Re: wired control by usb joystick

Post by tovrin »

lol, that doesnt seem dangerous at all

Post Reply