Possible to use Blynk just to interface with Particle IDE?

Hey everyone, I need some guidance on using Blynk to interface with my Particle IDE. My idea involves a program to run the Blackjack game, where the user plays against my program. I need the Blynk app for the user to enter Hit or Stand, and am wondering if it is possible for this value to be sent to the Particle IDE alone?

I’ve outlined the steps to create a program for this Blackjack:

  1. Create a random number generator function to generate a random number between 1 and 10. The sum of numbers dealt to player will be displayed on the Blynk screen of player, and also LED matrices for spectators to watch. ( In case you are wondering, this is a final project for my course so these rather arbitrary features are needed for my demo to be more cool :wink: )

  2. Then, depending on the sum of the cards he is dealt with, have the user enter HIT or STAND on the Blynk app. I’m having difficulty with this part especially. How do I use Blynk to send what the user inputs to the Particle IDE? If the player decides HIT, the random number generator function in the IDE is called again. Else, program is terminated and winner determined. Is it possible for the Blynk app to only interface with the Particle IDE, as well as the LEDs? For example if user input is done through a webpage, I know I need the Particle.function (). So I’m wondering if something similar is needed with Blynk?

  3. Then, the computer being the dealer, if the sum of cards dealt is 16 or under, he has to take another card, thus the random generator function is called again. If it is 17 or above, it is STAND.

  4. Also, depending on whether the user enters HIT or STAND earlier, his decision will cause a different LED to light up.

So, this is a first major project, and I don’t know where to start. Kind users please help me out. I’ve identified 5 major functions:

Random number generator function
Function to process HIT or STAND by user
Function to light up green or red LEDs
Function to check sum of both sides
-Function to update the sum of cards displayed on the LED matrices
Am I on the right track? Thanks! And also, all the programming would be done on the Particle IDE right?

Ok, my project involves a player playing Blackjack with the computer through the Blynk app. Basically, the computer who is the dealer, generates random numbers and display them on the LCD widget of the blynk app. Then the player decides either to HIT or STAND. For the dealer, he keeps on generating a random number for himself until his sum is greater than 16, which he then stops. The sum of the player cards are displayed on a pair of 8x8 Adafruit LED matrices, which are connected to my Particle Photon. My code can compile, but the numbers of the player’s card do not show up on screen. When I added Serial.println statements to debug, no numbers show up either. I’m wondering which part of my code is wrong. Help is really much needed! I’ve spent two sleepless nights on this:

// This #include statement was automatically added by the Particle IDE.
#include <adafruit-led-backpack.h>
// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#include <stdlib.h>
#include <application.h>

char auth[] = "558bc6ae35b54915b4591cd0b994c27a";

Adafruit_8x8matrix matrix1;
Adafruit_8x8matrix matrix2;

void setupMatrix ( void *mat )
{
  Adafruit_8x8matrix *m = (Adafruit_8x8matrix*) mat;
  m->clear();
  m->writeDisplay();
  m->setTextSize(1);
  m->setTextWrap(false);
  m->setTextColor(LED_ON);
  m->setRotation(0);
  m->setCursor(0, 0);
}

struct cardsandCount
{
    int cards [25];
    int count;
    int sum;
};
    
struct cardsandCount playerStats = { {0}, 0, 0};
struct cardsandCount compStats = { {0}, 0, 0};
struct cardsandCount *compFlag;
struct cardsandCount *playerFlag;

WidgetLCD lcd(V1);


enum { STAND,HIT };

int pinDataV1 = 0, pinDataV2 = 0;

struct cardsandCount *initialdealPlayer ( struct cardsandCount *theStruct );
struct cardsandCount *initialdealComp (struct cardsandCount *theStruct );
void displayonLCD ( struct cardsandCount *theStruct );                          //declaration of functions, function definitions at the bottom
void displaySum ( struct cardsandCount *theStruct );
struct cardsandCount *random_generator ( struct cardsandCount *theStruct );
struct cardsandCount *sumofCards ( struct cardsandCount *theStruct );
void decideWinner ( struct cardsandCount *playerStruct, struct cardsandCount *compStruct );


void setup()
{
    uint32_t seed = millis(); 
    srand(seed);
     
    Serial.begin(9600);
    delay(5000); // Allow board to settle
    
    lcd.clear();

    Blynk.begin(auth);
    
      // set the I2C address of each matrix
    matrix1.begin(0x70);
    matrix2.begin(0x71);

    // initialize each matrix
    setupMatrix(&matrix1);
    setupMatrix(&matrix2); 
    
    playerFlag = initialdealPlayer ( &playerStats );
    playerFlag = sumofCards ( playerFlag );
    displaySum (playerFlag);
    displayonLCD (playerFlag);
    
    compFlag = initialdealComp (&compStats);
    compFlag = sumofCards ( compFlag );
    while ( compFlag->sum < 17 )
    {
         compFlag = random_generator ( compFlag );
         compFlag = sumofCards (compFlag);
    }
    

}


