9.2.5 9.2.5 Test Code
Download codes: Codes
Download and unzip the file. Open the corresponding code file. Code file for Chapter 9.2.5 is named 9-2-5.
/*
Keyestudio ESP32 Robot Arm
9-2-5 tutorial code
Function: read the value of the joystick in axis X Y Z
http://www.keyestudio.com
*/
#define xPin 13
//define axis X pin to IO13
#define yPin 15
//define axis Y pin to IO15
#define buttonPin 12
int
xData
=
0;
//define a variable to store the analog value in axis x
int
yData
=
0;
//define a variable to store the analog value in axis y
int
buttonData
=
0;
void
setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void
loop() {
xData
=
analogRead(xPin);
yData
=
analogRead(yPin);
buttonData
=
digitalRead(buttonPin);
Serial.print("X:");
Serial.print(xData);
Serial.print("
Serial.print(yData);
Serial.print("
Serial.println(buttonData);
delay(100);
//delay 100ms
}
9.2.6 9.2.6 Test Result
After uploading code, click
baud rate to 9600. The joystick values will be displayed on the monitor.
9.2. 9.2 Read Analog Value of Joystick
//define axis z (button) to pin 12
//define a variable to store the digital value in axis z
//set the baud rate of the serial port
//set pin buttonPin to input.
//read the analog value in axis x
//read the analog value in axis y
//read the button state and print it
//monitor prints "X:"
//print the analog value of axis X
Y:");
B:");
//print the analog value of axis Z(button) and wrap
on Arduino IDE to open the serial monitor, and click
keyestudio WiKi
to set
127
Need help?
Do you have a question about the ESP32 and is the answer not in the manual?
Questions and answers