137
Chapter 12 Joystick
Code
In this project code, we will read ADC value of X and Y axis of Joystick, and read digital quality of Z axis, then
print these data out.
C Code 12.1.1 Joystick
First observe the project result, and then analyze the code.
1.
Use cd command to enter 12.1.1_Joystick directory of C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/12.1.1_ Joystick
2.
Use following command to compile "Joystick.c" and generate executable file "Joystick.c". "-lm" option is
needed.
gcc Joystick.c -o Joystick -lwiringPi -lm
3.
Then run the generated file "Joystick".
sudo ./Joystick
After Program is executed, the terminal window will print out the data of 3 axes X, Y, Z. And shifting the
Joystick or pressing it will make those data change.
The flowing is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#include <softPwm.h>
#define address 0x48
//pcf8591 default address
#define pinbase 64
//any number above 64
#define A0 p 0
#define A1 p 1
#define A2 p 2
#define A3 p 3
#define Z_Pin 1
//define pin for axis Z
int
main
(
void
){
int
val_X
,
val_Y
,
val_Z
;
if
(
wiringPiSetup
()
==
-
1
){
//when initialize wiring failed,print message to screen
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...