Joystick

From ElectroDragon Wiki
Revision as of 14:54, 18 June 2020 by Chao (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation

File:Joytstick-Schematic-red.pdf
File:Schematic-green.pdf Demo Code

Arduino Demo Code 2

int FirstShotX , FirstShotY;

void setup()
{
for(int i=0; i<19; i++)
{
pinMode(i, INPUT);
digitalWrite(i, 1);
}

Serial.begin(9600);
FirstShotX = 0;
FirstShotY = 0;

}

void loop(){

int i, someInt, flag = 0;
for(i=4; i<11; i++)
{
someInt = digitalRead(i);
if(someInt == 0)
{
flag =1;
break;
}
}
if(flag == 1)
{
switch(i)
{
case 4: Serial.println("--------> Button A"); break;
case 5: Serial.println("--------> Button B"); break;
case 6: Serial.println("--------> Button C"); break;
case 7: Serial.println("--------> Button D"); break;
case 8: Serial.println("--------> Button E"); break;
case 9: Serial.println("--------> Button F"); break;
case 10: Serial.println("--------> Button KEY"); break;
default: break;
}
flag=0;
}

int sensorValue = analogRead(A0);
if(FirstShotX == 0)
{
FirstShotX = sensorValue;
Serial.print("FirstShotX = ");
Serial.println(FirstShotX);
}

Serial.print("X = ");
Serial.println(sensorValue - FirstShotX);

sensorValue = analogRead(A1);
if(FirstShotY == 0)
{
FirstShotY = sensorValue;
Serial.print("FirstShotY = ");
Serial.println(FirstShotY);
}
Serial.print("Y = ");
Serial.println(sensorValue - FirstShotY);

delay(200);
}

Demo Code for Joystick Module

 
#include <Servo.h>
Servo myservo;
Servo myservo1;
int analogPin = 3;
int analogPin1 = 4;
int val,val1;
void setup()
{
  myservo.attach(9);  // 9 Pin output servo control signal 
  myservo1.attach(10);
  Serial.begin(9600);         // only use pin 9,10
}
void loop()
{
  Serial.print("servo:");
  Serial.println(val);
  delay(15);
  Serial.print("servo1:");
  Serial.println(val1);
  val = analogRead(analogPin);
  val1 = analogRead(analogPin1);
 // read from adjustable resistor analog value (from 0 - 1023)
  val = map(val, 0, 1023, 0, 179);     // use "map" function resize this value, get the angle value that need for the servo (0-180)
  val1 = map(val1, 0, 1023, 0, 179);
  myservo.write(val);     // set the position of servo
  myservo1.write(val1);
  delay(15);             // wait till the motor rotate to the target angle
}