EasyManua.ls Logo

Freenove Ultimate Starter Kit - Page 137

Freenove Ultimate Starter Kit
286 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
137
Chapter 12 Joystick
www.freenove.com
support@freenove.com
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 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 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

Table of Contents