Confused about Blynk

@limxx518, real quick cause I need to go to bed…

  • both BLYNK_WRITE() functions need to be outside of loop()!
  • V1 and V2 are not variables. Instead, you set a global variable with param.asInt() so you need to revise your code in both BLYNK_WRITE() functions.
  • You need to read up on C++. The break; you have after the else if (V1 == 0 && V2 == 1) will do nothing and may cause an error.
  • This code is not in any function:
> 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");
> }

That’s all I have for now.

@peekay123 No worries. Thanks so much for your help. I’ll continue to work on it. :grinning:

@peekay123 Okay, I've edited the code following your corrections, and there are just 4 error messages left to correct! Here is the most recent 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 <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),(V2);


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

BLYNK_WRITE(V1)
{
     pinDataV1 = param.asInt ();
}
    
BLYNK_WRITE(V2)
{
    pinDataV2 = 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);
        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("It's a draw");
    }
    
}

I dropped the #include <time.h> since the compiler could not recognize it. Instead I followed the example provided in this link:

The four error messages are:

/workspace//src/blackjack.cpp:8:15: error: unable to find numeric literal operator 'operator"" bc6ae35b54915b4591cd0b994c27a'
void setupMatrix ( void *mat );
^
/workspace/lib/blynk/src/Blynk/BlynkHandlers.h:21:13: error: expected unqualified-id before numeric constant
#define V2 2
^
/workspace/lib/blynk/src/Blynk/BlynkHandlers.h:21:13: error: expected ')' before numeric constant
#define V2 2
^
/workspace//src/blackjack.cpp:216:32: error: no matching function for call to 'WidgetLCD::print(const char [12])'
}
^

I don't know how to resolve all the above errors. How do I declare two push buttons on Blynk? And also, I'm not sure how to print the strings in decideWinner function to the LCD terminal of Blynk.

Thanks!

That string should have quotes around it

char auth[] = "558bc6ae35b54915b4591cd0b994c27a";

I think the last error has to do with lcd.print("It's a draw"). In your other calls to lcd.print, you pass two ints and a string, and I think that is necessary.

1 Like

A usual Blackjack Dealer's Shoe is filled with 1 to 8 decks of 52 cards so in each round of 52 to 416 cards each number can only appear a limited number of times. A normal random generator would not heed this restriction, so you'd need to add some logic to do this yourself.

BTW, there is no Ethernet connectivity on any of the Particle devices (unless you attach one) and hence there is no Ethernet.h (unless you import the library for the extra hardware).
And since there is no Ethernet, there will be little use in including BlynkSimpleEthernet.h either, IMO.

Particle devices are all about wireless, so you should look in that direction.

(Update: I see I was a bit late to the party and most of the questions were already dealt with :blush: )

1 Like

@ScruffR There's still some issues left to be fixed :relaxed: It seems that I'm declaring WidgetLCD wrongly, these error messages pop up after compiling:

/workspace/lib/blynk/src/Blynk/BlynkHandlers.h:21:13: error: expected unqualified-id before numeric constant
#define V2 2
^
/workspace/lib/blynk/src/Blynk/BlynkHandlers.h:21:13: error: expected ')' before numeric constant
#define V2 2
^

Also, I'm just designing this to be a 1 player version versus the "computer", so would a deck of cards suffice? It would be great to scale this up to include more players, but for now I hope to get the 1 player version working to meet next week's deadline.

Thanks for your help!

You are not allowed to define these in your own code at all. V2 is already predefined in the blink library.

What does this line do?

WidgetLCD lcd(V1),(V2);

The part after the comma does not make any sense.

@ScruffR I’m not sure at all. I’m just guessing here. I assumed that WidgetLCD lcd(V1) is to declare Virtual Pin 1 in Blynk, since I’m having two buttons it made sense for me to declare (V2) as well.

Nope, WidgetLCD lcd(V1) creates a WidgetLCD object named lcd which is tied to the virtual pin V1 but V2 does not belong anywhere here.
You could declare another WidgetLCD other(V2) but what do you actually intend to do with V2?
Do you actually want those virtual pins tied to an WidgetLCD object?

@ScruffR I'm not sure if I need two virtual pins, but I know I need two push buttons on the Blynk app. One for HIT and another for STAND.

I'm confused. What does a WifgetLCD object mean? Basically I just need two push buttons on Blynk, and a function to read which one of both is pressed.

You added that WidgetLCD to your code and you are using it all over the place via lcd.....

I guess you need to get your basics sorted first :wink:
http://docs.blynk.cc/#widgets-displays-lcd
http://docs.blynk.cc/#widgets-controllers-button

2 Likes

@ScruffR Oh I see that I’m mixing up LCD with the push buttons. Major brain fart…:blush: Yeah, so I’m clear on all this now. I do not need to declare the virtual pins for the push buttons as I did for LCD, as I had declared them under BLYNK_WRITE, yes? :grinning:

Generally speaking, I'd say yes, but looking at your code, I'm in doubt :wink:

IMHO (since I don't use Blynk really) I doubt that this is tha way to check the state of the virtual buttons.

Maybe you should go back to the very basic samples that come with Blynk and try to understand how things are done and why.

@ScruffR Thanks for all your help so far. I’ve looked at the basic sketches, and they do not seem to be very helpful to me. The sketch of interest to me is this:

I think it would work though if I declared int pinDataV1 and pinDataV2 as global variables. But maybe this question is better suited for the Blynk community.

@ScruffR I’m meaning something like this:

if ( pinDataV1 == 1 && pinDataV2 == 0)

Yup, that should do the trick.

1 Like

@ScruffR Thanks!

@ScruffR Something is not working, when I flashed the code to my Photon, no numbers show up on the LCD screen of my Blynk app. The status of my Photon is online on the Blynk app though.

Is it something minor, or the whole code logic is just wrong? (I really hope not! )

Also, I’m wondering how this program is terminated, since I didn’t write any functions after decideWinner. Must I add something after this?

Edit: Some parts of the program are working, after pressing the HIT and STAND button randomly, the line It's a draw appers on the LCD widget. But after that, nothing seems to be happening. I can’t figure out what the program is doing. Does in continue the process over and over again in the loop function?

Try adding some serial prints, and check those with a serial monitor.

1 Like