BLYNK_WRITE() virtual pin

Arduino NANO 33 loT

Hi! I have problem with my project.

I want to make ‘up-down game’ with Blynk.

How can I get ‘your_answer’ value(in BLYNK_WRITE(V1) and ‘real_answer’ value(in BLYNK_WRITE(V2) to BLYNK_WRITE(V3)?

This is my code.

#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
    
char auth[] = "";
char ssid[] = "";
char pass[] = "";

WidgetLCD lcd(V0);

BLYNK_WRITE(V1) // Slier 
{
  int your_answer = param.asInt();
}

BLYNK_WRITE(V2) // Start button
{
  int start_value = param.asInt();
  
  if (start_value == 1)
  {
    lcd.clear();
    int start_value = 0;
    int set_value = 0;
    int try_count = 0;
   
    lcd.print(2, 0, "Let's start!");
    lcd.print(0, 1, "Guess the number");
    delay(3000);
    lcd.clear();

    int real_answer = random(1, 100);
    Serial.print(real_answer);
  }
}

BLYNK_WRITE(V3) // Set button
{
  
  int set_value = param.asInt();
  
  if (set_value == 1)
  { 
    lcd.print(0, 0, "your answer");
    lcd.print(0, 1, "> ");
    lcd.print(3, 1, your_answer); 
    delay(3000);
    lcd.clear();
    
    if(your_answer > real_answer){
      lcd.print(0, 0, "down");
      delay(3000);
      lcd.clear();
    }
    else if (your_answer < real_answer) {
      lcd.print(0, 0, "up");
      delay(3000);
      lcd.clear();
    }
    else {
      lcd.print(0, 0, "You win!");
      delay(3000);
      lcd.clear();
    }
  }
}

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  lcd.clear();

  randomSeed(analogRead(0));

  delay(1000);

  int start_value = 0;
  int set_value = 0;
  
  lcd.print(3, 0, "Press the");
  lcd.print(2, 1, "start button");
}

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

make the variables global

I tried that but it didn’t work

Your code shows different.

Use Global variables and assign variables to the two options (Yours and Real) and then compare as needed with if(yours == real) (do something) else (do something different) as you have sorta done… but by NOT using global variables there can be no comparison across different functions.

BTW, I fixed your code formatting as needed in this forum (three backticks ``` fore and aft of code), and delay() is always a bad idea with Blynk as it is a blocking command and that blocks EVERYTHING including Blynk’s heartbeat and communication.

Hi. Thank you for your reply. Where can I use global variables in my code?

You need to make your_answer start_value etc global by declaring them at the top of your code and removing the int when you use these variables in the BLYNK_WRITE functions.

Pete.

I changed my code but ramdom value only print out “33”.

#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
    
char auth[] = "";
char ssid[] = "";
char pass[] = "";

int your_answer = 0;
int real_answer = random(0, 100);

WidgetLCD lcd(V0);

BLYNK_WRITE(V1)
{
  int your_answer = param.asInt();
  lcd.print(0, 0, "your answer");
  lcd.print(0, 1, "> "); 
  lcd.print(3, 1, your_answer);
  delay(3000);
  lcd.clear();

  if(your_answer > real_answer){
    lcd.print(0, 0, "down");
    delay(3000);
    lcd.clear();
  }
  else if (your_answer < real_answer) {
    lcd.print(0, 0, "up");
    delay(3000);
    lcd.clear();
  }
  else {
    lcd.print(0, 0, "You win!");
    delay(3000);
    lcd.clear();
  }
}

BLYNK_WRITE(V2)
{
  int start_value = param.asInt();  
  
  if (start_value == 1)
  {
    lcd.clear();
    int start_value = 0;
    int set_value = 0;
    int try_count = 0;
   
    lcd.print(2, 0, "Let's start!");
    lcd.print(0, 1, "Guess the number");
    delay(3000);
    lcd.clear();
  
    Serial.print(real_answer);
  }
}

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  lcd.clear();

  randomSeed(analogRead(0));

  delay(1000);

  int start_value = 0;
  int set_value = 0;
   
  lcd.print(3, 0, "Press the");
  lcd.print(2, 1, "start button");
}

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

@Hwi-Yeong you need to format your code correctly with three backticks when you post it to the forum…

Please edit your post, using the pencil icon at the bottom, remove the blockquote that you’ve used and add triple backticks instead.

Pete.

Oh I’m sorry… I edited it.

Pete.

I’ll try! Thank you :slight_smile: