Confused about Blynk

Ok, so I want to create a program for the Black Jack card game, and I don’t know where to start. I know I’m using the Blynk app to allow the user to input whether he wants to be dealt another card, or he has enough, and also display the cards he has and its sum. Then, the user plays against a computer, and the sum of the cards are displayed on a matrix of LEDs.

Here I’m not sure if I write the program on the Particle IDE or the Blynk sketch? Sorry, this may sound really dumb but I don’t know where to start. Since I need to generate random numbers when dealing the cards, do I program it on the Particle IDE? How do I connect the Particle to Blynk? I’m confused. Some help on how or where to start is much appreciated. Thanks!

You would use the Particle Web IDE and there you’ll also find the Blynk library and some usage examples therein.

1 Like

I suggest you first program your device in the WebIDE and if you then before forward information to your smartphone, you need a Blync account and only few commands to the data to be transmitted.
However, I have not got an exact idea of your project yet: card playing with a photon - sounds interesting!

Thanks for your advice! It’s nothing too fancy…in fact I think it’s really not as fancy as you expected. So the idea is this:

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, so I think that using Blynk may be a little too complicated for me right now, and am switching the interface to the internet webpage instead. I’m wondering how to publish a message on the webpage signalling that the user wins or vice versa, if one side goes over 21. For example, from this link


, I know that I need to use the Particle.publish () function, but that needs the user to prompt the button for the message to be displayed. I’m wondering if there’s a way to do that automatically.

Also, a huge part of my program just runs om the IDE without much interface with the hardware itself. I just need the Photon to display the sum of the cards the user is dealt with, and turn on one of two LEDs signalling the player’s decision.

Actually Blynk would be rather easy to use for that.

How much time have you got till deadline?

2 Likes

@ScruffR Thanks! I’m willing to learn how to use it if you can show me where. I have two weeks till deadline.

So far the documentation I’ve read about the virtual write pins in Blynk communicates to the hardware, not the software in the IDE itself. I’m wondering if Blynk has something that supports what I need?

You can actually put any code in a BLYNK_WRITE() macro. You don’t need to put any hardware related commands in these.

BTW, IDE stands for integrated development environment, so once you have built your firmware and flashed it to the device the IDE is completely out of the picture, so I don’t quite get this statement

@ScruffR Oh, if so would my idea work at all? This is because I need to call the random generator function again if the user decides HIT, and this function has nothing to do with hardware at all?

Sorry, I’m getting confused here.

You mentioned hardware in connection with the hardware pins. Which you could but don’t have to control in a BLYNK_WRITE() macro.
But when you talk about a random generator that mostly is a software construct - irrespective of the fact that this software construct is “living” ona hardware device, of course.

So yes, that is doable with Blynk and a Photon and it’s not even difficult IMO.

Just as a side note, if you are simulating a card game, I’d not use the random generators results direct, but would us the result of it as index into a virtual stack of shuffled cards to have a more realistic distribution of virtual cards to deal.

@ScruffR I’ve come up with the first draft of code, I see how your suggestion on simulation would improve the randomization process, but I’m not clear on what you meant by virtual stack of shuffled cards. There seems to be an error in my program which makes it unable to compile. Below is my program:

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


#define BLYNK_PRINT Serial 
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
 
 // This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#include <stdlib.h>
#include <time.h>

char auth[] = "YourAuthToken";

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, compStats, *compFlag, *playerFlag;

playerStats.cards [25] = {0}; playerStats.count = 0, playerStats.sum = 0;
compStats.cards [25] = {0}; compStats.cards [25] = {0}, compStats.sum = 0;

enum { STAND,HIT };

void displayonLCD ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct;
    for ( int i = 0; i < structPointer->count; i++ )
    {
        lcd.print(i, 0, array [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 , count; i++ )
    {
        structPointer->sum += structPointer->cards [i];
    }
    return structPointer;
    
}
WidgetLCD lcd(V1);

void setup()
{
    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); 

}

srand (time (NULL));

for ( int i = 0; i < 2; i++ )
{
    playerStats.cards [i] = rand () % 10 + 1;
}

playerFlag = sumofCards ( &playerStats );
displaySum (playerFlag);

for ( int i = 0; i < 2; i++ )
{
    compStats.cards [i] = rand () % 10 + 1;
}

compFlag = sumofCards ( &compStats );
while ( compFlag->sum < 17 )
{
    compFlag = random_generator ( &compStats );
    compFlag = sumofCards (compFlag);
}



void loop ()
{
    Blynk.run();
    
    displayonLCD ( playerFlag );

BLYNK_WRITE(V1)
{
    int pinData = param.asInt ();
}

}

