.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! 2.1.6 Joystick ============== .. note:: .. image:: ../img/mcp3008_and_adc0834.jpg :width: 25% :align: left Depending on your kit version, please identify whether you have **ADC0834** or **MCP3008** and proceed with the matching section. Introduction ------------ In this project, We're going to learn how joystick works. We manipulate the Joystick and display the results on the screen. Components ---------- .. image:: ../img/image317.png Schematic Diagram ----------------- When the data of joystick is read, there are some differents between axis: data of X and Y axis is analog, which need to use ADC0834 to convert the analog value to digital value. Data of Z axis is digital, so you can directly use the GPIO to read, or you can also use ADC to read. .. image:: ../img/image319.png .. image:: ../img/image320.png Experimental Procedures ----------------------- **Step 1:** Build the circuit. .. image:: ../img/image193.png **Step 2:** Go to the folder of the code. .. raw:: html .. code-block:: cd ~/davinci-kit-for-raspberry-pi/nodejs/ **Step 3:** Run the code. .. raw:: html .. code-block:: sudo node joystick.js After the code runs, turn the Joystick, then the corresponding values of x, y, Btn are displayed on screen. **Code** .. code-block:: js const Gpio = require('pigpio').Gpio; const ADC0834 = require('./adc0834.js').ADC0834; const adc = new ADC0834(17, 18, 22); const btn = new Gpio(25, { mode: Gpio.INPUT, pullUpDown: Gpio.PUD_UP, }); setInterval(async() => { x_val = await adc.read(0); y_val = await adc.read(1); btn_val = btn.digitalRead(); console.log(`x = ${x_val}, y = ${y_val}, btn = ${btn_val}\n`); }, 100); **Code Explanation** .. code-block:: js const ADC0834 = require('./adc0834.js').ADC0834; We import an ``ADC0834`` constructor to use the adc0834 module. .. code-block:: js setInterval(async() => { x_val = await adc.read(0); y_val = await adc.read(1); btn_val = btn.digitalRead(); console.log(`x = ${x_val}, y = ${y_val}, btn = ${btn_val}\n`); }, 100); When reading the values of multiple channels of ADC0834 at the same time, asynchronous programming is required. We build a promise function here, And use the await instruction of async function to elegantly write this complex asynchronous task. * `Promise `_ * `Async Function `_ Phenomenon Picture ------------------ .. image:: ../img/image194.jpeg