[SOLVED] Joystick and Arduino

Hello all,

I am working on a simple project where a Joystick on the Blynk app on my phone sends it’s values to the Arduino and then gets printed on my LCD screen. For the most part, things are working. The app and the Arduino are connected, and the LCD screen does print data. However, the data isn’t accurate and doesn’t represent the joystick’s movements whatsoever. Below is the Arduino Code, Terminal code, and some screenshots from the app.

Arduino Code

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3);

#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 8, 9, 7);

char auth[] = "64ff1d6e5ff646a5aff28f52de11c914";

int x = 6;
int y = 5;

unsigned long xvalue;
unsigned long yvalue;

void setup() {

  SwSerial.begin(9600);
  Blynk.begin(auth);

  Serial.begin(9600);
  lcd.begin(16, 2);

  lcd.clear();

  pinMode(x, INPUT);
  pinMode(y, INPUT);
      
}

void loop() {

  Blynk.run();

  int xvalue = analogRead(x);
  int yvalue = analogRead(y);

  lcd.setCursor(0, 0);
      
  lcd.print("X:");
  lcd.print(xvalue);

  lcd.setCursor(0, 1);
      
  lcd.print("Y:");
  lcd.print(yvalue);
      
  delay(100);
}

Terminal code:

cd ./Documents/Arduino/libraries/Blynk/scripts
bash ./blynk-ser.sh

Below are some photos of the interface from my phone.


Hello. What is purpose of delay?

That short delay is to make the data readable. Without that delay, data was going up and down so fast you couldn’t even make numbers out on the LCD screen.

Does this code make any more sense?

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3);
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 8, 9, 7);

char auth[] = "64ff1d6e5ff646a5aff28f52de11c914";

void setup() {
  
  SwSerial.begin(9600);
  Blynk.begin(auth);

  lcd.begin(16, 2);
  
}

BLYNK_WRITE(D5) {
  
  int x = param[5].asInt();

  lcd.setCursor(0, 0);
  
  lcd.print("X = ");
  lcd.print(x);

}

BLYNK_WRITE(D6) {

  int y = param[6].asInt();
  
  lcd.setCursor(0, 1);
  
  lcd.print("; Y = ");
  lcd.print(y);
  
}

void loop() {
  
  Blynk.run();
  
}

Or this?

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3);
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 8, 9, 7);

char auth[] = "YourAuthToken"; // isnert auth token here

void setup() {
  
  Serial.begin(9600);
  SwSerial.begin(9600);
  Blynk.begin(auth);

  lcd.begin(16, 2);
}

BLYNK_WRITE(V1) {
  
  int x = param[5].asInt();
  int y = param[6].asInt();

  lcd.setCursor(0, 0);
  
  lcd.print("X = ");
  lcd.print(x);

  lcd.setCursor(0, 1);
  
  lcd.print("; Y = ");
  lcd.print(y);
}

void loop() {
  
  Blynk.run();
  
}

Someone please do help.

thanks,

marco

Could you please show us somehow what do you mean here?

The values printed go up and down between around 150 - 300 with no pattern whatsoever. When I connect Blynk to the Arduino and move the Joystick, the joystick’s position does not reflect the values on the screen.

Should be

int x = param[0].asInt();
int y = param[1].asInt();

Last sketch seems most correct for me.

Yeah, I picked up on that earlier and fixed it. However, with that code, nothing is displayed by the LCD screen. I don’t really understand the BLYNK_WRITE concept because it is never even called in void loop()… The docs page leaves me very confused. why is it BLYNK_WRITE(V1) when I’m connecting it to PWM pins, not virtual pins?

If you connecting to PWM pins in joystick widget than virtual write handler not needed.

Did you read this Introduction - Blynk Documentation?

I just checked it out. What do I exactly do then to read the PWM pins? I want the digital joystick outputting data to the hardware(my lcd screen)

Edit/update: My values just spiked to the thousands…

Ok. I’ll try to explain a bit.
You can work directly with Digital/Analog pins with Blynk. In that case no need to set pinmode. It just works out of box. But as you want write and read value from pins at sane time you have to go via Virtual Pin.
So you should make “merge” mode in joystick, select virtual pin, let’s say V1. And add next code to sketch :

BLYNK_WRITE(V1) {
  int x = param[0].asInt();
  int y = param[1].asInt();
 
  //do what you need.
}

That’s it. Hope that what you need.

Thanks, I’ll try it. So if the joystick is connected to V1 in the app, and I use this code, it should work? Do I need to call BLYNK _WRITE(V1); in void loop?

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3);
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 8, 9, 7);

char auth[] = "64ff1d6e5ff646a5aff28f52de11c914"; // isnert auth token here

void setup() {
  
  Serial.begin(9600);
  SwSerial.begin(9600);
  Blynk.begin(auth);

  lcd.begin(16, 2);
}

BLYNK_WRITE(V1) {
  
  int x = param[0].asInt();
  int y = param[1].asInt();

  lcd.setCursor(0, 0);
  
  lcd.print("X = ");
  lcd.print(x);

  lcd.setCursor(0, 1);
  
  lcd.print("; Y = ");
  lcd.print(y);
}

void loop() {
  
  Blynk.run();

  BLYNK_WRITE(V1);
  
}

No. Blynk.run() does that for you.

Ah thank you. It works now!

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3);
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 8, 9, 7);

char auth[] = "64ff1d6e5ff646a5aff28f52de11c914";

void setup() {
  
  Serial.begin(9600);
  SwSerial.begin(9600);
  Blynk.begin(auth);

  lcd.begin(16, 2);
}

BLYNK_WRITE(V2) {
  
  int x = param[0].asInt();
  int y = param[1].asInt();

  lcd.setCursor(0, 0);
  
  lcd.print("X:");
  lcd.print("   ");

  lcd.setCursor(2, 0);
  lcd.print(x);

  lcd.setCursor(0, 1);
  
  lcd.print("Y:");
  lcd.print("   ");
  
  lcd.setCursor(2, 1);
  lcd.print(y);
  
}

void loop() {
  
  Blynk.run();
  
}
1 Like