if ( pinData == 1)
{
    playerFlag = random_generator (playerFlag);
}

else if (pinData == 0)
{
    playerFlag = sumofCards (playerFlag);
}
break;
}


if ( playerFlag->sum > compFlag->sum )
{
    lcd.print(0,0,"Player wins!")
}

else if (  playerFlag->sum < compFlag->sum )
{
    lcd.print (0,0,"Computer wins!")
}

else if (playerFlag->sum ==compFlag->sum )
{
    lcd.print("It's a draw");
}

The compiler return as error message:
/workspace//src/blackjack.cpp:7:22: fatal error: Ethernet.h: No such file or directory
void setupMatrix ( void *mat );

I don’t know how to correct that. Can you please help me out here? Also, do you think this program would work?

Thanks!

You’re including a library that can’t be found, and isn’t needed as none of the Particle products has ethernet (other than the Pi). Try factoring that out.

@Moors7 Thanks! Which ones? I’m rather new to Blynk, so I included this block of libraries:

#define BLYNK_PRINT Serial 
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

because I thought that they are needed to read data from Blynk. Can all the above be deleted?

If you look at the Blynk library it’ll give you some examples on which you can base your code :wink:

@Moors7 To confirm from the examples, only#include <blynk.h> is needed? Also, another compiler error message is this:

/workspace//src/blackjack.cpp:13:15: error: unable to find numeric literal operator ‘operator"" bc6ae35b54915b4591cd0b994c27a’
#include <time.h>
^

Is it because the time.h library in C cannot be used here?

It seems that there are many errors in my code…the four most common error messages are like this:

/workspace//src/blackjack.cpp:41:38: error: expected primary-expression before ‘.’ token
{

/workspace//src/blackjack.cpp:123:7: error: expected constructor, destructor, or type conversion before ‘(’ token
// initialize each matrix

/workspace//src/blackjack.cpp:125:1: error: expected unqualified-id before 'for’
setupMatrix(&matrix2);
^

/workspace//src/blackjack.cpp:125:18: error: ‘i’ does not name a type
setupMatrix(&matrix2);
^

Any suggestions about the common causes (if any) of the above error messages?

Thanks!

@limxx518, first, the front end of your code needs to look like 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>


char auth[] = "YourAuthToken";

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 };

You can’t initialize struct data the way you were doing. Also, you were referring to the lcd object before declaring it so it needs to be moved up. Beyond this code you have total havoc with for, while and if statements dangling by themselves outside of any function body. I suggest you clean up your code first then come back with your next round of questions.

@peekay123 Thanks so much for your help. I really appreciate it because I’m on my own in this project. If you don’t mind me asking, the code I’ve written so far, if I were to package them nicely into functions, would they work? For it wouldn’t make sense for me to reorganize code that is already broken in the first place.

Thanks again!

@limxx518, I can’t really tell. Do the cleanup and repost your code so I can take a look.

1 Like

@peekay123 I’ve cleaned up the code and edited it a little. There are less error messages when compiled, so I guess it’s a work in progress. I realized that I need two push buttons on my Blynk app, and am wondering how to declare them in the code? The numerous ways that I’ve tried are wrong.

Also, I’m wondering if I can use the time.h library? I only started learning to program in C, and that’s the only way I know to generate random numbers.

Below is my cleaner (hopefully) code:

    > // 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 <time.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),(V2);


    > enum { STAND,HIT };

    > 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 );

    > srand (time (NULL));

    > void setup()
    > {
    >     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();
>     
>     displayonLCD ( playerFlag );
>     
>     BLYNK_WRITE(V1)
>     {
>         int pinData = param.asInt ();
>     }
>     
>     BLYNK_WRITE(V2)
>     {
>         int pinData = param.asInt ();
>     }
>     
>     if ( V1 == 1 && V2 == 0)
>     {
>         playerFlag = random_generator (playerFlag);
>         playerFlag = sumofCards ( playerFlag );
>         displaySum (playerFlag);
>         displayonLCD (playerFlag);
>     }
>     
>     else if (V1 == 0 && V2 == 1)
>     {
>         playerFlag = sumofCards (playerFlag);
>     }
>     break;
> }
>     
>     
> if ( playerFlag->sum > compFlag->sum )
> {
>     lcd.print(0,0,"Player wins!")
> }
>     
> else if (  playerFlag->sum < compFlag->sum )
>  {
>      lcd.print (0,0,"Computer wins!")
>  }
>     
> else if (playerFlag->sum ==compFlag->sum )
> {
>     lcd.print("It's a draw");
> }



> 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++ )
>     {
>         compStats.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;
>     
> }

Thank you!