Joystick

From ElectroDragon
Revision as of 12:05, 26 November 2016 by Chao (talk | contribs) (Arduino Demo Code 1)
Jump to navigation Jump to search

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);
}