This is a self-centering spring loaded joystick, meaning when you release the joystick it will center itself. The goal of the joystick is to communicate motion in 2D (2-axis) to an Arduino. This is achieved by housing two independent 10K potentiometers (one per axis). These potentiometers are used as dual adjustable voltage dividers, providing 2-Axis analog input in a control stick form. This joystick also contains a switch which activates when you push down on the cap. The lever works no matter what position the joystick is in. In order to read the joystick’s physical position, we need to measure the change in resistance of a potentiometer. The values on each axis can vary from 0 to 1023. So, if the stick is moved on X axis from one end to the other, the X values will change from 0 to 1023 and similar thing happens when moved along the Y axis. When the joystick stays in its center position the value is around 512. (image credits lastminuteengineers.com)
I found a simple project with joystick here and developed that for the game.
I did wiring this way:
Now it’s time to connect Arduino to the computer and upload the following code from Arduino IDE to the board:
int xValue = 0 ; // read value of the X axis int yValue = 0 ; // read value of the Y axis int bValue = 0 ; // value of the button reading void setup() { Serial.begin(9600) ; // Open the serial port pinMode(8,INPUT) ; // Configure Pin 8 as input digitalWrite(8,HIGH); } void loop() { // Read analog port values A0 and A1 xValue = analogRead(A0); yValue = analogRead(A1); // Read the logic value on pin 8 bValue = digitalRead(8); // We display our data separated by a comma Serial.print(xValue,DEC); Serial.print(","); Serial.print(yValue,DEC); Serial.print(","); Serial.print(!bValue); // We end with a newline character to facilitate subsequent analysis Serial.print("\n"); // Small delay before the next measurement delay(10); }
After this I opened the Processing application downlaoded from official website and run the following code.
import processing.serial.*; //import the Serial library Serial myPort; int x; // variable holding the value from A0 int y; // variable holding the value from A1 int b; // variable holding the value from digital pin 2 PFont f; // define the font variable String portName; String val; float x1; // variable for random x coordinate of the circle float y1; // variable for random y coordinate of the circle void setup() { size ( 512 , 512 ) ; // window size // we are opening the port myPort = new Serial(this, Serial.list()[2], 9600); myPort.bufferUntil('\n'); // choose the font and size f = createFont("Arial", 16, true); // Arial, 16px, anti-aliasing textFont ( f, 16 ) ; // size 16px x1 = random(1024); y1 = random(1024); } // drawing loop void draw() { fill(0) ; // set the fill color to black clear() ; // clean the screen fill(255,0,0); // set the fill color to red ellipse(x1/2, y1/2, 50, 50); fill(255) ; // set the fill color to white textSize(16); text("Press any key to restart the game",10,20); ellipse(x/2,y/2, 50, 50); if (b == 1 && abs(x-x1) < 25 && abs(y-y1) < 25) // check if the button is pressed and ball is on the circle { textSize(64); fill(0, 255, 0); // set the fill color to green text("WELL DONE!!!", 100, 300); // myPort.stop(); // setup(); } else if (b == 1 && (abs(x-x1) >= 25 || abs(y-y1) >= 25)) // the button is pressed but the ball is not on the circle { fill(255, 0, 0); // set the fill color to red textSize(32); text("TRY MORE!!!", 100, 300); // draw a circle with a certain coordinates fill(255); ellipse(x/2, y/2, 50, 50); } else { //draw a circle with a certain coordinates fill(255); ellipse(x/2,y/2, 50, 50); } // display data textSize(16); fill(255); text("X="+(1023-x)+" Y="+(1023-y),10,50); // coordinates of the ball // we display data textSize(16); fill(255, 0, 0); text("X="+(1023-x1)+" Y="+(1023-y1),10,70); // coordinates of the circle } // fuction or restarting game when a key is pressed void keyPressed() { myPort.stop(); setup(); } // data support from the serial port void serialEvent(Serial myPort) { // read the data until the newline n appears val = myPort.readStringUntil('\n'); if (val != null) { val = trim(val); // break up the decimal and new line reading int[] vals = int(splitTokens(val, ",")); // we assign to variables x = vals[0]; y = vals[1] ; b = vals[2]; } }
Some explanations for the code
if (b == 1 && abs(x-x1) < 25 && abs(y-y1) < 25)
myPort = new Serial(this, Serial.list()[2], 9600);
And the video with the result:
The problems I have faced and issues to be fixed:
myPort.stop();
setup();
void keyPressed() {
myPort.stop();
setup();
}
Here you can find the file with the Arduino code and the code for Processing to lounch the game.
#define BLYNK_PRINT SwSerial #include <SoftwareSerial.h> SoftwareSerial SwSerial(10, 11); // RX, TX #include <BlynkSimpleStream.h> #include <DHT.h> // You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon). char auth[] = "wzbjGe-U658FP_c18CIhynGo9js15_5c"; //paste here your auth token #define DHTPIN 2 // What digital pin we're connected to #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); BlynkTimer timer; // This function sends Arduino's up time every second to Virtual Pin (5). // In the app, Widget's reading frequency should be set to PUSH. This means // that you define how often to send data to Blynk App. void sendSensor() { float h = dht.readHumidity(); float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit if (isnan(h) || isnan(t)) { SwSerial.println("Failed to read from DHT sensor!"); return; } // You can send any value at any time. // Please don't send more that 10 values per second. Blynk.virtualWrite(V5, h); Blynk.virtualWrite(V6, t); } void setup() { // Debug console SwSerial.begin(9600); // Blynk will work through Serial // Do not read or write this serial manually in your sketch Serial.begin(9600); Blynk.begin(Serial, auth); dht.begin(); // Setup a function to be called every second timer.setInterval(1000L, sendSensor); } void loop() { Blynk.run(); timer.run(); }
brew install socat
blynk-ser.sh
./blynk-ser.sh -c /dev/cu.usbmodem14101
. Here instead of the “usbmodem14101” should be the name of the port which the Arduino board is connected to.Download the code here.
February 3, 2022