9. Input devices¶
This week I learned about diffent kinds of input devices and how to use them.
General specifications¶
For my final project I want to use a joystick to make a selection. The joystick module which I use has two analog, one digital and a Vcc and GND pin. The x and y values of the joystick are generated by a potentiometer for each axis which generates an analoge signal. To test the joystick connection I used the following code.
Useful links¶
Code Explaination¶
I used the serial output for the FTDI cable again, so I can read the measured value of the joystick input. Since I need two analoge input siganls, I use the analoge pins “A0” for the x value and “A1” for thy y value.
#include <SoftwareSerial.h>
#define ANALOG_X A0
#define ANALOG_Y A1
#define BUTTON_PIN A2
SoftwareSerial mySerial(3,4); //RX;TX
int buttonState;
void setup() {
// put your setup code here, to run once:
pinMode(ANALOG_X, INPUT);
pinMode(ANALOG_Y, INPUT);
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH);
mySerial.begin(4800); // BAUD rate should not be more than 4800
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(BUTTON_PIN);
// if the button of the joystick is pressed, print a note in the serail mointor and pause // the input reading for 2 seconds
if(!buttonState){
mySerial.println("#-----------------------------------#");
mySerial.println("Button was pressed");
mySerial.println("#-----------------------------------#\n");
delay(2000);
}
// readout the values of the analoge pins
int xValue = analogRead(ANALOG_X);
int yValue = analogRead(ANALOG_Y);
// write the x and y values into the serial monitor
mySerial.print("x Value: ");
mySerial.print(xValue);
mySerial.print(" y Value: ");
mySerial.println(yValue);
}
Gallery¶
Here you can see the connections for the joystick:
And the values of the serail monitor:
Download¶
- The joystick code skript as a zip: week09_skript.rar