void loop ()
{
    Blynk.run();
    
}
    
    
struct cardsandCount *initialdealPlayer ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct;
    
    for ( int i = 0; i < 2; i++ )
    {
        structPointer->cards [i] = rand () % 10 + 1;
    }
    
    return structPointer;
}

struct cardsandCount *initialdealComp (struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct;
    
    for ( int i = 0; i < 2; i++ )
    {
        structPointer->cards [i] = rand () % 10 + 1;
    }
    return structPointer;
}

void displayonLCD ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct;
    for ( int i = 0; i < structPointer->count; i++ )
    {
        lcd.print(i, 0, structPointer->cards [i]);
    }
}

void displaySum ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer;
    
    char ones = structPointer->sum % 10;
    char tens = structPointer->sum / 10;
    
    // write the ones digit to matrix1
  matrix1.clear();
  matrix1.setCursor(0, 0);
  matrix1.write(tens + '0'); // the ASCII value of tens is ‘0’+tens
  matrix1.writeDisplay();

  // write the tens digit to matrix2
  matrix2.clear();
  matrix2.setCursor(0, 0);
  matrix2.write(ones + '0');
  matrix2.writeDisplay();
}


struct cardsandCount *random_generator ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct;
    int i = 0;
    while( structPointer->cards[i] != 0 )
    {
        i++;
    }
    
    structPointer->cards[i] = rand () % 10 + 1;
    structPointer->count++;
    return structPointer;
}

struct cardsandCount *sumofCards ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct ;
    for ( int i = 0; i < structPointer->count; i++ )
    {
        structPointer->sum += structPointer->cards [i];
    }
    return structPointer;
    
}

BLYNK_WRITE(V1)
{
     pinDataV1 = param.asInt ();
}
    
BLYNK_WRITE(V2)
{
    pinDataV2 = param.asInt ();

    
    if ( pinDataV1 == 1 && pinDataV2 == 0)
    {
        playerFlag = random_generator (playerFlag);
        playerFlag = sumofCards ( playerFlag );
        displaySum (playerFlag);
        displayonLCD (playerFlag);
    }
    
    else if ( pinDataV1 == 0 && pinDataV2 == 1)
    {
        playerFlag = sumofCards (playerFlag);
        decideWinner ( playerFlag, compFlag );
    }
}

void decideWinner ( struct cardsandCount *playerStruct, struct cardsandCount *compStruct)
{
    struct cardsandCount *playerPointer, *compPointer;
    
    if ( playerPointer->sum > compPointer->sum )
    {
        lcd.print(0,0,"Player wins!");
    }
    
    else if (  playerPointer->sum < compPointer->sum )
    {
        lcd.print (0,0,"Computer wins!");
    }
    
    else if (playerPointer->sum ==compPointer->sum )
    {
        lcd.print(0,0,"It's a draw");
    }
    
}

Pleeeeease help. Thanks!

1 Like

Sounds like a really cool project !

@limxx518 Please don’t post multiple topics for similar questions and/or situations, thank you. I have merged your 2nd topic into the original.

@limxx518 Are you also running a physical LCD or other display?? Your code seems a bit too complex, what with the matrix commands… which will have NO affect on the Blynk Widget LCD. EDIT: OK, I see you also have 8x8 LED matrix displays referenced.

But I am not sure how this will work with the LCD Widget? I don’t think it supports text wrapping.

@Gunner Thanks for your help! I don’t know, what I want to achieve with the LCD Widget is to display the numbers dealt to the player, that’s all. Is that possible? Also, Do you have any idea why no numbers are generated in my program at all? I’ve tried debugging by adding serial.println statements, and nothing appears on the terminal. My code looks ok to me, though

Okay, now there are some numbers being printed out to the serial terminal, but nothing is showing up on the LCD widget. I’m wondering if it is because I need to use the simple mode of the LCD widget to display numbers? Must I use the BLYNK_READ and Blynk.VirtualWrite functions to write the numbers to the LCD widget. Does lcd.print not work in this case? Thanks!

EDIT: Perhaps I haven’t been clear about my question. I would like to know if for the advanced mode, lcd.print (0,0,some_variable_in_program) would print out the variable. For example in my program, I want it to print out the array of numbers dealt to player, and those values are stored in a variable. Can lcd.print print those values out? IF it can’t what other ways could I achieve this? Thanks.

check out the various example sketched for the LCD in order to get a better understanding of it’s controls and limitations.

In simple mode you just assign data to a virtual pin for each of the two lines.

Blynk.virtualWrite(V0, data);  // data on line 0
Blynk.virtualWrite(V1, data);  /data on line 1

In advanced mode you have the control over the exact placement of the first character of your data and can send separate data to separate positions on the same line.

lcd.print(4, 0, data);  // Start placing your data at the 4th character position on the first line (X: 0-15, position Y: 0-1)

You could also consider multiple fixed display widgets, one for each card number, or a single display with a generated string of “number, space, number, space, etc.”