18 April 2021

Game Controller

This Game controller started off completely different for me, as I originally planned to use the Circuit Playground Express to create the program needed, but I went with the Leonardo instead. I originally had a trill square, but I actually broke it by accident (soldering is more difficult than I anticipated), so I had to get a replacement resistive touch sensor as fast as I could, so I acquired a Nextion 3.2 inch display which is a TFT screen with a resistive touch screen overlayed on top of the screen. To add the image to the screen I was able to use Nextions editing software which not only allows you to create the image, it makes it possible to send the information to the arduino. I was going to use a lunchbox no matter what, as any of the games I picked I had a fond memory of them during school, and I associate a lunchbox with school.

The mapping is fairly simple, the arduino powers the screen, and the TX and RX are connected to pins 10 and 11 on the arduino, To help give some signifiers, I added arrows in the area that they are supposed to be touched at, just so that anyone who tries to use it, can use it. Also, when it gets plugged in, the screen turns on, so that should also indicate to a person to touch it.

How could I possibly get it to work as a press and hold and not constantly tap?









To get it to work correctly because I only used one serial port, you need to go to NexConfig.h and do this
/** 
 * Define DEBUG_SERIAL_ENABLE to enable debug serial. 
 * Comment it to disable debug serial. 
 */
/**#define DEBUG_SERIAL_ENABLE */

/**
 * Define dbSerial for the output of debug messages. 
 */
/** #define dbSerial Serial */

/**
 * Define nexSerial for communicate with Nextion touch panel. 
 */
/** #define nexSerial Serial2 */

#include <SoftwareSerial.h>
extern SoftwareSerial HMISerial;
#define nexSerial HMISerial


CODE:
#include <SoftwareSerial.h> // Library for Software Serial Port #include <Keyboard.h> // Keyboard Emulation #include <Nextion.h> SoftwareSerial HMISerial(10, 11); // Nextion TX to pin 10 and RX to pin 11 of Arduino // This part is used to declare all the objects we can receive from the Nextion Display // (page, ID, Object name) NexHotspot m0 = NexHotspot(0, 3, "m0"); // "Left Arrow" HotSpot on Page 1 NexHotspot m1 = NexHotspot(0, 1, "m1"); // "Up Arrow" HotSpot on Page 1 NexHotspot m2 = NexHotspot(0, 2, "m2"); // "Right Arrow" HotSpot on Page 1 NexHotspot m3 = NexHotspot(0, 4, "m3"); // "Down Arrow" HotSpot on Page 1 // This part is used to list the possible touchscreen events in an Array NexTouch *nextion_touch_events[] = { &m0, // Page 0 HotSpot &m1, &m2, &m3, NULL // End string }; // This part is for the different functions for Main Screen Touch events (page zero) void m0PushCallback(void *ptr) { Keyboard.press(216); // 'UP ARROW KEY' delay(600); Keyboard.releaseAll(); } void m1PushCallback(void *ptr) { Keyboard.press(218); // 'UP ARROW KEY' delay(600); Keyboard.releaseAll(); } void m2PushCallback(void *ptr) { Keyboard.press(215); // 'ENTER KEY' delay(600); Keyboard.releaseAll(); } void m3PushCallback(void *ptr) { Keyboard.press(217); // 'DOWN ARROW KEY' delay(600); Keyboard.releaseAll(); } // Main Setup void setup() { HMISerial.begin(9600); // Start Software Serial at 9600bauds nexInit(); // Nextion Display initalize Keyboard.begin(); // Start Keyboard emulation // Link the touchscreen events to their relative functions in the code // attachPush for press events or attachPop for release events m0.attachPush(m0PushCallback); m1.attachPush(m1PushCallback); m2.attachPush(m2PushCallback); m3.attachPush(m3PushCallback); } // Main Loop void loop() { delay(10); // small delay before checking again for touch events nexLoop(nextion_touch_events); // Check for any touch event and run the associated function }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.