Joystick
|
The Joystick.
|
|
There are 5 pins. GND, +5V, VRx, VRy and SW. I would guess that SW is digital (1 or 0 for the switch,
activated by clicking the joystick down), and VRx and VRy are analog, returning values depending on how the
joystick is moved sideways.
So I wired it accordingly.
Wired up.
Arduino 5V -> breadboard -> resistor -> LED ->
Joystick +
Joysticks GND - -> breadboard -> Arduino GND
The yellow pin from the water sensor is now ready to read.
|
|
This is the code that I used.
/*
Read the Joystick. Try reading the analog values, and / or the digital value, to see what returns.
*/
int analogInputPinX = 3;
int analogInputPinY = 5;
int digitalInputPin = 2;
int wait = 1000;
long analogInputValX;
long analogInputValY;
int digitalInputVal;
void setup()
{
pinMode(analogInputPinX, INPUT);
pinMode(analogInputPinY, INPUT);
pinMode(digitalInputPin, INPUT);
Serial.begin(9600); // ...set up the serial ouput on 0004 style
}
void loop()
{
analogInputValX = analogRead(analogInputPinX);
analogInputValY = analogRead(analogInputPinY);
digitalInputVal = digitalRead(digitalInputPin);
Serial.print("Digital is ");
Serial.print(digitalInputVal);
Serial.print(" and Analog X is ");
Serial.print(analogInputValX);
Serial.print(" and Analog Y is ");
Serial.print(analogInputValY);
Serial.println(" "); // println, to end with a carriage return
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
|
That code led to these results :
|
|
So I changed the code to remove the loop, and only print values when they changed.
Here is the code :
/*
Read the joystick. Only print values that change.
*/
int analogInputPinX = 3;
int analogInputPinY = 5;
int digitalInputPin = 2;
int wait = 1000;
long analogInputValX;
long analogInputValY;
long last_analogInputValX;
long last_analogInputValY;
int digitalInputVal;
void setup()
{
pinMode(analogInputPinX, INPUT);
pinMode(analogInputPinY, INPUT);
pinMode(digitalInputPin, INPUT);
Serial.begin(9600); // ...set up the serial ouput on 0004 style
}
void loop()
{
analogInputValX = analogRead(analogInputPinX);
analogInputValY = analogRead(analogInputPinY);
digitalInputVal = digitalRead(digitalInputPin);
if (digitalInputVal != 0)
{
Serial.print("YAY ! Digital is ");
Serial.print(digitalInputVal);
Serial.println(" "); // println, to end with a carriage return
}
if (analogInputValX != last_analogInputValX)
{
Serial.print("Change X to ");
Serial.print(analogInputValX);
Serial.println(" "); // println, to end with a carriage return
last_analogInputValX = analogInputValX;
}
if (analogInputValY != last_analogInputValY)
{
Serial.print("Change Y to ");
Serial.print(analogInputValY);
Serial.println(" "); // println, to end with a carriage return
last_analogInputValY = analogInputValY;
}
}
|
And here is the resulting screenshot :
That code led to these results :
|
|