Arduino Joy Stick module
Here is a simple Example of using the Joy Stick module with button.
Source Code
Circuit Diagram |
Source Code
// Arduino pin numbers
const int SW_pin = 2; // digital pin 2 connected to switch output
const int X_pin = 0; // analog pin 0 connected to X output
const int Y_pin = 1; // analog pin 1connected to Y output
const int Xmin = 8; // left - digital pin 8
const int Ymax = 9; // right - digital pin 9
const int Ymin = 10; // down - digital pin 10
const int Xmax = 11; // up - digital pin 11
int Y;
int X;
int SW;
void setup() {
pinMode(SW_pin, INPUT);
pinMode(Xmin, OUTPUT);
pinMode(Xmax, OUTPUT);
pinMode(Ymin, OUTPUT);
pinMode(Ymax, OUTPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");
X = analogRead(X_pin);
Y = analogRead(Y_pin);
SW = digitalRead(SW_pin);
if(X<=250){
digitalWrite(Xmin, HIGH);
Serial.print("Xmin\n");
}
else if(X>=750){
digitalWrite(Xmax, HIGH);
Serial.print("Xmax\n");
}
else if(Y<=250){
digitalWrite(Ymin, HIGH);
Serial.print("Ymin\n");
}
else if(Y>=750){
digitalWrite(Ymax, HIGH);
Serial.print("Ymax\n");
}
else{
digitalWrite(Xmin, LOW);
digitalWrite(Xmax, LOW);
digitalWrite(Ymin, LOW);
digitalWrite(Ymax, LOW);
Serial.print("none");
}
if(!SW){
digitalWrite(Xmin, HIGH);
digitalWrite(Xmax, HIGH);
digitalWrite(Ymin, HIGH);
digitalWrite(Ymax, HIGH);
Serial.print("clicked");
}
// delay(500);
}
Comments
Post a Comment